<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0" xml:base="http://www.hashbangcode.com">
<channel>
 <title>#! code blogs</title>
 <link>http://www.hashbangcode.com/blog</link>
 <description />
 <language>en</language>
<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/hashbangcode" /><feedburner:info uri="hashbangcode" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
 <title>PHPNW13 Call For Papers And Blind Bird Ending Soon!</title>
 <link>http://feedproxy.google.com/~r/hashbangcode/~3/JXOhXR-qiVM/phpnw13-call-papers-and-blind-bird-ending-soon-716.html</link>
 <description>&lt;div class="field field-name-body field-type-text-with-summary field-label-hidden"&gt;&lt;div class="field-items"&gt;&lt;div class="field-item even"&gt;&lt;p&gt;The 5th annual PHPNW conference is set for October 5th and 6th this year and preparations for the Manchester conference are in full swing. If you want to talk at the conference then the call for papers is set to close on the 23rd June. The speaker package includes accommodation for the Friday and Saturday night as well as a speakers dinner on the Friday night. If you want some help in submitting a talk then take a look at &lt;a href="http://www.lornajane.net/posts/2008/How-to-Submit-a-Conference-Talk" title="How to Submit a Conference Talk"&gt;Lorna Mitchell's blog post on submitting a talk proposal&lt;/a&gt;. The call for papers page can be found on the &lt;a href="http://conference.phpnw.org.uk/phpnw13/call-papers/" title="PHPNW13 Call For Papers"&gt;PHPNW13 website&lt;/a&gt;.&lt;/p&gt;

&lt;img src="/sites/default/files/pictures/phpnw13badge.png" width="230" height="176" alt="phpnw13badge.png" class="pull-right" /&gt;&lt;p&gt;Once the call for paper submissions are closed it will only be a few days before the talks start being announced, at which point the blind bird ticket price of £95 will increase to the early bird ticket prices. The ticket gets you two full days of excellent talks, free tea and coffee, two full meals and a free bar (whilst it lasts) on the Saturday night. As there are only a few days left before the blind bird tickets are stopped then you should head over to the &lt;a href="http://conference.phpnw.org.uk/phpnw13/tickets/" title="PHPNW13 Tickets"&gt;PHPNW13 conference site ticket page&lt;/a&gt; and get them.&lt;/p&gt;

&lt;p&gt;The conference has got better year on year and this years event is set to be just as good as ever. It really is one of the best development conferences in the UK and absolutely worth the cost of a ticket. If you are using PHP at all and can visit Manchester in October then you absolutely must attend. The talks are of the highest calibre and I always learn a lot each year.&lt;/p&gt;

&lt;p&gt;If you are interested in seeing what has happened in previous years PHPNW conference then take a look at our previous reviews.&lt;/p&gt;

&lt;ul&gt;&lt;li&gt;&lt;a href="http://www.hashbangcode.com/blog/phpnw12-review-677.html" title="PHPNW12"&gt;PHPNW12&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://www.hashbangcode.com/blog/phpnw11-review-602.html" title="PHPNW11"&gt;PHPNW11&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://www.hashbangcode.com/blog/phpnw10-review-514.html" title="PHPNW10"&gt;PHPNW10&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="http://www.hashbangcode.com/blog/phpnw09-review-433.html" title="PHPNW09"&gt;PHPNW09&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;I'll be there to represent #! code and I hope you will attend to!&lt;/p&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="field field-name-field-blogcategory field-type-taxonomy-term-reference field-label-above"&gt;&lt;div class="field-label"&gt;Category:&amp;nbsp;&lt;/div&gt;&lt;div class="field-items"&gt;&lt;div class="field-item even"&gt;&lt;a href="/blog/category/php"&gt;PHP&lt;/a&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="field field-name-field-blogtags field-type-taxonomy-term-reference field-label-above"&gt;&lt;div class="field-label"&gt;Tags:&amp;nbsp;&lt;/div&gt;&lt;div class="field-items"&gt;&lt;div class="field-item even"&gt;&lt;a href="/category/tags/phpnw"&gt;phpnw&lt;/a&gt;&lt;/div&gt;&lt;div class="field-item odd"&gt;&lt;a href="/blog/tag/phpnw13"&gt;PHPNW13&lt;/a&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/hashbangcode/~4/JXOhXR-qiVM" height="1" width="1"/&gt;</description>
 <pubDate>Tue, 18 Jun 2013 21:15:56 +0000</pubDate>
 <dc:creator>philipnorton42</dc:creator>
 <guid isPermaLink="false">716 at http://www.hashbangcode.com</guid>
 <comments>http://www.hashbangcode.com/blog/phpnw13-call-papers-and-blind-bird-ending-soon-716.html#comments</comments>
<feedburner:origLink>http://www.hashbangcode.com/blog/phpnw13-call-papers-and-blind-bird-ending-soon-716.html</feedburner:origLink></item>
<item>
 <title>Copying Files With Secure Copy</title>
 <link>http://feedproxy.google.com/~r/hashbangcode/~3/vlkR2ETH8V0/copying-files-secure-copy-715.html</link>
 <description>&lt;div class="field field-name-body field-type-text-with-summary field-label-hidden"&gt;&lt;div class="field-items"&gt;&lt;div class="field-item even"&gt;&lt;p&gt;The secure copy command (run using &lt;strong&gt;scp&lt;/strong&gt;) is a Linux command that allows the transfer of files between two computers. This can be locally to a remote server, from a remote server to a local location, or even between two remote servers.&lt;/p&gt;

&lt;p&gt;When copying to or from a remote host scp uses ssh for the data transfer. This means that authentication is required, but the files are copied in a secure fashion. When starting a scp request the command first sets up an ssh connection to the remote location, which is then used for the rest of the transfer.&lt;/p&gt;

&lt;p&gt;It is also possible to copy the files on a local hard drive, but in this case you should probably use the standard &lt;strong&gt;cp&lt;/strong&gt; command.&lt;/p&gt;

&lt;p&gt;Secure copy is an easy command to use and has a pretty straightforward syntax. I tend to use scp more than other remote file copying tools (like rsync) as I can easily remember the flags and syntax for the command. It is also much more secure than other transfer mechanisms like the insecure FTP.&lt;/p&gt;

&lt;p&gt;scp has the syntax of the 'source' followed by the 'destination'. Either the target or the destination can be written as a remote reference. For example, to copy a local file to a remote server your would use the following.&lt;/p&gt;

&lt;span class="geshifilter"&gt;&lt;code class="text geshifilter-text"&gt;sci source.txt user@host:/destination/directory/target.txt&lt;/code&gt;&lt;/span&gt;

&lt;p&gt;Using the above will also change the name of the file from source.txt to target.txt so care must be taken if you want the file to have the same name. An alternative to the above mechanism is to leave out the target filename, this will then use the original filename specified in the copu.&lt;/p&gt;

&lt;span class="geshifilter"&gt;&lt;code class="text geshifilter-text"&gt;sci source.txt user@host:/destination/directory&lt;/code&gt;&lt;/span&gt;

&lt;p&gt;When copying to a remote server it is also a good idea to use the full directory of the destination. The default directory used is the ssh user's home directory. A full path is needed to set the destination to be elsewhere.&lt;/p&gt;

&lt;p&gt;If you want to copy a file from a remote server to a local directory you just need to put the remote part first.&lt;/p&gt;

&lt;span class="geshifilter"&gt;&lt;code class="text geshifilter-text"&gt;scp user@host:/source/directory/source.txt target.txt&lt;/code&gt;&lt;/span&gt;

&lt;p&gt;To copy the contents of an entire directory use the &lt;strong&gt;-r&lt;/strong&gt; flag. This recursively copies the directory and any sub-directories. This will copy the source directory into the destination directory and so you don't need to add the directory name here.&lt;/p&gt;

&lt;span class="geshifilter"&gt;&lt;code class="text geshifilter-text"&gt;scp -r source/directory user@host:/destination&lt;/code&gt;&lt;/span&gt;

&lt;p&gt;If your ssh server is using a non-standard port then you can use the &lt;strong&gt;-P&lt;/strong&gt; flag to let scp know what port to use for the remote connection. The following uses the port 6001 to connect to the remote host.&lt;/p&gt;

&lt;span class="geshifilter"&gt;&lt;code class="text geshifilter-text"&gt;scp -P 6001 source.txt user@host:/destination/source.txt&lt;/code&gt;&lt;/span&gt;

&lt;p&gt;Note that the port flag is a capital 'P' because the lower case &lt;strong&gt;-p&lt;/strong&gt; flag is used to preserve the modification times, access times and modes from the original file.&lt;/p&gt;

&lt;p&gt;When you run a scp command each file that is copied will be given a progress meeter showing the time left to complete the transfer. This is useful for larger files but is pointless when copying lots of smaller files as each files progress meeter will only be seen for an instant. To turn off the progress meeter use the &lt;strong&gt;-q&lt;/strong&gt; flag.&lt;/p&gt;

&lt;p&gt;If you want to save on bandwidth you have the option of limiting the bandwidth to a citation threshold with the &lt;strong&gt;-l&lt;/strong&gt; flag (specified in Kbit/s). Alternatively, you can try to allow compression to be used on the files before they are transmitted with the &lt;strong&gt;-C&lt;/strong&gt; flag, although not all versions of ssh support this.&lt;/p&gt;

&lt;p&gt;These are the basic controls for using the scp command. There are a few more flags available that control how the ssh connection is used and are only really useful if you have a special ssh setup.&lt;/p&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="field field-name-field-blogcategory field-type-taxonomy-term-reference field-label-above"&gt;&lt;div class="field-label"&gt;Category:&amp;nbsp;&lt;/div&gt;&lt;div class="field-items"&gt;&lt;div class="field-item even"&gt;&lt;a href="/blog/category/linuxunix"&gt;Linux/Unix&lt;/a&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="field field-name-field-blogtags field-type-taxonomy-term-reference field-label-above"&gt;&lt;div class="field-label"&gt;Tags:&amp;nbsp;&lt;/div&gt;&lt;div class="field-items"&gt;&lt;div class="field-item even"&gt;&lt;a href="/category/tags/linux"&gt;linux&lt;/a&gt;&lt;/div&gt;&lt;div class="field-item odd"&gt;&lt;a href="/category/tags/ssh"&gt;SSH&lt;/a&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/hashbangcode/~4/vlkR2ETH8V0" height="1" width="1"/&gt;</description>
 <pubDate>Sat, 15 Jun 2013 17:52:19 +0000</pubDate>
 <dc:creator>philipnorton42</dc:creator>
 <guid isPermaLink="false">715 at http://www.hashbangcode.com</guid>
 <comments>http://www.hashbangcode.com/blog/copying-files-secure-copy-715.html#comments</comments>
<feedburner:origLink>http://www.hashbangcode.com/blog/copying-files-secure-copy-715.html</feedburner:origLink></item>
<item>
 <title>Control Structures In Phing</title>
 <link>http://feedproxy.google.com/~r/hashbangcode/~3/ZognAsP0CoE/control-structures-phing-714.html</link>
 <description>&lt;div class="field field-name-body field-type-text-with-summary field-label-hidden"&gt;&lt;div class="field-items"&gt;&lt;div class="field-item even"&gt;&lt;p&gt;Phing has a few different tasks and elements that allow you to select paths of code execution depending on what you need to happen in a build file. These are limited to loops and if statements, but a lot of functionality can be covered with just a couple of lines of XML.&lt;/p&gt;
&lt;!--break--&gt;
&lt;h3&gt;Foreach Task&lt;/h3&gt;

&lt;p&gt;The &lt;em&gt;foreach&lt;/em&gt; task iterates over a list of items, and/or fieldsets. For every item found in the loop a phing target will be called with the item in the list passed as a parameter to the target. The foreach target requires the following two attributes.&lt;/p&gt;

&lt;ul&gt;&lt;li&gt;&lt;strong&gt;target&lt;/strong&gt; : This is the phing target to call on each parameter found in the &lt;em&gt;param&lt;/em&gt; attribute.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;param&lt;/strong&gt; : This is the parameter that will be passed to the target found in the &lt;em&gt;target&lt;/em&gt; attribute.&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;The &lt;em&gt;param&lt;/em&gt; attribute can contain either a property or a placeholder for a fileset. The property can be split in the same way as the explode() PHP function to split the string into an array. You can use the &lt;em&gt;delimiter&lt;/em&gt; attribute to alter the separator, which defaults to a comma (,), but you must also include a &lt;em&gt;list&lt;/em&gt; attribute to let the foreach target know what property to use.&lt;/p&gt;

&lt;p&gt;Here is a simple example of a foreach task that loops through a series of numbers and prints them out in turn.&lt;/p&gt;

&lt;div class="geshifilter"&gt;&lt;table class="xml geshifilter-xml" style="font-family:monospace;"&gt;&lt;tbody&gt;&lt;tr class="li1"&gt;&lt;td style="width:1px;text-align:right;margin:0;padding:0 2px;vertical-align:top;"&gt;&lt;pre style="font-family: monospace; font-weight: normal; font-style: normal"&gt;1
2
3
4
5
6
7
8
9
10
11
12
13
&lt;/pre&gt;&lt;/td&gt;&lt;td style="font-family: monospace; font-weight: normal; font-style: normal"&gt;&lt;pre style="font-family: monospace; font-weight: normal; font-style: normal"&gt;&lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;?xml&lt;/span&gt; &lt;span style="color: #000066;"&gt;version&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"1.0"&lt;/span&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;?&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;project&lt;/span&gt; &lt;span style="color: #000066;"&gt;name&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"foreachtest"&lt;/span&gt; &lt;span style="color: #000066;"&gt;default&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"foreachtest"&lt;/span&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt; 
 
  &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;target&lt;/span&gt; &lt;span style="color: #000066;"&gt;name&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"foreachtest"&lt;/span&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
    &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;property&lt;/span&gt; &lt;span style="color: #000066;"&gt;name&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"list"&lt;/span&gt; &lt;span style="color: #000066;"&gt;value&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"1,2,3"&lt;/span&gt; &lt;span style="color: #000000; font-weight: bold;"&gt;/&amp;gt;&lt;/span&gt;&lt;/span&gt;
    &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;foreach&lt;/span&gt; &lt;span style="color: #000066;"&gt;list&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"${list}"&lt;/span&gt; &lt;span style="color: #000066;"&gt;param&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"item"&lt;/span&gt; &lt;span style="color: #000066;"&gt;target&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"itemtarget"&lt;/span&gt; &lt;span style="color: #000066;"&gt;delimiter&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;","&lt;/span&gt; &lt;span style="color: #000000; font-weight: bold;"&gt;/&amp;gt;&lt;/span&gt;&lt;/span&gt; 
  &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;/target&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
 
  &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;target&lt;/span&gt; &lt;span style="color: #000066;"&gt;name&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"itemtarget"&lt;/span&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
    &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;echo&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;${item}&lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;/echo&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
  &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;/target&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
 
&lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;/project&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;This produces the following output.&lt;/p&gt;

&lt;div class="geshifilter"&gt;&lt;table class="text geshifilter-text" style="font-family:monospace;"&gt;&lt;tbody&gt;&lt;tr class="li1"&gt;&lt;td style="width:1px;text-align:right;margin:0;padding:0 2px;vertical-align:top;"&gt;&lt;pre style="font-family: monospace; font-weight: normal; font-style: normal"&gt;1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
&lt;/pre&gt;&lt;/td&gt;&lt;td style="font-family: monospace; font-weight: normal; font-style: normal"&gt;&lt;pre style="font-family: monospace; font-weight: normal; font-style: normal"&gt;foreachtest &amp;gt; foreachtest:
 
  [foreach] Calling Buildfile '/foreach.xml' with target 'itemtarget'
 
foreachtest &amp;gt; itemtarget:
 
     [echo] 1
  [foreach] Calling Buildfile '/foreach.xml' with target 'itemtarget'
 
foreachtest &amp;gt; itemtarget:
 
     [echo] 2
  [foreach] Calling Buildfile '/foreach.xml' with target 'itemtarget'
 
foreachtest &amp;gt; itemtarget:
 
     [echo] 3
 
BUILD FINISHED
 
Total time: 0.0632 seconds&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The foreach target can also include a fileset or a mapper element as a nested argument. If this is the case then the &lt;em&gt;parameter&lt;/em&gt; attribute will be used as a placeholder for the output of the nested elements. You can either create a new fileset within the foreach task, or just add a reference to one. The following Phing project creates a fileset and passes this to a foreach target, which prints each filename in turn. The directory I am using here is a common sounds directory found on most Debian/Ubuntu systems and so should produce some sort of result.&lt;/p&gt;

&lt;div class="geshifilter"&gt;&lt;table class="xml geshifilter-xml" style="font-family:monospace;"&gt;&lt;tbody&gt;&lt;tr class="li1"&gt;&lt;td style="width:1px;text-align:right;margin:0;padding:0 2px;vertical-align:top;"&gt;&lt;pre style="font-family: monospace; font-weight: normal; font-style: normal"&gt;1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
&lt;/pre&gt;&lt;/td&gt;&lt;td style="font-family: monospace; font-weight: normal; font-style: normal"&gt;&lt;pre style="font-family: monospace; font-weight: normal; font-style: normal"&gt;&lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;project&lt;/span&gt; &lt;span style="color: #000066;"&gt;name&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"foreachtest"&lt;/span&gt; &lt;span style="color: #000066;"&gt;default&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"foreachtest"&lt;/span&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt; 
 
 &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;target&lt;/span&gt; &lt;span style="color: #000066;"&gt;name&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"foreachtest"&lt;/span&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt; 
   &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;fileset&lt;/span&gt; &lt;span style="color: #000066;"&gt;dir&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"/usr/share/sounds/alsa/"&lt;/span&gt; &lt;span style="color: #000066;"&gt;id&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"thefiles"&lt;/span&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
     &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;include&lt;/span&gt; &lt;span style="color: #000066;"&gt;name&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"**"&lt;/span&gt; &lt;span style="color: #000000; font-weight: bold;"&gt;/&amp;gt;&lt;/span&gt;&lt;/span&gt;
   &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;/fileset&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
 
   &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;foreach&lt;/span&gt; &lt;span style="color: #000066;"&gt;param&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"filename"&lt;/span&gt; &lt;span style="color: #000066;"&gt;target&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"foreachfile"&lt;/span&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
     &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;fileset&lt;/span&gt; &lt;span style="color: #000066;"&gt;refid&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"thefiles"&lt;/span&gt; &lt;span style="color: #000000; font-weight: bold;"&gt;/&amp;gt;&lt;/span&gt;&lt;/span&gt;
   &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;/foreach&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
 &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;/target&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
 
 &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;target&lt;/span&gt; &lt;span style="color: #000066;"&gt;name&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"foreachfile"&lt;/span&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
   &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;echo&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;${filename}&lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;/echo&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
 &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;/target&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;/project&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The idea here is that you would do more than just print out the name of the file. You could change the permissions of the file or perhaps something more involved. The benefits of using this approach mean that you have a target that does a single action on a file that can be used anywhere in your project, not just as the target of the foreach loop.&lt;/p&gt;

&lt;h3&gt;Conditions&lt;/h3&gt;

&lt;p&gt;Conditions are nested elements that are used in &lt;em&gt;condition&lt;/em&gt; and &lt;em&gt;if&lt;/em&gt; tasks to evaluate expressions and need to be understood before those elements can be used. Essentially, a conditional will evaluate something and return true or false. They can be split into &lt;em&gt;comparators&lt;/em&gt; that compare two values and &lt;em&gt;modifiers&lt;/em&gt; that change the output of a comparison in some way.&lt;/p&gt;

&lt;p&gt;None of the comparators work on their own. Instead they are used in the &lt;em&gt;condition&lt;/em&gt; and &lt;em&gt;if&lt;/em&gt; tasks in order to drive those conditional statements. It is essential to understand these elements before looking at the condition and if elements.&lt;/p&gt;

&lt;h4&gt;Comparators&lt;/h4&gt;

&lt;p&gt;This consists of a series of comparison operators that compare one or more values and return a boolean result. The tables in this section were taken directly from the &lt;a href="http://www.phing.info/docs/guide/stable/chapters/ProjectComponents.html#Conditions" title="Phing conditions"&gt;official Phing support documentation on conditions&lt;/a&gt;.&lt;/p&gt;

&lt;h5&gt;equals&lt;/h5&gt;

&lt;p&gt;This is the basic comparator and essentially compares two strings (provided by the &lt;em&gt;arg1&lt;/em&gt; and &lt;em&gt;arg2&lt;/em&gt; parameters) to one another. If the two strings are identical then the element returns true. The &lt;em&gt;casesensitive&lt;/em&gt; parameter can be used to force the comparison to be case or non case sensitive. The &lt;em&gt;trim&lt;/em&gt; parameter can also be used to trim whitespace from the arguments before they are compared. The two arguments can also accept properties as arguments. All of the following examples will return true.&lt;/p&gt;

&lt;div class="geshifilter"&gt;&lt;table class="xml geshifilter-xml" style="font-family:monospace;"&gt;&lt;tbody&gt;&lt;tr class="li1"&gt;&lt;td style="width:1px;text-align:right;margin:0;padding:0 2px;vertical-align:top;"&gt;&lt;pre style="font-family: monospace; font-weight: normal; font-style: normal"&gt;1
2
3
4
5
6
&lt;/pre&gt;&lt;/td&gt;&lt;td style="font-family: monospace; font-weight: normal; font-style: normal"&gt;&lt;pre style="font-family: monospace; font-weight: normal; font-style: normal"&gt;&lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;equals&lt;/span&gt; &lt;span style="color: #000066;"&gt;arg1&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"text"&lt;/span&gt; &lt;span style="color: #000066;"&gt;arg2&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"text"&lt;/span&gt; &lt;span style="color: #000000; font-weight: bold;"&gt;/&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;equals&lt;/span&gt; &lt;span style="color: #000066;"&gt;arg1&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"TEXT"&lt;/span&gt; &lt;span style="color: #000066;"&gt;arg2&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"text"&lt;/span&gt; &lt;span style="color: #000066;"&gt;casesensitive&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"false"&lt;/span&gt; &lt;span style="color: #000000; font-weight: bold;"&gt;/&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;equals&lt;/span&gt; &lt;span style="color: #000066;"&gt;arg1&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"TEXT"&lt;/span&gt; &lt;span style="color: #000066;"&gt;arg2&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;" text "&lt;/span&gt; &lt;span style="color: #000066;"&gt;trim&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"true"&lt;/span&gt; &lt;span style="color: #000066;"&gt;casesensitive&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"false"&lt;/span&gt; &lt;span style="color: #000000; font-weight: bold;"&gt;/&amp;gt;&lt;/span&gt;&lt;/span&gt;
 
&lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;property&lt;/span&gt; &lt;span style="color: #000066;"&gt;name&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"property1"&lt;/span&gt; &lt;span style="color: #000066;"&gt;value&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"TExt"&lt;/span&gt; &lt;span style="color: #000000; font-weight: bold;"&gt;/&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;equals&lt;/span&gt; &lt;span style="color: #000066;"&gt;arg1&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"${property1}"&lt;/span&gt; &lt;span style="color: #000066;"&gt;arg2&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"text "&lt;/span&gt; &lt;span style="color: #000066;"&gt;trim&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"true"&lt;/span&gt; &lt;span style="color: #000066;"&gt;casesensitive&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"false"&lt;/span&gt; &lt;span style="color: #000000; font-weight: bold;"&gt;/&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;

&lt;table class="table table-condensed table-bordered"&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Attribute&lt;/th&gt;&lt;th&gt;Description&lt;/th&gt;&lt;th&gt;Required&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tr&gt;&lt;td&gt;arg1&lt;/td&gt;&lt;td&gt;First string to test.&lt;/td&gt;&lt;td&gt;Yes&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;arg2&lt;/td&gt;&lt;td&gt;Second string to test.&lt;/td&gt;&lt;td&gt;Yes&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;casesensitive&lt;/td&gt;&lt;td&gt;Perform a case sensitive comparison. Default is true.&lt;/td&gt;&lt;td&gt;No&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;trim&lt;/td&gt;&lt;td&gt;Trim whitespace from arguments before comparing them. Default is false.&lt;/td&gt;&lt;td&gt;No&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;h5&gt;isset&lt;/h5&gt;

&lt;p&gt;The &lt;em&gt;isset&lt;/em&gt; element returns true if a property has been set within the project scope. This is useful for checking to see if certain properties have been assigned before trying to use them. If the property hasn't been set then a property prompt or even a failure event can be used to act accordingly.&lt;/p&gt;

&lt;div class="geshifilter"&gt;&lt;table class="xml geshifilter-xml" style="font-family:monospace;"&gt;&lt;tbody&gt;&lt;tr class="li1"&gt;&lt;td style="width:1px;text-align:right;margin:0;padding:0 2px;vertical-align:top;"&gt;&lt;pre style="font-family: monospace; font-weight: normal; font-style: normal"&gt;1
2
&lt;/pre&gt;&lt;/td&gt;&lt;td style="font-family: monospace; font-weight: normal; font-style: normal"&gt;&lt;pre style="font-family: monospace; font-weight: normal; font-style: normal"&gt;&lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;property&lt;/span&gt; &lt;span style="color: #000066;"&gt;name&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"property1"&lt;/span&gt; &lt;span style="color: #000066;"&gt;value&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"text"&lt;/span&gt; &lt;span style="color: #000000; font-weight: bold;"&gt;/&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;isset&lt;/span&gt; &lt;span style="color: #000066;"&gt;property&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"property1"&lt;/span&gt; &lt;span style="color: #000000; font-weight: bold;"&gt;/&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;

&lt;table class="table table-condensed table-bordered"&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Attribute&lt;/th&gt;&lt;th&gt;Description&lt;/th&gt;&lt;th&gt;Required&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tr&gt;&lt;td&gt;property&lt;/td&gt;&lt;td&gt;The name of the property to test.&lt;/td&gt;&lt;td&gt;Yes&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;h5&gt;contains&lt;/h5&gt;

&lt;p&gt;This element tests to see if one string contains another string. The &lt;em&gt;string&lt;/em&gt; attribute is the string to search in and the &lt;em&gt;substring&lt;/em&gt; attribute is the string to search for. If the string attribute contains substring then the condition will return true. The &lt;em&gt;casesensitive&lt;/em&gt; attribute is a boolean value that can be used to turn on or off case sensitivity, the default being on. All of the following examples return true.&lt;/p&gt;

&lt;div class="geshifilter"&gt;&lt;table class="xml geshifilter-xml" style="font-family:monospace;"&gt;&lt;tbody&gt;&lt;tr class="li1"&gt;&lt;td style="width:1px;text-align:right;margin:0;padding:0 2px;vertical-align:top;"&gt;&lt;pre style="font-family: monospace; font-weight: normal; font-style: normal"&gt;1
2
3
4
5
&lt;/pre&gt;&lt;/td&gt;&lt;td style="font-family: monospace; font-weight: normal; font-style: normal"&gt;&lt;pre style="font-family: monospace; font-weight: normal; font-style: normal"&gt;&lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;contains&lt;/span&gt; &lt;span style="color: #000066;"&gt;string&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"some string"&lt;/span&gt; &lt;span style="color: #000066;"&gt;substring&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"me str"&lt;/span&gt; &lt;span style="color: #000000; font-weight: bold;"&gt;/&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;contains&lt;/span&gt; &lt;span style="color: #000066;"&gt;string&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"SOME string"&lt;/span&gt; &lt;span style="color: #000066;"&gt;substring&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"me str"&lt;/span&gt; &lt;span style="color: #000066;"&gt;casesensitive&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"false"&lt;/span&gt; &lt;span style="color: #000000; font-weight: bold;"&gt;/&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;contains&lt;/span&gt; &lt;span style="color: #000066;"&gt;string&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"abcdef"&lt;/span&gt; &lt;span style="color: #000066;"&gt;substring&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"a"&lt;/span&gt; &lt;span style="color: #000000; font-weight: bold;"&gt;/&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;contains&lt;/span&gt; &lt;span style="color: #000066;"&gt;string&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"aBcDe"&lt;/span&gt; &lt;span style="color: #000066;"&gt;substring&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"aB"&lt;/span&gt; &lt;span style="color: #000000; font-weight: bold;"&gt;/&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;contains&lt;/span&gt; &lt;span style="color: #000066;"&gt;string&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"aBcDe"&lt;/span&gt; &lt;span style="color: #000066;"&gt;substring&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"ab"&lt;/span&gt; &lt;span style="color: #000066;"&gt;casesensitive&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"false"&lt;/span&gt; &lt;span style="color: #000000; font-weight: bold;"&gt;/&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;

&lt;table class="table table-condensed table-bordered"&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Attribute&lt;/th&gt;&lt;th&gt;Description&lt;/th&gt;&lt;th&gt;Required&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tr&gt;&lt;td&gt;string&lt;/td&gt;&lt;td&gt;The string to search in.&lt;/td&gt;&lt;td&gt;Yes&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;substring&lt;/td&gt;&lt;td&gt;The string to search for.&lt;/td&gt;&lt;td&gt;Yes&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;casesensitive&lt;/td&gt;&lt;td&gt;Perform a case sensitive comparision. Default	is true.&lt;/td&gt;&lt;td&gt;No&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;h5&gt;istrue&lt;/h5&gt;

&lt;p&gt;This tests to see if a string evaluates to true. This uses standard PHP boolean string casting so any non-empty string will equate to true, whereas values like 'false', 0 or empty strings equate to false. All of the following examples will return true.&lt;/p&gt;

&lt;div class="geshifilter"&gt;&lt;table class="xml geshifilter-xml" style="font-family:monospace;"&gt;&lt;tbody&gt;&lt;tr class="li1"&gt;&lt;td style="width:1px;text-align:right;margin:0;padding:0 2px;vertical-align:top;"&gt;&lt;pre style="font-family: monospace; font-weight: normal; font-style: normal"&gt;1
2
3
4
&lt;/pre&gt;&lt;/td&gt;&lt;td style="font-family: monospace; font-weight: normal; font-style: normal"&gt;&lt;pre style="font-family: monospace; font-weight: normal; font-style: normal"&gt;&lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;istrue&lt;/span&gt; &lt;span style="color: #000066;"&gt;value&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"1"&lt;/span&gt; &lt;span style="color: #000000; font-weight: bold;"&gt;/&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;istrue&lt;/span&gt; &lt;span style="color: #000066;"&gt;value&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"true"&lt;/span&gt; &lt;span style="color: #000000; font-weight: bold;"&gt;/&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;istrue&lt;/span&gt; &lt;span style="color: #000066;"&gt;value&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"yes"&lt;/span&gt; &lt;span style="color: #000000; font-weight: bold;"&gt;/&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;istrue&lt;/span&gt; &lt;span style="color: #000066;"&gt;value&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;" "&lt;/span&gt; &lt;span style="color: #000000; font-weight: bold;"&gt;/&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;

&lt;table class="table table-condensed table-bordered"&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Attribute&lt;/th&gt;&lt;th&gt;Description&lt;/th&gt;&lt;th&gt;Required&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tr&gt;&lt;td&gt;value&lt;/td&gt;&lt;td&gt;value to test&lt;/td&gt;&lt;td&gt;Yes&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;h5&gt;isfalse&lt;/h5&gt;
&lt;p&gt;This is essentially the negative of istrue, but will return true if the value is false. All of the following examples will return true.&lt;/p&gt;

&lt;div class="geshifilter"&gt;&lt;table class="xml geshifilter-xml" style="font-family:monospace;"&gt;&lt;tbody&gt;&lt;tr class="li1"&gt;&lt;td style="width:1px;text-align:right;margin:0;padding:0 2px;vertical-align:top;"&gt;&lt;pre style="font-family: monospace; font-weight: normal; font-style: normal"&gt;1
2
3
&lt;/pre&gt;&lt;/td&gt;&lt;td style="font-family: monospace; font-weight: normal; font-style: normal"&gt;&lt;pre style="font-family: monospace; font-weight: normal; font-style: normal"&gt;&lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;isfalse&lt;/span&gt; &lt;span style="color: #000066;"&gt;value&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"0"&lt;/span&gt; &lt;span style="color: #000000; font-weight: bold;"&gt;/&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;isfalse&lt;/span&gt; &lt;span style="color: #000066;"&gt;value&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"false"&lt;/span&gt; &lt;span style="color: #000000; font-weight: bold;"&gt;/&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;isfalse&lt;/span&gt; &lt;span style="color: #000066;"&gt;value&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;""&lt;/span&gt; &lt;span style="color: #000000; font-weight: bold;"&gt;/&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;

&lt;table class="table table-condensed table-bordered"&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Attribute&lt;/th&gt;&lt;th&gt;Description&lt;/th&gt;&lt;th&gt;Required&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tr&gt;&lt;td&gt;value&lt;/td&gt;&lt;td&gt;value to test&lt;/td&gt;&lt;td&gt;Yes&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;h5&gt;os&lt;/h5&gt;

&lt;p&gt;This tests to see if an operating system is of a given type. The element takes one attribute of &lt;em&gt;family&lt;/em&gt; which can have one of three values. For Microsoft Windows based systems this is 'windows', for Apple Macintosh systems this is 'mac' and for Unix-like operating systems this is 'unix'. For example, if the family attribute is 'unix' and the script was being run on Ubuntu then this would return true. This is a pretty good way of running different functionality on different platforms and the options for each system are pretty simple.&lt;/p&gt;

&lt;div class="geshifilter"&gt;&lt;table class="xml geshifilter-xml" style="font-family:monospace;"&gt;&lt;tbody&gt;&lt;tr class="li1"&gt;&lt;td style="width:1px;text-align:right;margin:0;padding:0 2px;vertical-align:top;"&gt;&lt;pre style="font-family: monospace; font-weight: normal; font-style: normal"&gt;1
&lt;/pre&gt;&lt;/td&gt;&lt;td style="font-family: monospace; font-weight: normal; font-style: normal"&gt;&lt;pre style="font-family: monospace; font-weight: normal; font-style: normal"&gt;&lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;os&lt;/span&gt; &lt;span style="color: #000066;"&gt;family&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"unix"&lt;/span&gt; &lt;span style="color: #000000; font-weight: bold;"&gt;/&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;

&lt;table class="table table-condensed table-bordered"&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Attribute&lt;/th&gt;&lt;th&gt;Description&lt;/th&gt;&lt;th&gt;Required&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tr&gt;&lt;td&gt;family&lt;/td&gt;&lt;td&gt;The name of the operating system family to expect.&lt;/td&gt;&lt;td&gt;Yes&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;h5&gt;referenceexists&lt;/h5&gt;

&lt;p&gt;The &lt;em&gt;referenceexists&lt;/em&gt; condition tests to see if a specified reference exists. A reference is a Phing type that references files, which includes filset, filelist, filterchains, chains, and file mappers.&lt;/p&gt;

&lt;p&gt;The following will return true because the filset reference 'someid' exists.&lt;/p&gt;

&lt;div class="geshifilter"&gt;&lt;table class="xml geshifilter-xml" style="font-family:monospace;"&gt;&lt;tbody&gt;&lt;tr class="li1"&gt;&lt;td style="width:1px;text-align:right;margin:0;padding:0 2px;vertical-align:top;"&gt;&lt;pre style="font-family: monospace; font-weight: normal; font-style: normal"&gt;1
2
3
4
5
&lt;/pre&gt;&lt;/td&gt;&lt;td style="font-family: monospace; font-weight: normal; font-style: normal"&gt;&lt;pre style="font-family: monospace; font-weight: normal; font-style: normal"&gt;&lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;fileset&lt;/span&gt; &lt;span style="color: #000066;"&gt;id&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"someid"&lt;/span&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
  &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;include&lt;/span&gt; &lt;span style="color: #000066;"&gt;name&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"*.php"&lt;/span&gt; &lt;span style="color: #000000; font-weight: bold;"&gt;/&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;/fileset&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
 
&lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;referenceexists&lt;/span&gt; &lt;span style="color: #000066;"&gt;ref&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"someid"&lt;/span&gt; &lt;span style="color: #000000; font-weight: bold;"&gt;/&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;

&lt;table class="table table-condensed table-bordered"&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Attribute&lt;/th&gt;&lt;th&gt;Description&lt;/th&gt;&lt;th&gt;Required&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tr&gt;&lt;td&gt;ref&lt;/td&gt;&lt;td&gt;reference to test for&lt;/td&gt;&lt;td&gt;Yes&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;h5&gt;available&lt;/h5&gt;

&lt;p&gt;The &lt;em&gt;available&lt;/em&gt; comparison is used to test to see if a file or directory exists. The &lt;em&gt;file&lt;/em&gt; attribute details the file or directory name to be looked for. This can be swapped for the &lt;em&gt;resource&lt;/em&gt; attribute to look for a specific path or the &lt;em&gt;extension&lt;/em&gt; attribute to find a specific extension. The &lt;em&gt;type&lt;/em&gt; attribute is used to differentiate between files and directories and the &lt;em&gt;filepath&lt;/em&gt; attribute dictates which path should be used to look for the file.&lt;/p&gt;

&lt;p&gt;This comparator works in the same way as the available task with the exception that the property and value attributes do nothing and will be ignored. It is a good way of double checking that things have worked correctly during the build. If the primary function of the build file is to create a compressed file then a check should be done to make sure the compressed file exists.&lt;/p&gt;

&lt;p&gt;Using the following should always return true as this will find the build file.&lt;/p&gt;
&lt;span class="geshifilter"&gt;&lt;code class="xml geshifilter-xml"&gt;&lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;available&lt;/span&gt; &lt;span style="color: #000066;"&gt;extension&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"xml"&lt;/span&gt; &lt;span style="color: #000000; font-weight: bold;"&gt;/&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/span&gt;

&lt;p&gt;The following will detect the /home directory on a Unix based system.&lt;/p&gt;
&lt;span class="geshifilter"&gt;&lt;code class="xml geshifilter-xml"&gt;&lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;available&lt;/span&gt; &lt;span style="color: #000066;"&gt;file&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"/home"&lt;/span&gt; &lt;span style="color: #000066;"&gt;type&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"dir"&lt;/span&gt; &lt;span style="color: #000000; font-weight: bold;"&gt;/&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/span&gt;

&lt;p&gt;The following will return true if the the file index.php is within the directory /some/path.&lt;/p&gt;
&lt;span class="geshifilter"&gt;&lt;code class="xml geshifilter-xml"&gt;&lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;available&lt;/span&gt; &lt;span style="color: #000066;"&gt;file&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"index.php"&lt;/span&gt; &lt;span style="color: #000066;"&gt;type&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"file"&lt;/span&gt; &lt;span style="color: #000066;"&gt;filepath&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"/some/path/"&lt;/span&gt; &lt;span style="color: #000000; font-weight: bold;"&gt;/&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/span&gt;

&lt;table class="table table-condensed table-bordered"&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Name&lt;/th&gt;&lt;th&gt;Type&lt;/th&gt;&lt;th&gt;Description&lt;/th&gt;&lt;th&gt;Default&lt;/th&gt;&lt;th&gt;Required&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;file&lt;/td&gt;&lt;td&gt;String&lt;/td&gt;&lt;td&gt;File/directory to check existence.&lt;/td&gt;&lt;td&gt;n/a&lt;/td&gt;&lt;td&gt;Yes (or &lt;em&gt;resource&lt;/em&gt; or &lt;em&gt;extension&lt;/em&gt;)&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;resource&lt;/td&gt;&lt;td&gt;String&lt;/td&gt;&lt;td&gt;Path of the resource to look for.&lt;/td&gt;&lt;td&gt;n/a&lt;/td&gt;&lt;td&gt;Yes (or &lt;em&gt;file&lt;/em&gt; or &lt;em&gt;extension&lt;/em&gt;)&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;extension&lt;/td&gt;&lt;td&gt;String&lt;/td&gt;&lt;td&gt;Name of the extension to look for.&lt;/td&gt;&lt;td&gt;n/a&lt;/td&gt;&lt;td&gt;Yes (or &lt;em&gt;file&lt;/em&gt; or &lt;em&gt;resource&lt;/em&gt;)&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;type&lt;/td&gt;&lt;td&gt;String (file|dir)&lt;/td&gt;&lt;td&gt;Determines if &lt;em&gt;AvailableTask&lt;/em&gt; should look for a file or a directory at the position set by &lt;em&gt;file&lt;/em&gt;. If empty, it checks for either file or directory.&lt;/td&gt;&lt;td&gt;n/a&lt;/td&gt;&lt;td&gt;No&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;filepath&lt;/td&gt;&lt;td&gt;String&lt;/td&gt;&lt;td&gt;The path to use when looking up &lt;em&gt;file&lt;/em&gt;.&lt;/td&gt;&lt;td&gt;n/a&lt;/td&gt;&lt;td&gt;No&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;followSymlinks&lt;/td&gt;&lt;td&gt;Boolean&lt;/td&gt;&lt;td&gt;Whether to dereference symbolic links when looking up &lt;em&gt;file&lt;/em&gt;.&lt;/td&gt;&lt;td&gt;false&lt;/td&gt;&lt;td&gt;No&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;h4&gt;Modifiers&lt;/h4&gt;

&lt;p&gt;Modifiers are used to modify the result of a comparison in some way. They do not compare things themsevles but they can be used to group together multiple comparisons or to negate a comparison. None of the comparison modifiers have any arguments.&lt;/p&gt;

&lt;h5&gt;not&lt;/h5&gt;

&lt;p&gt;This is the simplest form of modifier and will just negate the result of the comparison. This is a good way of testing to see if a property is set and acting accordingly using the &lt;em&gt;isset&lt;/em&gt; comparison. The &lt;em&gt;not&lt;/em&gt; comparison modifier cannot accept more than one condition, but it is possible to nest multiple comparisons within another comparison modifier and negate that. The following example will negate the result of the istrue comparison of false, which essentially returns true.&lt;/p&gt;

&lt;div class="geshifilter"&gt;&lt;table class="xml geshifilter-xml" style="font-family:monospace;"&gt;&lt;tbody&gt;&lt;tr class="li1"&gt;&lt;td style="width:1px;text-align:right;margin:0;padding:0 2px;vertical-align:top;"&gt;&lt;pre style="font-family: monospace; font-weight: normal; font-style: normal"&gt;1
2
3
&lt;/pre&gt;&lt;/td&gt;&lt;td style="font-family: monospace; font-weight: normal; font-style: normal"&gt;&lt;pre style="font-family: monospace; font-weight: normal; font-style: normal"&gt;&lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;not&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
  &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;istrue&lt;/span&gt; &lt;span style="color: #000066;"&gt;value&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"false"&lt;/span&gt; &lt;span style="color: #000000; font-weight: bold;"&gt;/&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;/not&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The following is an example of nesting multiple comparisons into an &lt;em&gt;and&lt;/em&gt; comparison modifier and then negating the result using the &lt;em&gt;not&lt;e&gt; modifier. In this case the result of the and comparison is false, but this negates and so the overall result is true.&lt;/e&gt;&lt;/em&gt;&lt;/p&gt;

&lt;div class="geshifilter"&gt;&lt;table class="xml geshifilter-xml" style="font-family:monospace;"&gt;&lt;tbody&gt;&lt;tr class="li1"&gt;&lt;td style="width:1px;text-align:right;margin:0;padding:0 2px;vertical-align:top;"&gt;&lt;pre style="font-family: monospace; font-weight: normal; font-style: normal"&gt;1
2
3
4
5
6
&lt;/pre&gt;&lt;/td&gt;&lt;td style="font-family: monospace; font-weight: normal; font-style: normal"&gt;&lt;pre style="font-family: monospace; font-weight: normal; font-style: normal"&gt;&lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;not&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
  &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;and&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
    &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;istrue&lt;/span&gt; &lt;span style="color: #000066;"&gt;value&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"false"&lt;/span&gt; &lt;span style="color: #000000; font-weight: bold;"&gt;/&amp;gt;&lt;/span&gt;&lt;/span&gt;
    &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;istrue&lt;/span&gt; &lt;span style="color: #000066;"&gt;value&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"0"&lt;/span&gt; &lt;span style="color: #000000; font-weight: bold;"&gt;/&amp;gt;&lt;/span&gt;&lt;/span&gt;
  &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;/and&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;                        
&lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;/not&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;

&lt;h5&gt;and&lt;/h5&gt;

&lt;p&gt;The &lt;em&gt;and&lt;/em&gt; comparison modifier compares the result of multiple nested comparisons. The result is true if &lt;em&gt;all&lt;/em&gt; of the inner comparisons are equal to true. The conditions will be evaluated in the order they appear in the build file. As soon as a condition is evaluated to false then no other conditions are evaluated in the rest of the and element. The following is an example of an and element comparing multiple nested comparison elements to return true.&lt;/p&gt;

&lt;div class="geshifilter"&gt;&lt;table class="xml geshifilter-xml" style="font-family:monospace;"&gt;&lt;tbody&gt;&lt;tr class="li1"&gt;&lt;td style="width:1px;text-align:right;margin:0;padding:0 2px;vertical-align:top;"&gt;&lt;pre style="font-family: monospace; font-weight: normal; font-style: normal"&gt;1
2
3
4
5
6
7
8
9
&lt;/pre&gt;&lt;/td&gt;&lt;td style="font-family: monospace; font-weight: normal; font-style: normal"&gt;&lt;pre style="font-family: monospace; font-weight: normal; font-style: normal"&gt;&lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;property&lt;/span&gt; &lt;span style="color: #000066;"&gt;name&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"property1"&lt;/span&gt; &lt;span style="color: #000066;"&gt;value&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"text"&lt;/span&gt; &lt;span style="color: #000000; font-weight: bold;"&gt;/&amp;gt;&lt;/span&gt;&lt;/span&gt;
 
&lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;and&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
  &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;equals&lt;/span&gt; &lt;span style="color: #000066;"&gt;arg1&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"text"&lt;/span&gt; &lt;span style="color: #000066;"&gt;arg2&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"text"&lt;/span&gt; &lt;span style="color: #000000; font-weight: bold;"&gt;/&amp;gt;&lt;/span&gt;&lt;/span&gt;
  &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;isset&lt;/span&gt; &lt;span style="color: #000066;"&gt;property&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"property1"&lt;/span&gt; &lt;span style="color: #000000; font-weight: bold;"&gt;/&amp;gt;&lt;/span&gt;&lt;/span&gt;
  &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;contains&lt;/span&gt; &lt;span style="color: #000066;"&gt;string&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"some string"&lt;/span&gt; &lt;span style="color: #000066;"&gt;substring&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"me str"&lt;/span&gt; &lt;span style="color: #000000; font-weight: bold;"&gt;/&amp;gt;&lt;/span&gt;&lt;/span&gt;
  &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;istrue&lt;/span&gt; &lt;span style="color: #000066;"&gt;value&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"1"&lt;/span&gt; &lt;span style="color: #000000; font-weight: bold;"&gt;/&amp;gt;&lt;/span&gt;&lt;/span&gt;
  &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;isfalse&lt;/span&gt; &lt;span style="color: #000066;"&gt;value&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"0"&lt;/span&gt; &lt;span style="color: #000000; font-weight: bold;"&gt;/&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;/and&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;

&lt;h5&gt;or&lt;/h5&gt;

&lt;p&gt;This comparison modifier will evaluate multiple nested comparisons and return true if at least one of those conditions is true. The comparisons will be evaluated in the order they are found in the build file. When a true value is found the element will return true immediately and no other comparisons will be evaluated. The following example will evaluate the nested conditions and return true because at least one of them returns a true value.&lt;/p&gt;

&lt;div class="geshifilter"&gt;&lt;table class="xml geshifilter-xml" style="font-family:monospace;"&gt;&lt;tbody&gt;&lt;tr class="li1"&gt;&lt;td style="width:1px;text-align:right;margin:0;padding:0 2px;vertical-align:top;"&gt;&lt;pre style="font-family: monospace; font-weight: normal; font-style: normal"&gt;1
2
3
4
&lt;/pre&gt;&lt;/td&gt;&lt;td style="font-family: monospace; font-weight: normal; font-style: normal"&gt;&lt;pre style="font-family: monospace; font-weight: normal; font-style: normal"&gt;&lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;or&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
  &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;equals&lt;/span&gt; &lt;span style="color: #000066;"&gt;arg1&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"text"&lt;/span&gt; &lt;span style="color: #000066;"&gt;arg2&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"TEXT"&lt;/span&gt; &lt;span style="color: #000000; font-weight: bold;"&gt;/&amp;gt;&lt;/span&gt;&lt;/span&gt;
  &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;contains&lt;/span&gt; &lt;span style="color: #000066;"&gt;string&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"some string"&lt;/span&gt; &lt;span style="color: #000066;"&gt;substring&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"me str"&lt;/span&gt; &lt;span style="color: #000000; font-weight: bold;"&gt;/&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;/or&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;

&lt;h3&gt;Condition Task&lt;/h3&gt; 

&lt;p&gt;The &lt;em&gt;condition&lt;/em&gt; task is used to set a property if a condition is true. By default this property is set to be a boolean (i.e. the result of the comparison) but this can be changed by using the &lt;em&gt;value&lt;/em&gt; attribute. The condition task must have exactly one nested comparison, although comparison modifiers (such as &lt;em&gt;and&lt;/em&gt;) can be used when multiple comparisons need to be done. If the condition is not met then the property is not set. Here are a few examples of the condition task in action.&lt;/p&gt;

&lt;p&gt;The following example sets the property 'textisthesame' to be true, based on the output of the &lt;em&gt;equals&lt;/em&gt; comparison.&lt;/p&gt;

&lt;div class="geshifilter"&gt;&lt;table class="xml geshifilter-xml" style="font-family:monospace;"&gt;&lt;tbody&gt;&lt;tr class="li1"&gt;&lt;td style="width:1px;text-align:right;margin:0;padding:0 2px;vertical-align:top;"&gt;&lt;pre style="font-family: monospace; font-weight: normal; font-style: normal"&gt;1
2
3
&lt;/pre&gt;&lt;/td&gt;&lt;td style="font-family: monospace; font-weight: normal; font-style: normal"&gt;&lt;pre style="font-family: monospace; font-weight: normal; font-style: normal"&gt;&lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;condition&lt;/span&gt; &lt;span style="color: #000066;"&gt;property&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"textisthesame"&lt;/span&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
  &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;equals&lt;/span&gt; &lt;span style="color: #000066;"&gt;arg1&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"text"&lt;/span&gt; &lt;span style="color: #000066;"&gt;arg2&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"text"&lt;/span&gt; &lt;span style="color: #000000; font-weight: bold;"&gt;/&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;/condition&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The following example sets the property 'sametextandmac' to be true, based on the output of both the equals comparison and the build being run on a Mac computer.&lt;/p&gt;

&lt;div class="geshifilter"&gt;&lt;table class="xml geshifilter-xml" style="font-family:monospace;"&gt;&lt;tbody&gt;&lt;tr class="li1"&gt;&lt;td style="width:1px;text-align:right;margin:0;padding:0 2px;vertical-align:top;"&gt;&lt;pre style="font-family: monospace; font-weight: normal; font-style: normal"&gt;1
2
3
4
5
6
&lt;/pre&gt;&lt;/td&gt;&lt;td style="font-family: monospace; font-weight: normal; font-style: normal"&gt;&lt;pre style="font-family: monospace; font-weight: normal; font-style: normal"&gt;&lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;condition&lt;/span&gt; &lt;span style="color: #000066;"&gt;property&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"sametextandmac"&lt;/span&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
  &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;and&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
    &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;equals&lt;/span&gt; &lt;span style="color: #000066;"&gt;arg1&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"text"&lt;/span&gt; &lt;span style="color: #000066;"&gt;arg2&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"text"&lt;/span&gt; &lt;span style="color: #000000; font-weight: bold;"&gt;/&amp;gt;&lt;/span&gt;&lt;/span&gt;
    &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;os&lt;/span&gt; &lt;span style="color: #000066;"&gt;family&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"mac"&lt;/span&gt; &lt;span style="color: #000000; font-weight: bold;"&gt;/&amp;gt;&lt;/span&gt;&lt;/span&gt;
  &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;/and&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;/condition&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The following example sets the property 'textisthesame' to be the string 'text' due to the value attribute being used in the condition task.&lt;/p&gt;

&lt;div class="geshifilter"&gt;&lt;table class="xml geshifilter-xml" style="font-family:monospace;"&gt;&lt;tbody&gt;&lt;tr class="li1"&gt;&lt;td style="width:1px;text-align:right;margin:0;padding:0 2px;vertical-align:top;"&gt;&lt;pre style="font-family: monospace; font-weight: normal; font-style: normal"&gt;1
2
3
&lt;/pre&gt;&lt;/td&gt;&lt;td style="font-family: monospace; font-weight: normal; font-style: normal"&gt;&lt;pre style="font-family: monospace; font-weight: normal; font-style: normal"&gt;&lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;condition&lt;/span&gt; &lt;span style="color: #000066;"&gt;property&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"textisthesame"&lt;/span&gt; &lt;span style="color: #000066;"&gt;value&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"text"&lt;/span&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
  &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;equals&lt;/span&gt; &lt;span style="color: #000066;"&gt;arg1&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"text"&lt;/span&gt; &lt;span style="color: #000066;"&gt;arg2&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"text"&lt;/span&gt; &lt;span style="color: #000000; font-weight: bold;"&gt;/&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;/condition&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The following example will not set the property because the result of the equals comparison is false. If you try to use this property elsewhere in the build file you will just print the name of the property.&lt;/p&gt;

&lt;div class="geshifilter"&gt;&lt;table class="xml geshifilter-xml" style="font-family:monospace;"&gt;&lt;tbody&gt;&lt;tr class="li1"&gt;&lt;td style="width:1px;text-align:right;margin:0;padding:0 2px;vertical-align:top;"&gt;&lt;pre style="font-family: monospace; font-weight: normal; font-style: normal"&gt;1
2
3
&lt;/pre&gt;&lt;/td&gt;&lt;td style="font-family: monospace; font-weight: normal; font-style: normal"&gt;&lt;pre style="font-family: monospace; font-weight: normal; font-style: normal"&gt;&lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;condition&lt;/span&gt; &lt;span style="color: #000066;"&gt;property&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"textisthesame"&lt;/span&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
  &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;equals&lt;/span&gt; &lt;span style="color: #000066;"&gt;arg1&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"text"&lt;/span&gt; &lt;span style="color: #000066;"&gt;arg2&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"notthesame"&lt;/span&gt; &lt;span style="color: #000000; font-weight: bold;"&gt;/&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;/condition&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;

&lt;table class="table table-condensed table-bordered"&gt;&lt;tr&gt;&lt;th&gt;Name&lt;/th&gt;&lt;th&gt;Type&lt;/th&gt;&lt;th&gt;Description&lt;/th&gt;&lt;th&gt;Default&lt;/th&gt;&lt;th&gt;Required&lt;/th&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;property&lt;/td&gt;&lt;td&gt;String&lt;/td&gt;&lt;td&gt;The name of the property to set.&lt;/td&gt;&lt;td&gt;n/a&lt;/td&gt;&lt;td&gt;Yes&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;value&lt;/td&gt;&lt;td&gt;String&lt;/td&gt;&lt;td&gt;The value to set the property to. Defaults to "true".&lt;/td&gt;&lt;td&gt;true&lt;/td&gt;&lt;td&gt;No&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;h3&gt;If Task&lt;/h3&gt;
&lt;a href="http://www.phing.info/docs/guide/stable/chapters/appendixes/AppendixB-CoreTasks.html#IfTask"&gt;http://www.phing.info/docs/guide/stable/chapters/appendixes/AppendixB-Co...&lt;/a&gt;

&lt;p&gt;The &lt;em&gt;if&lt;/em&gt; task is a bit more complex than the condition task in that it is able to direct the flow of the execution of the build file. It works in much the same way that an if statement is used in normal programming, although it can be a little difficult to understand at first due to the nested XML syntax. The if task has no attributes and on it's own doesn't really do all that much, the real work is done by a comparison and the &lt;em&gt;then&lt;/em&gt;, &lt;em&gt;else&lt;/em&gt;, and &lt;em&gt;elseif&lt;/em&gt; child elements. These child elements direct the flow of the build file depending on the outcome of the if comparison. The &lt;em&gt;then&lt;/em&gt; and &lt;em&gt;else&lt;/em&gt; child elements must be used only once within an &lt;em&gt;if&lt;/em&gt; statement.&lt;/p&gt;

&lt;p&gt;The &lt;em&gt;elseif&lt;/em&gt; element acts like a sub &lt;em&gt;if&lt;/em&gt; task and so must also have it's own comparison along with an &lt;em&gt;then&lt;/em&gt; or &lt;em&gt;elseif&lt;/em&gt; elements. The only exception here is that the &lt;em&gt;elseif&lt;/em&gt; element must not contain an &lt;em&gt;else&lt;/em&gt; element. It is also possible to nest if tasks to create complex comparisons. Here are a few examples of the if statement in action.&lt;/p&gt;

&lt;p&gt;The following if task compares two properties and prints a message if they are the same.&lt;/p&gt;

&lt;div class="geshifilter"&gt;&lt;table class="xml geshifilter-xml" style="font-family:monospace;"&gt;&lt;tbody&gt;&lt;tr class="li1"&gt;&lt;td style="width:1px;text-align:right;margin:0;padding:0 2px;vertical-align:top;"&gt;&lt;pre style="font-family: monospace; font-weight: normal; font-style: normal"&gt;1
2
3
4
5
6
7
8
9
&lt;/pre&gt;&lt;/td&gt;&lt;td style="font-family: monospace; font-weight: normal; font-style: normal"&gt;&lt;pre style="font-family: monospace; font-weight: normal; font-style: normal"&gt;&lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;property&lt;/span&gt; &lt;span style="color: #000066;"&gt;name&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"text1"&lt;/span&gt; &lt;span style="color: #000066;"&gt;value&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"text"&lt;/span&gt; &lt;span style="color: #000000; font-weight: bold;"&gt;/&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;property&lt;/span&gt; &lt;span style="color: #000066;"&gt;name&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"text2"&lt;/span&gt; &lt;span style="color: #000066;"&gt;value&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"text"&lt;/span&gt; &lt;span style="color: #000000; font-weight: bold;"&gt;/&amp;gt;&lt;/span&gt;&lt;/span&gt;
 
&lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;if&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
  &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;equals&lt;/span&gt; &lt;span style="color: #000066;"&gt;arg1&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"${text1}"&lt;/span&gt; &lt;span style="color: #000066;"&gt;arg2&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"${text2}"&lt;/span&gt; &lt;span style="color: #000000; font-weight: bold;"&gt;/&amp;gt;&lt;/span&gt;&lt;/span&gt;
  &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;then&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
    &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;echo&lt;/span&gt; &lt;span style="color: #000066;"&gt;message&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"The two strings are the same."&lt;/span&gt; &lt;span style="color: #000000; font-weight: bold;"&gt;/&amp;gt;&lt;/span&gt;&lt;/span&gt;
  &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;/then&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;/if&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The following is an example of using the and condition modifier in an if task. This just tests to see if two values are false and prints a message if they are.&lt;/p&gt;

&lt;div class="geshifilter"&gt;&lt;table class="xml geshifilter-xml" style="font-family:monospace;"&gt;&lt;tbody&gt;&lt;tr class="li1"&gt;&lt;td style="width:1px;text-align:right;margin:0;padding:0 2px;vertical-align:top;"&gt;&lt;pre style="font-family: monospace; font-weight: normal; font-style: normal"&gt;1
2
3
4
5
6
7
8
9
&lt;/pre&gt;&lt;/td&gt;&lt;td style="font-family: monospace; font-weight: normal; font-style: normal"&gt;&lt;pre style="font-family: monospace; font-weight: normal; font-style: normal"&gt;&lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;if&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
  &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;and&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
    &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;isfalse&lt;/span&gt; &lt;span style="color: #000066;"&gt;value&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"false"&lt;/span&gt; &lt;span style="color: #000000; font-weight: bold;"&gt;/&amp;gt;&lt;/span&gt;&lt;/span&gt;
    &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;isfalse&lt;/span&gt; &lt;span style="color: #000066;"&gt;value&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"0"&lt;/span&gt; &lt;span style="color: #000000; font-weight: bold;"&gt;/&amp;gt;&lt;/span&gt;&lt;/span&gt;
  &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;/and&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
  &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;then&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
    &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;echo&lt;/span&gt; &lt;span style="color: #000066;"&gt;message&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"The two values are false."&lt;/span&gt; &lt;span style="color: #000000; font-weight: bold;"&gt;/&amp;gt;&lt;/span&gt;&lt;/span&gt;
  &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;/then&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;/if&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The following if task will print a message depending on whether or not it is run on Windows.&lt;/p&gt;

&lt;div class="geshifilter"&gt;&lt;table class="xml geshifilter-xml" style="font-family:monospace;"&gt;&lt;tbody&gt;&lt;tr class="li1"&gt;&lt;td style="width:1px;text-align:right;margin:0;padding:0 2px;vertical-align:top;"&gt;&lt;pre style="font-family: monospace; font-weight: normal; font-style: normal"&gt;1
2
3
4
5
6
7
8
9
&lt;/pre&gt;&lt;/td&gt;&lt;td style="font-family: monospace; font-weight: normal; font-style: normal"&gt;&lt;pre style="font-family: monospace; font-weight: normal; font-style: normal"&gt;&lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;if&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
 &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;os&lt;/span&gt; &lt;span style="color: #000066;"&gt;family&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"windows"&lt;/span&gt; &lt;span style="color: #000000; font-weight: bold;"&gt;/&amp;gt;&lt;/span&gt;&lt;/span&gt;
 &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;then&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
   &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;echo&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;You are running this build file on Windows.&lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;/echo&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
 &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;/then&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
 &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;else&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
   &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;echo&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;You are not running this build file on Windows&lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;/echo&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
 &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;/else&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;/if&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The following if task will detect what operating system the build file is being run on and print a message. Notice that the &lt;em&gt;elseif&lt;/em&gt; element here contains it's own comparison and &lt;em&gt;then&lt;/em&gt; child element in order to compare and act on the comparison.&lt;/p&gt;

&lt;div class="geshifilter"&gt;&lt;table class="xml geshifilter-xml" style="font-family:monospace;"&gt;&lt;tbody&gt;&lt;tr class="li1"&gt;&lt;td style="width:1px;text-align:right;margin:0;padding:0 2px;vertical-align:top;"&gt;&lt;pre style="font-family: monospace; font-weight: normal; font-style: normal"&gt;1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
&lt;/pre&gt;&lt;/td&gt;&lt;td style="font-family: monospace; font-weight: normal; font-style: normal"&gt;&lt;pre style="font-family: monospace; font-weight: normal; font-style: normal"&gt;&lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;if&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
  &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;os&lt;/span&gt; &lt;span style="color: #000066;"&gt;family&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"windows"&lt;/span&gt; &lt;span style="color: #000000; font-weight: bold;"&gt;/&amp;gt;&lt;/span&gt;&lt;/span&gt;
  &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;then&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
    &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;echo&lt;/span&gt; &lt;span style="color: #000066;"&gt;message&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"This build file is being run on Windows.'"&lt;/span&gt; &lt;span style="color: #000000; font-weight: bold;"&gt;/&amp;gt;&lt;/span&gt;&lt;/span&gt;
  &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;/then&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
 
  &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;elseif&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
    &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;os&lt;/span&gt; &lt;span style="color: #000066;"&gt;family&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"mac"&lt;/span&gt; &lt;span style="color: #000000; font-weight: bold;"&gt;/&amp;gt;&lt;/span&gt;&lt;/span&gt;
    &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;then&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
      &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;echo&lt;/span&gt; &lt;span style="color: #000066;"&gt;message&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"This build file is being run on OSX.'"&lt;/span&gt; &lt;span style="color: #000000; font-weight: bold;"&gt;/&amp;gt;&lt;/span&gt;&lt;/span&gt;
    &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;/then&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
  &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;/elseif&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
 
  &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;else&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
    &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;echo&lt;/span&gt; &lt;span style="color: #000066;"&gt;message&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"This build file is being run on Linux.'"&lt;/span&gt; &lt;span style="color: #000000; font-weight: bold;"&gt;/&amp;gt;&lt;/span&gt;&lt;/span&gt;
  &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;/else&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;/if&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The following if statement sets a property with a default value if the property doesn't exist. This is a good way of allowing properties to be passed to build files but to also have them available if the property isn't present.&lt;/p&gt;

&lt;div class="geshifilter"&gt;&lt;table class="xml geshifilter-xml" style="font-family:monospace;"&gt;&lt;tbody&gt;&lt;tr class="li1"&gt;&lt;td style="width:1px;text-align:right;margin:0;padding:0 2px;vertical-align:top;"&gt;&lt;pre style="font-family: monospace; font-weight: normal; font-style: normal"&gt;1
2
3
4
5
6
7
8
&lt;/pre&gt;&lt;/td&gt;&lt;td style="font-family: monospace; font-weight: normal; font-style: normal"&gt;&lt;pre style="font-family: monospace; font-weight: normal; font-style: normal"&gt;&lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;if&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
  &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;not&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
    &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;isset&lt;/span&gt; &lt;span style="color: #000066;"&gt;property&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"docs_path"&lt;/span&gt; &lt;span style="color: #000000; font-weight: bold;"&gt;/&amp;gt;&lt;/span&gt;&lt;/span&gt;
  &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;/not&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
  &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;then&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
    &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;resolvepath&lt;/span&gt; &lt;span style="color: #000066;"&gt;propertyName&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"docs_path"&lt;/span&gt; &lt;span style="color: #000066;"&gt;file&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"/some/static/path"&lt;/span&gt; &lt;span style="color: #000000; font-weight: bold;"&gt;/&amp;gt;&lt;/span&gt;&lt;/span&gt;     
  &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;/then&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;/if&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The following is an example taken from a Drupal build file that puts the apc.php file into the correct place for it to be used by the system. It is an example of using an if statement with a then, elseif, and else element.&lt;/p&gt;

&lt;p&gt;On some platforms the acp.php file is kept as a compressed 'gz' file that must be extracted. This file exists then the file is copied, to the destination directory before being extracted. The copied compressed file is then deleted as it is not needed. If the apc.php file is present then it is just copied into the correct place. Finally, the build file will announce if it is unable to find the file on the current system.&lt;/p&gt;

&lt;div class="geshifilter"&gt;&lt;table class="xml geshifilter-xml" style="font-family:monospace;"&gt;&lt;tbody&gt;&lt;tr class="li1"&gt;&lt;td style="width:1px;text-align:right;margin:0;padding:0 2px;vertical-align:top;"&gt;&lt;pre style="font-family: monospace; font-weight: normal; font-style: normal"&gt;1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
&lt;/pre&gt;&lt;/td&gt;&lt;td style="font-family: monospace; font-weight: normal; font-style: normal"&gt;&lt;pre style="font-family: monospace; font-weight: normal; font-style: normal"&gt;&lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;if&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
  &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;available&lt;/span&gt; &lt;span style="color: #000066;"&gt;file&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"/usr/share/doc/php-apc/apc.php.gz"&lt;/span&gt; &lt;span style="color: #000000; font-weight: bold;"&gt;/&amp;gt;&lt;/span&gt;&lt;/span&gt;
  &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;then&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
    &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;echo&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;Compressed apc file found, extracting.&lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;/echo&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
    &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;mkdir&lt;/span&gt; &lt;span style="color: #000066;"&gt;dir&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"/docroot/sites/all/libraries/APC/"&lt;/span&gt; &lt;span style="color: #000000; font-weight: bold;"&gt;/&amp;gt;&lt;/span&gt;&lt;/span&gt;
    &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;copy&lt;/span&gt; &lt;span style="color: #000066;"&gt;file&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"/usr/share/doc/php-apc/apc.php.gz"&lt;/span&gt; &lt;span style="color: #000066;"&gt;tofile&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"/docroot/sites/all/libraries/APC/apc.php.gz"&lt;/span&gt; &lt;span style="color: #000066;"&gt;overwrite&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"true"&lt;/span&gt; &lt;span style="color: #000000; font-weight: bold;"&gt;/&amp;gt;&lt;/span&gt;&lt;/span&gt;
    &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;exec&lt;/span&gt; &lt;span style="color: #000066;"&gt;command&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"gunzip apc.php.gz"&lt;/span&gt; &lt;span style="color: #000066;"&gt;dir&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"/docroot/sites/all/libraries/APC/"&lt;/span&gt; &lt;span style="color: #000000; font-weight: bold;"&gt;/&amp;gt;&lt;/span&gt;&lt;/span&gt;
    &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;copy&lt;/span&gt; &lt;span style="color: #000066;"&gt;todir&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"${make_path}/docroot/sites/all/libraries/APC/"&lt;/span&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
      &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;mapper&lt;/span&gt; &lt;span style="color: #000066;"&gt;type&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"glob"&lt;/span&gt; &lt;span style="color: #000066;"&gt;from&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"apc.php"&lt;/span&gt; &lt;span style="color: #000066;"&gt;to&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"apc.php.inc"&lt;/span&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;/&amp;gt;&lt;/span&gt;&lt;/span&gt;
      &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;fileset&lt;/span&gt; &lt;span style="color: #000066;"&gt;dir&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"/docroot/sites/all/libraries/APC/"&lt;/span&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
        &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;include&lt;/span&gt; &lt;span style="color: #000066;"&gt;name&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"apc.php"&lt;/span&gt; &lt;span style="color: #000000; font-weight: bold;"&gt;/&amp;gt;&lt;/span&gt;&lt;/span&gt;
      &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;/fileset&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
    &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;/copy&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
    &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;delete&lt;/span&gt; &lt;span style="color: #000066;"&gt;file&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"/docroot/sites/all/libraries/APC/apc.php"&lt;/span&gt; &lt;span style="color: #000000; font-weight: bold;"&gt;/&amp;gt;&lt;/span&gt;&lt;/span&gt;
  &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;/then&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
 
  &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;elseif&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
    &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;available&lt;/span&gt; &lt;span style="color: #000066;"&gt;file&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"/usr/share/doc/php-apc/apc.php"&lt;/span&gt; &lt;span style="color: #000000; font-weight: bold;"&gt;/&amp;gt;&lt;/span&gt;&lt;/span&gt;
    &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;then&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
      &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;echo&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;Uncompressed apc file found, copying&lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;/echo&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
      &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;mkdir&lt;/span&gt; &lt;span style="color: #000066;"&gt;dir&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"/docroot/sites/all/libraries/APC/"&lt;/span&gt; &lt;span style="color: #000000; font-weight: bold;"&gt;/&amp;gt;&lt;/span&gt;&lt;/span&gt;
      &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;copy&lt;/span&gt; &lt;span style="color: #000066;"&gt;file&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"/usr/share/doc/php-apc/apc.php"&lt;/span&gt; &lt;span style="color: #000066;"&gt;tofile&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"/docroot/sites/all/libraries/APC/apc.php.inc"&lt;/span&gt; &lt;span style="color: #000066;"&gt;overwrite&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"true"&lt;/span&gt; &lt;span style="color: #000000; font-weight: bold;"&gt;/&amp;gt;&lt;/span&gt;&lt;/span&gt;
    &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;/then&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
  &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;/elseif&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
 
  &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;else&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
    &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;echo&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;APC not installed on this system, skipping APC setup&lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;/echo&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
  &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;/else&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;/if&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The following is a silly example that shows the syntax used when nesting if tasks but it useful to see how nesting works. This example will print 'True' because each if comparison will be true.&lt;/p&gt;

&lt;div class="geshifilter"&gt;&lt;table class="xml geshifilter-xml" style="font-family:monospace;"&gt;&lt;tbody&gt;&lt;tr class="li1"&gt;&lt;td style="width:1px;text-align:right;margin:0;padding:0 2px;vertical-align:top;"&gt;&lt;pre style="font-family: monospace; font-weight: normal; font-style: normal"&gt;1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
&lt;/pre&gt;&lt;/td&gt;&lt;td style="font-family: monospace; font-weight: normal; font-style: normal"&gt;&lt;pre style="font-family: monospace; font-weight: normal; font-style: normal"&gt;&lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;if&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
  &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;istrue&lt;/span&gt; &lt;span style="color: #000066;"&gt;value&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"true"&lt;/span&gt; &lt;span style="color: #000000; font-weight: bold;"&gt;/&amp;gt;&lt;/span&gt;&lt;/span&gt;
  &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;then&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
    &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;if&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
      &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;istrue&lt;/span&gt; &lt;span style="color: #000066;"&gt;value&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"true"&lt;/span&gt; &lt;span style="color: #000000; font-weight: bold;"&gt;/&amp;gt;&lt;/span&gt;&lt;/span&gt;
        &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;then&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
          &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;if&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
            &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;istrue&lt;/span&gt; &lt;span style="color: #000066;"&gt;value&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"true"&lt;/span&gt; &lt;span style="color: #000000; font-weight: bold;"&gt;/&amp;gt;&lt;/span&gt;&lt;/span&gt;
            &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;then&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
              &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;echo&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;True&lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;/echo&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
            &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;/then&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
          &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;/if&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
        &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;/then&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
    &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;/if&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
  &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;/then&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;/if&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="field field-name-field-blogcategory field-type-taxonomy-term-reference field-label-above"&gt;&lt;div class="field-label"&gt;Category:&amp;nbsp;&lt;/div&gt;&lt;div class="field-items"&gt;&lt;div class="field-item even"&gt;&lt;a href="/blog/category/phing"&gt;Phing&lt;/a&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="field field-name-field-blogtags field-type-taxonomy-term-reference field-label-above"&gt;&lt;div class="field-label"&gt;Tags:&amp;nbsp;&lt;/div&gt;&lt;div class="field-items"&gt;&lt;div class="field-item even"&gt;&lt;a href="/category/tags/phing"&gt;Phing&lt;/a&gt;&lt;/div&gt;&lt;div class="field-item odd"&gt;&lt;a href="/category/tags/if"&gt;if&lt;/a&gt;&lt;/div&gt;&lt;div class="field-item even"&gt;&lt;a href="/category/tags/comparison"&gt;comparison&lt;/a&gt;&lt;/div&gt;&lt;div class="field-item odd"&gt;&lt;a href="/category/tags/foreach"&gt;foreach&lt;/a&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/hashbangcode/~4/ZognAsP0CoE" height="1" width="1"/&gt;</description>
 <pubDate>Sun, 02 Jun 2013 10:52:15 +0000</pubDate>
 <dc:creator>philipnorton42</dc:creator>
 <guid isPermaLink="false">714 at http://www.hashbangcode.com</guid>
 <comments>http://www.hashbangcode.com/blog/control-structures-phing-714.html#comments</comments>
<feedburner:origLink>http://www.hashbangcode.com/blog/control-structures-phing-714.html</feedburner:origLink></item>
<item>
 <title>Monitoring Performance With Munin</title>
 <link>http://feedproxy.google.com/~r/hashbangcode/~3/0FXB4lMQOXM/monitoring-performance-munin-713.html</link>
 <description>&lt;div class="field field-name-body field-type-text-with-summary field-label-hidden"&gt;&lt;div class="field-items"&gt;&lt;div class="field-item even"&gt;&lt;p&gt;I have been searching for a good server monitoring solution for a while so that I can keep an eye on some of the servers that I run. Tools like &lt;a href="http://oss.oetiker.ch/smokeping/" title="Smokeping"&gt;Smokeping&lt;/a&gt;, &lt;a href="http://www.cacti.net/"&gt;Cacti&lt;/a&gt; and &lt;a href="http://www.nagios.org/"&gt;Nagios&lt;/a&gt; seemed promising at the outset, but they are more concerned with bandwidth and server status, not how the server is running. What I really needed was a way to find out how much memory a server was using, how many Apache requests were being made, what the average load of the server was, and also some way of letting me know when things were under strain.&lt;/p&gt;

&lt;p&gt;I did find some tools like &lt;a href="http://newrelic.com/"&gt;New Relic&lt;/a&gt; and &lt;a href="http://www.appneta.com/"&gt;AppNeta&lt;/a&gt; that seemed to fit the bill, which have large feature sets, and nice graphing and reporting solutions. The only major hurdle for me was that they are expensive, especially when I was considering setting this monitoring system up on my home server for my own projects.&lt;/p&gt;

&lt;p&gt;After not finding anything I decided to create a system that ran the &lt;em&gt;uptime&lt;/em&gt; command on a remote server and recorded the output. uptime is a Linux command that has varied output between systems but will report on how long the server had been running, how many users are logged in and what the load average of the server is. Here is an example output from my local machine (which was doing a lot of code updates and database processing on a development site at the time).&lt;/p&gt;

&lt;span class="geshifilter"&gt;&lt;code class="text geshifilter-text"&gt;10:42:44 up 1 day, 20:36,  4 users,  load average: 0.97, 1.07, 0.90&lt;/code&gt;&lt;/span&gt;

&lt;p&gt;I used Phing to allow me to run the uptime command on a remote server and capture the result. The &lt;em&gt;ssh&lt;/em&gt; task can be used to send any command to a remote system and capture the output in a property. So I used the ssh task to get the output of the uptime command and store it in a property called &lt;em&gt;monitor.uptime&lt;/em&gt;.&lt;/p&gt;

&lt;span class="geshifilter"&gt;&lt;code class="xml geshifilter-xml"&gt;&lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;ssh&lt;/span&gt; &lt;span style="color: #000066;"&gt;username&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"${monitor.username}"&lt;/span&gt; &lt;span style="color: #000066;"&gt;password&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"${monitor.password}"&lt;/span&gt; &lt;span style="color: #000066;"&gt;host&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"${monitor.host}"&lt;/span&gt; &lt;span style="color: #000066;"&gt;command&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"uptime"&lt;/span&gt; &lt;span style="color: #000066;"&gt;property&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"monitor.uptime"&lt;/span&gt; &lt;span style="color: #000000; font-weight: bold;"&gt;/&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/span&gt;

&lt;p&gt;With the output of this command in a property I then used some other processing to extract the data needed and add this to a log file, which was updated thanks to a cron task that just ran the Phing file every 2 minutes. With this log file I created a web page that then graphed the output using Flot charts. This took me an hour to put together and all worked nicely, but the major flaw was that in order to add more options I would need to use a separate ssh task, and each task then added another ssh call to the server. This was aside from the massive security issues surrounding automated ssh logins using passwords stored in plain text.&lt;/p&gt;

&lt;p&gt;I ran the Phing script for a day, but I was still looking for a better solution to monitoring server performance. After posting about the Phing script (and how mad it was) on Facebook Jeremy Coates posted a quick message about a system called Munin. Thanks Jeremy!&lt;/p&gt;

&lt;p&gt;Munin is a system that uses a set of Perl scripts to poll external systems to bring down information about server performance statistics. The data found is then written into a series of graphs that represent the data by day, week, month, and year. By default you get access to things like CPU usage, memory usage, system load, disk access times, processes, uptime, and network traffic. The system is modular so it can be extended to include Apache load, Varnish statistics, MySQL load, and even the number of emails sent.&lt;/p&gt;

&lt;p&gt;After using Munin for a couple of weeks I am really impressed by how much data I can view, and it has even allowed me to track down a performance issue on a server. It's pretty easy to install, but I thought that other people might benefit from me writing down some notes on getting up and running with Munin.&lt;/p&gt;

&lt;h3&gt;Install Munin Server&lt;/h3&gt;

&lt;p&gt;The first step in getting up an running with Munin is to install a Munin server. This is the primary hub of the Munin reporting tool and where you can view the data from each of your Munin nodes. Setting up Munin on most systems is pretty easy, just use the following apt-get command on Debian/Ubuntu setups.&lt;/p&gt;

&lt;span class="geshifilter"&gt;&lt;code class="text geshifilter-text"&gt;sudo apt-get install munin munin-node&lt;/code&gt;&lt;/span&gt;

&lt;p&gt;If you are on Centos/Redhat then you can use the yum package manager in the same way.&lt;/p&gt;

&lt;span class="geshifilter"&gt;&lt;code class="text geshifilter-text"&gt;sudo yum install munin munin-node&lt;/code&gt;&lt;/span&gt;

&lt;p&gt;In order to view the Munin graphs via the web interface you might also need to install the &lt;em&gt;libwww-perl&lt;/em&gt; package. This is required by Apache to run Perl scripts and is necessary in order to allow the server to show the Munin graphs.&lt;/p&gt;

&lt;p&gt;Additionally, you might also want to install the &lt;em&gt;munin-plugins-extra&lt;/em&gt; package. This package contains a bunch of extra modules that allow you to capture results for things like Apache, MySQL or Varnish that are on top of the normal CPU and memory usage statistics. You can do this as a separate step or as part of the installation of the Munin server. You might also be wondering why we are installing a Munin node on the server. This isn't essential and it will be used to report on the status of the server itself, but it's a good test to see if things are working correctly and lets you keep an eye on how things are running locally. My Munin server is hosted on a machine that does a bunch of other things so it makes sense to install the node along side this in order to make sure the rest of the services are able to run.&lt;/p&gt;

&lt;p&gt;The Munin graphs are shown in a web interface (i.e. through a web browser), but you'll need to properly set things up so that you can view the graphs. After installing Munin server on a few different systems as a test I have found that some systems (e.g. Ubuntu) will set everything up, whilst others tend to leave the finer detail to the administrators. On Ubuntu you can edit the file /etc/apache2/conf.d/munin in order to configure the location of the Apache webroot. By default this will only allow access from the localhost IP address and so you might want to add a couple of Allow rules. To allow everyone to access the Munin web interface use the following.&lt;/p&gt;

&lt;span class="geshifilter"&gt;&lt;code class="text geshifilter-text"&gt;Allow from 0.0.0.0&lt;/code&gt;&lt;/span&gt;

&lt;p&gt;To allow everyone from your local network use something like the following.&lt;/p&gt;

&lt;span class="geshifilter"&gt;&lt;code class="text geshifilter-text"&gt;192.168.1.0/24&lt;/code&gt;&lt;/span&gt;

&lt;p&gt;Be careful though, if you use port forwarding between your router and the Munin server you will essentially be allowing access from the outside world to your Munin interface. This is because internally the request appears to be coming from the internal network. This data is not the sort of thing you want everyone to see!&lt;/p&gt;

&lt;p&gt;The main Munin server configuration file is located at /etc/munin/munin.conf. There are a few configuration options to see here as well as the setup of where the Munin nodes are located. The default Munin configuration options are usually fine, unless you need to alter the directories Munin uses to write out the graphs. For example, if you changed the Apache webroot then you will need to alter the config option &lt;em&gt;htmldir&lt;/em&gt; to point to the same place.&lt;/p&gt;

&lt;p&gt;Now you have Munin installed you should be able to view the web interface via the address &lt;a href="http://localhost/munin"&gt;http://localhost/munin&lt;/a&gt;. By default you will see the statistics for localhost, as long as you installed the local Munin node. On installation a cron job is also set up that automatically updates the Munin data every 5 minutes and so it will take a little time for the graphs to start filling up.&lt;/p&gt;

&lt;p&gt;Setting up a node for Munin is probably your primary reason for editing the munin.conf file. The minimal node configuration you need is the display name of the node (for the web interface) and the physical address of the node.&lt;/p&gt;

&lt;div class="geshifilter"&gt;&lt;table class="text geshifilter-text" style="font-family:monospace;"&gt;&lt;tbody&gt;&lt;tr class="li1"&gt;&lt;td style="width:1px;text-align:right;margin:0;padding:0 2px;vertical-align:top;"&gt;&lt;pre style="font-family: monospace; font-weight: normal; font-style: normal"&gt;1
2
&lt;/pre&gt;&lt;/td&gt;&lt;td style="font-family: monospace; font-weight: normal; font-style: normal"&gt;&lt;pre style="font-family: monospace; font-weight: normal; font-style: normal"&gt;[www.hashbangcode.com]
  address 10.0.0.1&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;You can also group the hosts together, which helps when viewing the results as they will be grouped in the same way. Grouping has the following format:&lt;/p&gt;

&lt;div class="geshifilter"&gt;&lt;table class="text geshifilter-text" style="font-family:monospace;"&gt;&lt;tbody&gt;&lt;tr class="li1"&gt;&lt;td style="width:1px;text-align:right;margin:0;padding:0 2px;vertical-align:top;"&gt;&lt;pre style="font-family: monospace; font-weight: normal; font-style: normal"&gt;1
2
3
4
5
6
7
&lt;/pre&gt;&lt;/td&gt;&lt;td style="font-family: monospace; font-weight: normal; font-style: normal"&gt;&lt;pre style="font-family: monospace; font-weight: normal; font-style: normal"&gt;[MyHost;]
 
[MyHost;www.hashbangcode.com]
    address 10.0.0.1
 
[MyHost;www.example.com]
    address 10.0.0.2&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The &lt;em&gt;address&lt;/em&gt; config option is the minimum needed to get a Munin node working.&lt;/p&gt;

&lt;p&gt;To control the updating of a Munin node use the &lt;em&gt;update&lt;/em&gt; node level directive. This defaults to 'yes', but you can set it to 'no' to turn off the updates but retain the data and show the data on the graph.&lt;/p&gt;

&lt;div class="geshifilter"&gt;&lt;table class="text geshifilter-text" style="font-family:monospace;"&gt;&lt;tbody&gt;&lt;tr class="li1"&gt;&lt;td style="width:1px;text-align:right;margin:0;padding:0 2px;vertical-align:top;"&gt;&lt;pre style="font-family: monospace; font-weight: normal; font-style: normal"&gt;1
2
3
&lt;/pre&gt;&lt;/td&gt;&lt;td style="font-family: monospace; font-weight: normal; font-style: normal"&gt;&lt;pre style="font-family: monospace; font-weight: normal; font-style: normal"&gt;[www.hashbangcode.com]
  address 10.0.0.1
  update no&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;To change the port that Munin uses as a default (4949) use the &lt;em&gt;port&lt;/em&gt; node level directive. This is useful if you have to use a different port.

&lt;/p&gt;&lt;div class="geshifilter"&gt;&lt;table class="text geshifilter-text" style="font-family:monospace;"&gt;&lt;tbody&gt;&lt;tr class="li1"&gt;&lt;td style="width:1px;text-align:right;margin:0;padding:0 2px;vertical-align:top;"&gt;&lt;pre style="font-family: monospace; font-weight: normal; font-style: normal"&gt;1
2
3
&lt;/pre&gt;&lt;/td&gt;&lt;td style="font-family: monospace; font-weight: normal; font-style: normal"&gt;&lt;pre style="font-family: monospace; font-weight: normal; font-style: normal"&gt;[www.hashbangcode.com]
  address 10.0.0.1
  port 12345&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The &lt;em&gt;use_node_name&lt;/em&gt; directive determines whether the IP address or the node name (i.e. the name between []) is to be used to contact the node. This defaults to 'no', but can be useful to set to 'yes' when the IP address isn't translated into the correct server address. This might be the case if you are using Munin in an internal network and have a strict firewall policy, but for the most part you can leave this alone.&lt;/p&gt;

&lt;div class="geshifilter"&gt;&lt;table class="text geshifilter-text" style="font-family:monospace;"&gt;&lt;tbody&gt;&lt;tr class="li1"&gt;&lt;td style="width:1px;text-align:right;margin:0;padding:0 2px;vertical-align:top;"&gt;&lt;pre style="font-family: monospace; font-weight: normal; font-style: normal"&gt;1
2
3
&lt;/pre&gt;&lt;/td&gt;&lt;td style="font-family: monospace; font-weight: normal; font-style: normal"&gt;&lt;pre style="font-family: monospace; font-weight: normal; font-style: normal"&gt;[www.hashbangcode.com]
  address 10.0.0.1
  use_node_name yes&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;There are a lot more options than I have shown here. For more information about using the config file take a look at the &lt;a href="http://munin-monitoring.org/wiki/munin.conf" title="munin.conf"&gt;Munin documentation on config files&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;Installing Munin Node&lt;/h3&gt;

&lt;p&gt;Munin is a useful tool when viewing localhost statistics, but creating nodes is really where the power lies. As I have said previously, Munin works by having a central server polling remote nodes to draw down the data. So when we install Munin as a node we are only installing a small component that reacts to incoming Munin requests. This is actually an important concept with Munin, if your server doesn't ask the nodes for results (e.g. because of a network failure) then the data is not be generated and your Munin graphs will have holes in them. Your Munin nodes will not keep hold of data until the next time it can give them to the server, they don't even generate it.&lt;/p&gt;

&lt;p&gt;To install a Munin node on a Debian/Ubuntu server use the following command.&lt;/p&gt;

&lt;span class="geshifilter"&gt;&lt;code class="text geshifilter-text"&gt;sudo apt-get install munin-node&lt;/code&gt;&lt;/span&gt;

&lt;p&gt;To install a Munin node on a Centos/Redhat server use the following. If this fails or is not found then you might need to have the EPEL repository installed as a prerequisite.&lt;/p&gt;

&lt;span class="geshifilter"&gt;&lt;code class="text geshifilter-text"&gt;sudo yum install munin-node&lt;/code&gt;&lt;/span&gt;

&lt;p&gt;Now that the Munin node is installed you'll want to look at the config file, which can be found at /etc/munin/munin-node.conf. The following is a typical Munin node configuration file.&lt;/p&gt;

&lt;div class="geshifilter"&gt;&lt;table class="text geshifilter-text" style="font-family:monospace;"&gt;&lt;tbody&gt;&lt;tr class="li1"&gt;&lt;td style="width:1px;text-align:right;margin:0;padding:0 2px;vertical-align:top;"&gt;&lt;pre style="font-family: monospace; font-weight: normal; font-style: normal"&gt;1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
&lt;/pre&gt;&lt;/td&gt;&lt;td style="font-family: monospace; font-weight: normal; font-style: normal"&gt;&lt;pre style="font-family: monospace; font-weight: normal; font-style: normal"&gt;log_level 4
log_file /var/log/munin-node/munin-node.log
pid_file /var/run/munin/munin-node.pid
 
background 1m
setsid 1
 
user root
group root
 
# Regexps for files to ignore
ignore_file [\#~]$m
ignore_file DEADJOE$
ignore_file \.bak$
ignore_file %$
ignore_file \.dpkg-(tmp|new|old|dist)$
ignore_file \.rpm(save|new)$
ignore_file \.pod$
 
#host_name localhost.localdomain
 
allow ^127\.0\.0\.1$
allow ^::1$
 
# Which address to bind to;
host *
 
# And which port
port 4949&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;By default this node only allows access to the Munin data from the local machine. Before you can use the Munin node you need to add your IP address as an allow rule in this config file.&lt;/p&gt;

&lt;p&gt;Once that is done, it's probably a good idea to make sure that the server the node is installed on allows external connections on the Munin port (4949). For this you will need to check your iptables configuration. To view the current iptables configurations you have use the following command (the sudo is important).&lt;/p&gt;

&lt;span class="geshifilter"&gt;&lt;code class="text geshifilter-text"&gt;sudo iptables -L -n&lt;/code&gt;&lt;/span&gt;

&lt;p&gt;If no reference to the Munin server IP address on port 4949 exists then you will need to create one. Here is an example of adding a iptables rule for a Munin server.&lt;/p&gt;

&lt;span class="geshifilter"&gt;&lt;code class="text geshifilter-text"&gt;sudo iptables -A INPUT -p tcp --dport 4949 -s 10.0.0.1 -j ACCEPT&lt;/code&gt;&lt;/span&gt;

&lt;p&gt;When you run the configuration lookup command (sudo iptables -L -n) a second time you should see the following.&lt;/p&gt;

&lt;div class="geshifilter"&gt;&lt;table class="text geshifilter-text" style="font-family:monospace;"&gt;&lt;tbody&gt;&lt;tr class="li1"&gt;&lt;td style="width:1px;text-align:right;margin:0;padding:0 2px;vertical-align:top;"&gt;&lt;pre style="font-family: monospace; font-weight: normal; font-style: normal"&gt;1
2
3
4
5
6
&lt;/pre&gt;&lt;/td&gt;&lt;td style="font-family: monospace; font-weight: normal; font-style: normal"&gt;&lt;pre style="font-family: monospace; font-weight: normal; font-style: normal"&gt;Chain INPUT (policy ACCEPT)
target     prot opt source               destination
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:22
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:80
ACCEPT     tcp  --  0.0.0.0/0            0.0.0.0/0            tcp dpt:8080
ACCEPT     tcp  --  10.0.0.1       0.0.0.0/0            tcp dpt:4949&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Everything is now set up, but you might notice that your Munin server doesn't connect. The reason is that when you change any of the rules in your Munin node you will need to restart the node for the options to be loaded. To restart a Munin node use the following.&lt;/p&gt;

&lt;span class="geshifilter"&gt;&lt;code class="text geshifilter-text"&gt;sudo service munin-node restart&lt;/code&gt;&lt;/span&gt;

&lt;p&gt;Or, if &lt;em&gt;service&lt;/em&gt; isn't available then you can use this command instead.&lt;/p&gt;

&lt;span class="geshifilter"&gt;&lt;code class="text geshifilter-text"&gt;sudo /etc/init.d/munin-node restart&lt;/code&gt;&lt;/span&gt;

&lt;p&gt;When you install a Munin node for the first time you need to make sure that the service is running if the server is rebooted. To make sure the Munin node starts if the server restarts with the server you can use &lt;em&gt;chkconfig&lt;em&gt;.&lt;/em&gt;&lt;/em&gt;&lt;/p&gt;

&lt;span class="geshifilter"&gt;&lt;code class="text geshifilter-text"&gt;sudo chkconfig --add munin-node&lt;/code&gt;&lt;/span&gt;

&lt;p&gt;If that fails then try the alternative of.&lt;/p&gt;

&lt;span class="geshifilter"&gt;&lt;code class="text geshifilter-text"&gt;sudo /sbin/chkconfig munin-node on&lt;/code&gt;&lt;/span&gt;

&lt;p&gt;One final thing to remember is that if your host is running a hardware firewall then you'll need to allow connections to pass through that as well.&lt;/p&gt;

&lt;h3&gt;Troubleshooting&lt;/h3&gt;

&lt;p&gt;Getting started with Munin can sometimes be a little bit painful. For the most part everything installs and runs fine. My main problem was when trying to hookup a new node. There tends to be a lot of stuff in-between that can cause the Munin server to fail it's data polling. If you have any problems then remember that Munin logs everything. If you edit your server configuration file to add a new node and nothing appears to be showing in the web interface then have a look at the munin-update.log. It will usually tell you if there is a connection problem or if something wrong happened during the update process, which happens every 5 minutes. To view the last 50 log entries use the following.&lt;/p&gt;

&lt;span class="geshifilter"&gt;&lt;code class="text geshifilter-text"&gt;sudo tail -n 50 /var/log/munin/munin-update.log&lt;/code&gt;&lt;/span&gt;

&lt;p&gt;The Munin server uses a cron job set to run every 5 minutes in order to poll the Munin nodes. You need to make sure that the user who runs the cron is munin-cron. If nothing is being written to the logs then it's generally because the cron job isn't running. To view the cron jobs that have been setup for the munin-cron user use the following command.&lt;/p&gt;

&lt;span class="geshifilter"&gt;&lt;code class="text geshifilter-text"&gt;sudo -u munin-cron crontab -e&lt;/code&gt;&lt;/span&gt;

&lt;p&gt;What is good about Munin is that it uses a standard interface and so even if you install Munin 2.1 on your server and Munin 1.4 on your node the two will happily talk to each other so that tends not to be a problem.&lt;/p&gt;

&lt;h3&gt;Understanding The Output&lt;/h3&gt;

&lt;p&gt;Munin produces a lot of graphs and other data, and I could write an entire book about understanding it all. My first few days with Munin was trying to get to grips exactly what everything meant and if a graph with a certain value was a bad thing or not. Munin reports are spit into sections, which makes pinpointing issues really handy. Here is a breakdown of a few of the graphs in different sections.&lt;/p&gt;

&lt;h4&gt;Disk&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;Disk throughput per device&lt;/strong&gt; - This shows the amount of data being written or read from a disk. A disk will always be slower than memory when retrieving files, but a lot of disk reads can be a problem. For example, it could indicate that your database server doesn't have enough RAM and is therefore storing tables on the disk rather than in memory.&lt;/p&gt;

&lt;p&gt;&lt;img src="/sites/default/files/images/diskstats_throughput-day.png" width="497" height="280" alt="Disk throughput per device by day - Munin graph" /&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Inode usage in percent&lt;/strong&gt; - Every file system has an index showing where every file is stored, what it's name is, and what the permissions of the file are. This has a physical limit, and when it gets full it means you can't create any more files, even if there is enough space on the disk. Keeping an eye on this value is therefore very important as it will quickly cripple or bring down your website.&lt;/p&gt;

&lt;p&gt;&lt;img src="/sites/default/files/images/df_inode-day.png" width="497" height="340" alt="Inode usage in percent by day - Munin graph" /&gt;&lt;/p&gt;

&lt;h4&gt;Network&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;Firewall Throughput&lt;/strong&gt; - This is the amount of traffic going through the iptables firewall, which is generally all traffic on most servers. This is only important if you are running the Munin node on a router, firewall or gateway system.&lt;/p&gt;

&lt;p&gt;&lt;img src="/sites/default/files/images/fw_packets-day.png" width="497" height="292" alt="Firewall Throughputby day - Munin graph" /&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;eth0 Traffic&lt;/strong&gt; - The amount of traffic passing through the eth0 interface. Useful for keeping an eye on the available bandwidth on a web host.&lt;/p&gt;

&lt;p&gt;&lt;img src="/sites/default/files/images/if_eth0-day.png" width="497" height="280" alt="eth0 traffic by day - Munin graph" /&gt;&lt;/p&gt;

&lt;h4&gt;Processes&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;Fork rate&lt;/strong&gt; - Processes are created by forking existing processed into two new processes. This indicates the rate at which new processes are created and can be an indication of a bottleneck if lots of processes are being created.&lt;/p&gt;

&lt;p&gt;&lt;img src="/sites/default/files/images/forks-day.png" width="497" height="280" alt="Fork rate by day - Munin graph" /&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Number of threads&lt;/strong&gt; - This is the total number of processes running on a system.&lt;/p&gt;

&lt;p&gt;&lt;img src="/sites/default/files/images/threads-day.png" width="497" height="280" alt="Number of threads by day - Munin graph" /&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;CPU Usage&lt;/strong&gt; - This indicates the usage of the CPU and is divided into multiple parts. For example, &lt;em&gt;iowait&lt;/em&gt; shows the amount of time spent by processes for IO (e.g. disk access).

&lt;/p&gt;&lt;p&gt;&lt;img src="/sites/default/files/images/cpu-day.png" width="497" height="364" alt="CPU Usage by day - Munin graph" /&gt;&lt;/p&gt;

&lt;h4&gt;System&lt;/h4&gt;

&lt;p&gt;&lt;strong&gt;Available entropy&lt;/strong&gt; - The entropy is the measure of the random numbers available from /dev/urandom. These random numbers are needed to create SSL connections. If you create a large number of SSL connections this randomness pool could possibly run out of real random numbers.&lt;/p&gt;

&lt;p&gt;&lt;img src="/sites/default/files/images/entropy-day.png" width="497" height="280" alt="Available entropy by day - Munin graph" /&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Inode table usage&lt;/strong&gt; - This shows the total number of files that are open on a system. If this number increases suddenly then it might mean that there is a program on the system that is not closing files correctly.&lt;/p&gt;&lt;p&gt;

&lt;/p&gt;&lt;p&gt;&lt;img src="/sites/default/files/images/open_inodes-day.png" width="497" height="292" alt="Inode table usage by day - Munin graph" /&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Load average&lt;/strong&gt; - This is a useful number that shows how much is trying to be run by the system. Numbers under 1.0 mean that there is enough resources to cope with the amount of work to be done. Numbers over 1.0 mean that the system is under high load and there are not enough system resources. &lt;a href="http://blog.scoutapp.com/articles/2009/07/31/understanding-load-averages" title="Understanding Linux CPU Load - when should you be worried?"&gt;Understanding Linux CPU Load - when should you be worried?&lt;/a&gt; is perhaps the best explanation I have seen for load average.&lt;/p&gt;

&lt;p&gt;&lt;img src="/sites/default/files/images/load-day.png" width="497" height="280" alt="Load average by day - Munin graph" /&gt;&lt;/p&gt;

&lt;p&gt;All of the above screenshots were taken from the Munin setup I have for #! code. Those of you who are familiar with the statistics Munin produces will see that the server isn't really under strain all that much. This is only a small sample of the statistics available from Munin, especially when you start to use plugins were you can view statistics for Apache, Varnish, MySQL, sendmail and a variety of other things. Setting up these extra plugins is usually quite simple, and quite often is done for you anyway.&lt;/p&gt;

&lt;h3&gt;Notifications&lt;/h3&gt;

&lt;p&gt;Looking at graphs is fine, but the real power of Munin is that it can tell you when things start going wrong. Built into Munin is a notification system that will highlight problems within the web interface but also contact you (via email as a default) when things reach certain limits. These limits are numbers that you need to set yourself, but doing so isn't too difficult. There are two levels of issues in Munin, &lt;em&gt;warning&lt;/em&gt; is for when things might go wrong and &lt;em&gt;critical&lt;/em&gt; is for when things really need attention. As these values are set arbitrarily it is up to you to decide what is a warning and what is a critical error.&lt;/p&gt;

&lt;p&gt;The first step in setting up Munin to contact you when a problem arrises is to create a contact. To do this you just need to add something like the following to your servers munin.conf file. This tells Munin to always email the 'user' email address when a warning or critical issue has been found.&lt;/p&gt;

&lt;div class="geshifilter"&gt;&lt;table class="text geshifilter-text" style="font-family:monospace;"&gt;&lt;tbody&gt;&lt;tr class="li1"&gt;&lt;td style="width:1px;text-align:right;margin:0;padding:0 2px;vertical-align:top;"&gt;&lt;pre style="font-family: monospace; font-weight: normal; font-style: normal"&gt;1
2
3
&lt;/pre&gt;&lt;/td&gt;&lt;td style="font-family: monospace; font-weight: normal; font-style: normal"&gt;&lt;pre style="font-family: monospace; font-weight: normal; font-style: normal"&gt;# 'user' can be any user you want it to be
contact.user.command mail -s "Munin notification" email@example.com
contact.user.always_send warning critical&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The next step is to set up some warnings for your nodes. In the following example I am setting the load average warning threshold to be 15 and the critical threshold to be 20. This means that if the system load passes any of these two values Munin will let me know about it.&lt;/p&gt;

&lt;div class="geshifilter"&gt;&lt;table class="text geshifilter-text" style="font-family:monospace;"&gt;&lt;tbody&gt;&lt;tr class="li1"&gt;&lt;td style="width:1px;text-align:right;margin:0;padding:0 2px;vertical-align:top;"&gt;&lt;pre style="font-family: monospace; font-weight: normal; font-style: normal"&gt;1
2
3
4
&lt;/pre&gt;&lt;/td&gt;&lt;td style="font-family: monospace; font-weight: normal; font-style: normal"&gt;&lt;pre style="font-family: monospace; font-weight: normal; font-style: normal"&gt;[www.example.com]
    address 10.0.0.1
    load.load.warning 10
    load.load.critical 20&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;A good tip here is that when you are trying out these values for the first time set the threshold to an intentionally low value so that they will be triggered. This ensures that the correct values are being watched in Munin and that the alerts will work correctly. Increase these values to sensible levels when you are happy that things will work when they are needed.&lt;/p&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="field field-name-field-blogcategory field-type-taxonomy-term-reference field-label-above"&gt;&lt;div class="field-label"&gt;Category:&amp;nbsp;&lt;/div&gt;&lt;div class="field-items"&gt;&lt;div class="field-item even"&gt;&lt;a href="/blog/category/linuxunix"&gt;Linux/Unix&lt;/a&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="field field-name-field-blogtags field-type-taxonomy-term-reference field-label-above"&gt;&lt;div class="field-label"&gt;Tags:&amp;nbsp;&lt;/div&gt;&lt;div class="field-items"&gt;&lt;div class="field-item even"&gt;&lt;a href="/blog/tag/munin"&gt;Munin&lt;/a&gt;&lt;/div&gt;&lt;div class="field-item odd"&gt;&lt;a href="/category/tags/performance"&gt;performance&lt;/a&gt;&lt;/div&gt;&lt;div class="field-item even"&gt;&lt;a href="/blog/tag/monitor"&gt;monitor&lt;/a&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/hashbangcode/~4/0FXB4lMQOXM" height="1" width="1"/&gt;</description>
 <pubDate>Mon, 20 May 2013 18:42:24 +0000</pubDate>
 <dc:creator>philipnorton42</dc:creator>
 <guid isPermaLink="false">713 at http://www.hashbangcode.com</guid>
 <comments>http://www.hashbangcode.com/blog/monitoring-performance-munin-713.html#comments</comments>
<feedburner:origLink>http://www.hashbangcode.com/blog/monitoring-performance-munin-713.html</feedburner:origLink></item>
<item>
 <title>Source Controlled Git Hooks With Phing</title>
 <link>http://feedproxy.google.com/~r/hashbangcode/~3/NFy2QNXRA_g/source-controlled-git-hooks-phing-712.html</link>
 <description>&lt;div class="field field-name-body field-type-text-with-summary field-label-hidden"&gt;&lt;div class="field-items"&gt;&lt;div class="field-item even"&gt;&lt;p&gt;The other day I was experimenting with Git hooks. These are scripts that you can execute before certain actions are run in Git. For example, you might want to ensure that forced updates are not run, ensuring respository files have the correct permissions after merging, or that the files have ASCII standard names before being committed.&lt;/p&gt;

&lt;p&gt;To use a hook in Git you just need to add them to the .git/hooks directory in your respository and to change the mode of the file so that it is executable. A new Git repository will create several sample hook files that can be used by removing the '.sample' from the end and making them executable. For more information on Git hooks and how to use them see the &lt;a href="https://www.kernel.org/pub/software/scm/git/docs/githooks.html" title="githooks(5) Manual Page"&gt;Git hooks manual page&lt;/a&gt; in the Git documentation.&lt;/p&gt;

&lt;p&gt;My primary reason for creating a Git hook was to ensure that there were no syntax errors on my code before it was even committed. I had previously been either checking manually or using Jenkins to download the repository and check the code for errors, but then I realised that it would be far easier to stop them at the source.&lt;/p&gt;

&lt;p&gt;To this end I created a hook file called &lt;em&gt;pre-commit&lt;/em&gt; and added this to the hooks directory. As you can probably guess this hook is run before the commit action is executed in Git. I have talked previously about &lt;a href="http://www.hashbangcode.com/blog/checking-syntax-errors-php-and-javascript-using-phing-678.html" title="Checking Syntax Errors In PHP And JavaScript Using Phing"&gt;using Phing to syntax check all PHP and JavaScript files&lt;/a&gt; in a repository so I just used the same Phing target here. The only addition was that I also needed the commit action to stop if the Phing syntax check failed, which could be done in the hook file itself. There would be enough information about what failed in the Phing output to show why the commit had failed so I didn't need to print anything else out here. This is the hook file in full.&lt;/p&gt;

&lt;div class="geshifilter"&gt;&lt;table class="php geshifilter-php" style="font-family:monospace;"&gt;&lt;tbody&gt;&lt;tr class="li1"&gt;&lt;td style="width:1px;text-align:right;margin:0;padding:0 2px;vertical-align:top;"&gt;&lt;pre style="font-family: monospace; font-weight: normal; font-style: normal"&gt;1
2
3
4
5
&lt;/pre&gt;&lt;/td&gt;&lt;td style="font-family: monospace; font-weight: normal; font-style: normal"&gt;&lt;pre style="font-family: monospace; font-weight: normal; font-style: normal"&gt;&lt;span style="color: #666666; font-style: italic;"&gt;#!/bin/sh&lt;/span&gt;
 
&lt;span style="color: #b1b100;"&gt;echo&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;"Parsing PHP and JavaScript files for lint errors."&lt;/span&gt;
 
phing &lt;span style="color: #339933;"&gt;-&lt;/span&gt;f scripts&lt;span style="color: #339933;"&gt;/&lt;/span&gt;build&lt;span style="color: #339933;"&gt;.&lt;/span&gt;xml syntaxcheck &lt;span style="color: #339933;"&gt;||&lt;/span&gt; &lt;a href="http://www.php.net/exit"&gt;&lt;span style="color: #990000;"&gt;exit&lt;/span&gt;&lt;/a&gt; &lt;span style="color: #cc66cc;"&gt;1&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;This worked as expected and I was unable to commit and files that contained syntax errors to the repository. The only problem was that if I checked the repository out again in a different location I would lose this hook as the files in the .git directory (and therefore .git/hooks) are not source controlled. This also meant that I couldn't get other people working on the project to follow the same practice as they would be missing the hook on their repositories as well. After a bit of searching for a solution to this I found the best course of action was to include the hook file in the respository and then write a script to automate the inclusion of the hook into the .git/hooks directory.&lt;/p&gt;

&lt;p&gt;I created a directory in my respository called githooks and moved the hook I had created into it so that it could be source controlled. Rather than create a script from scratch I decided to add a githook target to the already existing Phing build file in my repository. This target has the following actions:&lt;/p&gt;

&lt;ul&gt;&lt;li&gt;Define a couple of properties that point to the .git/hooks directory and the githooks directory in my repository.&lt;/li&gt;
&lt;li&gt;Delete the existing .git/hooks direcory.&lt;/li&gt;
&lt;li&gt;Symlink the githooks directory to .git/hooks, thereby replacing the original hooks directory.&lt;/li&gt;
&lt;li&gt;Create a fileset that contains all of the possible hooks files in the githooks directory.&lt;/li&gt;
&lt;li&gt;Loop through the files in the fileset and ensure that they are all set to be executable. This is tricky and requires the use of a secondary target called chmodsetexecute that has the single action of setting the filename passed to it to be executable.&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;Here are the two Phing targets used to set up the repository with custom Git hooks.&lt;/p&gt;

&lt;div class="geshifilter"&gt;&lt;table class="xml geshifilter-xml" style="font-family:monospace;"&gt;&lt;tbody&gt;&lt;tr class="li1"&gt;&lt;td style="width:1px;text-align:right;margin:0;padding:0 2px;vertical-align:top;"&gt;&lt;pre style="font-family: monospace; font-weight: normal; font-style: normal"&gt;1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
&lt;/pre&gt;&lt;/td&gt;&lt;td style="font-family: monospace; font-weight: normal; font-style: normal"&gt;&lt;pre style="font-family: monospace; font-weight: normal; font-style: normal"&gt; &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;target&lt;/span&gt; &lt;span style="color: #000066;"&gt;name&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"githook"&lt;/span&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
   &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;resolvepath&lt;/span&gt; &lt;span style="color: #000066;"&gt;propertyName&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"git_hooks_path"&lt;/span&gt; &lt;span style="color: #000066;"&gt;file&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"${project.basedir}/../.git/hooks"&lt;/span&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;/&amp;gt;&lt;/span&gt;&lt;/span&gt;
   &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;resolvepath&lt;/span&gt; &lt;span style="color: #000066;"&gt;propertyName&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"repo_hooks_path"&lt;/span&gt; &lt;span style="color: #000066;"&gt;file&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"${project.basedir}/../githooks"&lt;/span&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;/&amp;gt;&lt;/span&gt;&lt;/span&gt;
 
   &lt;span style="color: #808080; font-style: italic;"&gt;&amp;lt;!-- Delete existing .git/hooks directory --&amp;gt;&lt;/span&gt;
   &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;delete&lt;/span&gt; &lt;span style="color: #000066;"&gt;dir&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"${git_hooks_path}"&lt;/span&gt; &lt;span style="color: #000066;"&gt;quiet&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"true"&lt;/span&gt; &lt;span style="color: #000066;"&gt;failonerror&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"false"&lt;/span&gt; &lt;span style="color: #000000; font-weight: bold;"&gt;/&amp;gt;&lt;/span&gt;&lt;/span&gt;
   
   &lt;span style="color: #808080; font-style: italic;"&gt;&amp;lt;!-- link the git hooks directories together --&amp;gt;&lt;/span&gt;
   &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;symlink&lt;/span&gt; &lt;span style="color: #000066;"&gt;target&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"${repo_hooks_path}"&lt;/span&gt; &lt;span style="color: #000066;"&gt;link&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"${git_hooks_path}"&lt;/span&gt; &lt;span style="color: #000000; font-weight: bold;"&gt;/&amp;gt;&lt;/span&gt;&lt;/span&gt;
   
   &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;fileset&lt;/span&gt; &lt;span style="color: #000066;"&gt;dir&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"${repo_hooks_path}"&lt;/span&gt; &lt;span style="color: #000066;"&gt;id&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"hookfiles"&lt;/span&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
     &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;include&lt;/span&gt; &lt;span style="color: #000066;"&gt;name&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"applypatch-msg"&lt;/span&gt; &lt;span style="color: #000000; font-weight: bold;"&gt;/&amp;gt;&lt;/span&gt;&lt;/span&gt;
     &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;include&lt;/span&gt; &lt;span style="color: #000066;"&gt;name&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"post-commit"&lt;/span&gt; &lt;span style="color: #000000; font-weight: bold;"&gt;/&amp;gt;&lt;/span&gt;&lt;/span&gt;
     &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;include&lt;/span&gt; &lt;span style="color: #000066;"&gt;name&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"post-update"&lt;/span&gt; &lt;span style="color: #000000; font-weight: bold;"&gt;/&amp;gt;&lt;/span&gt;&lt;/span&gt;
     &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;include&lt;/span&gt; &lt;span style="color: #000066;"&gt;name&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"pre-commit"&lt;/span&gt; &lt;span style="color: #000000; font-weight: bold;"&gt;/&amp;gt;&lt;/span&gt;&lt;/span&gt;
     &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;include&lt;/span&gt; &lt;span style="color: #000066;"&gt;name&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"update"&lt;/span&gt; &lt;span style="color: #000000; font-weight: bold;"&gt;/&amp;gt;&lt;/span&gt;&lt;/span&gt;
     &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;include&lt;/span&gt; &lt;span style="color: #000066;"&gt;name&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"commit-msg"&lt;/span&gt; &lt;span style="color: #000000; font-weight: bold;"&gt;/&amp;gt;&lt;/span&gt;&lt;/span&gt;
     &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;include&lt;/span&gt; &lt;span style="color: #000066;"&gt;name&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"post-receive"&lt;/span&gt; &lt;span style="color: #000000; font-weight: bold;"&gt;/&amp;gt;&lt;/span&gt;&lt;/span&gt;
     &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;include&lt;/span&gt; &lt;span style="color: #000066;"&gt;name&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"pre-applypatch"&lt;/span&gt; &lt;span style="color: #000000; font-weight: bold;"&gt;/&amp;gt;&lt;/span&gt;&lt;/span&gt;
     &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;include&lt;/span&gt; &lt;span style="color: #000066;"&gt;name&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"pre-rebase"&lt;/span&gt; &lt;span style="color: #000000; font-weight: bold;"&gt;/&amp;gt;&lt;/span&gt;&lt;/span&gt;
   &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;/fileset&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
   
   &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;foreach&lt;/span&gt; &lt;span style="color: #000066;"&gt;param&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"hookfilename"&lt;/span&gt; &lt;span style="color: #000066;"&gt;target&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"chmodsetexecute"&lt;/span&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
     &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;fileset&lt;/span&gt; &lt;span style="color: #000066;"&gt;refid&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"hookfiles"&lt;/span&gt; &lt;span style="color: #000000; font-weight: bold;"&gt;/&amp;gt;&lt;/span&gt;&lt;/span&gt;
   &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;/foreach&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
 &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;/target&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
 
 &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;target&lt;/span&gt; &lt;span style="color: #000066;"&gt;name&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"chmodsetexecute"&lt;/span&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
   &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;exec&lt;/span&gt; &lt;span style="color: #000066;"&gt;command&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;"chmod +x ${repo_hooks_path}/${hookfilename}"&lt;/span&gt; &lt;span style="color: #000000; font-weight: bold;"&gt;/&amp;gt;&lt;/span&gt;&lt;/span&gt;
 &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;/target&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;With all this in place it is just a case of making sure that this target is ran every time a new checkout is done and that existing repositories are updated with this change. This sets up the correct Git hooks in place and makes sure that no one can commit code that has syntax errors.&lt;/p&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="field field-name-field-blogcategory field-type-taxonomy-term-reference field-label-above"&gt;&lt;div class="field-label"&gt;Category:&amp;nbsp;&lt;/div&gt;&lt;div class="field-items"&gt;&lt;div class="field-item even"&gt;&lt;a href="/blog/category/phing"&gt;Phing&lt;/a&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="field field-name-field-blogtags field-type-taxonomy-term-reference field-label-above"&gt;&lt;div class="field-label"&gt;Tags:&amp;nbsp;&lt;/div&gt;&lt;div class="field-items"&gt;&lt;div class="field-item even"&gt;&lt;a href="/blog/tag/git"&gt;git&lt;/a&gt;&lt;/div&gt;&lt;div class="field-item odd"&gt;&lt;a href="/category/tags/phing"&gt;Phing&lt;/a&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/hashbangcode/~4/NFy2QNXRA_g" height="1" width="1"/&gt;</description>
 <pubDate>Fri, 10 May 2013 23:15:51 +0000</pubDate>
 <dc:creator>philipnorton42</dc:creator>
 <guid isPermaLink="false">712 at http://www.hashbangcode.com</guid>
 <comments>http://www.hashbangcode.com/blog/source-controlled-git-hooks-phing-712.html#comments</comments>
<feedburner:origLink>http://www.hashbangcode.com/blog/source-controlled-git-hooks-phing-712.html</feedburner:origLink></item>
<item>
 <title>Detecting The Sudo User In Phing</title>
 <link>http://feedproxy.google.com/~r/hashbangcode/~3/ErCH7YOPmOM/detecting-sudo-user-phing-710.html</link>
 <description>&lt;div class="field field-name-body field-type-text-with-summary field-label-hidden"&gt;&lt;div class="field-items"&gt;&lt;div class="field-item even"&gt;&lt;p&gt;I use Phing for a lot of different tasks, it helps me to automate things that I would otherwise mess up if left to my own devices. Prime candidates for Phing scripts are things that I don't do that much and forget how to do them, or that have a number of complex steps. The only problem I have found is that because many of the Phing scripts I create rely on system changes (eg, configuring an Apache server) they therefore require system changing privileges. Normally I would just prefix the Phing command with sudo, but every now and then I forget all about that step and the build fails. This can be dangerous as I am then left with a build that failed, which might leave a system partly configured or even take a server offline.&lt;/p&gt;

&lt;p&gt;This got me to thinking about what Phing could do to remind me if I forget to use sudo on my sudo requiring scripts. There must a better way of reminding myself that I should be running the script as sudo rather than just having the build fail on me. After looking through the Phing documentation and a bit of searching online I found nothing that does this.&lt;/p&gt;

&lt;p&gt;The &lt;a href="http://www.twitter.com/phingofficial" title="@phingofficial"&gt;@phingofficial&lt;/a&gt; channel on Twitter (who are awesome by the way) suggested that I could use the output of 'whoami' or 'id -u' to test if a user was running as sudo. This is quite easy to do using the &lt;em&gt;&amp;lt;exec&amp;gt;&lt;/em&gt; task, which I can use to set a property. Here are a couple of examples.&lt;/p&gt;

&lt;p&gt;The following sets the property 'currentuser' to be the output of the 'whoami' command. If the script is being run as sudo then this will be 'root' otherwise it will be the username of the current user.&lt;/p&gt;
&lt;span class="geshifilter"&gt;&lt;code class="xml geshifilter-xml"&gt;&lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;exec&lt;/span&gt; &lt;span style="color: #000066;"&gt;command&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;&amp;quot;whoami&amp;quot;&lt;/span&gt; &lt;span style="color: #000066;"&gt;outputProperty&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;&amp;quot;currentuser&amp;quot;&lt;/span&gt; &lt;span style="color: #000000; font-weight: bold;"&gt;/&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/span&gt;

&lt;p&gt;The following sets the property 'currentuser' to be the output of the 'id -u' command. If the script is being run as sudo then this will be '0', otherwise it will be the ID of the current user (1000 on my localhost).&lt;/p&gt;
&lt;span class="geshifilter"&gt;&lt;code class="xml geshifilter-xml"&gt;&lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;exec&lt;/span&gt; &lt;span style="color: #000066;"&gt;command&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;&amp;quot;id -u&amp;quot;&lt;/span&gt; &lt;span style="color: #000066;"&gt;outputProperty&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;&amp;quot;currentuser&amp;quot;&lt;/span&gt; &lt;span style="color: #000000; font-weight: bold;"&gt;/&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/span&gt;

&lt;p&gt;If you are on Windows then you might be able to use the following command, which will produce an access denied message if the user is not an administrator. If you have any other suggestions on how to detect the administrator user on Windows via the command line (or even through PHP) then I'd be happy to hear from you.&lt;/p&gt;
&lt;span class="geshifilter"&gt;&lt;code class="xml geshifilter-xml"&gt;&lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;exec&lt;/span&gt; &lt;span style="color: #000066;"&gt;command&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;&amp;quot;net user &amp;quot;&lt;/span&gt;%username%&lt;span style="color: #ff0000;"&gt;&amp;quot;&amp;quot;&lt;/span&gt; &lt;span style="color: #000066;"&gt;outputProperty&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;&amp;quot;currentuser&amp;quot;&lt;/span&gt; &lt;span style="color: #000000; font-weight: bold;"&gt;/&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/span&gt;

&lt;p&gt;With that in place I can use the property to create a boolean value that I can use later in the script using the &lt;em&gt;&amp;lt;condition&amp;gt;&lt;/em&gt; task. Assuming that I use the 'whoami' command from the above examples I can do the following.&lt;/p&gt;

&lt;div class="geshifilter"&gt;&lt;table class="xml geshifilter-xml" style="font-family:monospace;"&gt;&lt;tbody&gt;&lt;tr class="li1"&gt;&lt;td style="width:1px;text-align:right;margin:0;padding:0 2px;vertical-align:top;"&gt;&lt;pre style="font-family: monospace; font-weight: normal; font-style: normal"&gt;1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
&lt;/pre&gt;&lt;/td&gt;&lt;td style="font-family: monospace; font-weight: normal; font-style: normal"&gt;&lt;pre style="font-family: monospace; font-weight: normal; font-style: normal"&gt;&lt;span style="color: #808080; font-style: italic;"&gt;&amp;lt;!-- Create the currentuser property --&amp;gt;&lt;/span&gt;
&lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;exec&lt;/span&gt; &lt;span style="color: #000066;"&gt;command&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;&amp;quot;whoami&amp;quot;&lt;/span&gt; &lt;span style="color: #000066;"&gt;outputProperty&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;&amp;quot;currentuser&amp;quot;&lt;/span&gt; &lt;span style="color: #000000; font-weight: bold;"&gt;/&amp;gt;&lt;/span&gt;&lt;/span&gt;
&amp;nbsp;
&lt;span style="color: #808080; font-style: italic;"&gt;&amp;lt;!-- Set the 'issudo' property to be true if the user is root --&amp;gt;&lt;/span&gt;
&lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;condition&lt;/span&gt; &lt;span style="color: #000066;"&gt;property&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;&amp;quot;issudo&amp;quot;&lt;/span&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&amp;nbsp; &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;equals&lt;/span&gt; &lt;span style="color: #000066;"&gt;arg1&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;&amp;quot;${currentuser}&amp;quot;&lt;/span&gt; &lt;span style="color: #000066;"&gt;arg2&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;&amp;quot;root&amp;quot;&lt;/span&gt; &lt;span style="color: #000000; font-weight: bold;"&gt;/&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;/condition&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&amp;nbsp;
&lt;span style="color: #808080; font-style: italic;"&gt;&amp;lt;!-- Fail the build if the user is not root --&amp;gt;&lt;/span&gt;
&lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;if&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&amp;nbsp; &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;not&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;equals&lt;/span&gt; &lt;span style="color: #000066;"&gt;arg1&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;&amp;quot;${issudo}&amp;quot;&lt;/span&gt; &lt;span style="color: #000066;"&gt;arg2&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;&amp;quot;true&amp;quot;&lt;/span&gt; &lt;span style="color: #000000; font-weight: bold;"&gt;/&amp;gt;&lt;/span&gt;&lt;/span&gt;
&amp;nbsp; &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;/not&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&amp;nbsp; &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;then&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;fail&lt;/span&gt; &lt;span style="color: #000066;"&gt;message&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;&amp;quot;You must run this build as sudo!&amp;quot;&lt;/span&gt; &lt;span style="color: #000000; font-weight: bold;"&gt;/&amp;gt;&lt;/span&gt;&lt;/span&gt;
&amp;nbsp; &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;/then&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;/if&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Alternatively, I could just a single &lt;em&gt;&amp;lt;if&amp;gt;&lt;/em&gt; task to do the same sort of check.&lt;/p&gt;

&lt;div class="geshifilter"&gt;&lt;table class="xml geshifilter-xml" style="font-family:monospace;"&gt;&lt;tbody&gt;&lt;tr class="li1"&gt;&lt;td style="width:1px;text-align:right;margin:0;padding:0 2px;vertical-align:top;"&gt;&lt;pre style="font-family: monospace; font-weight: normal; font-style: normal"&gt;1
2
3
4
5
6
7
8
9
&lt;/pre&gt;&lt;/td&gt;&lt;td style="font-family: monospace; font-weight: normal; font-style: normal"&gt;&lt;pre style="font-family: monospace; font-weight: normal; font-style: normal"&gt;&lt;span style="color: #808080; font-style: italic;"&gt;&amp;lt;!-- Fail the build if the user is not root --&amp;gt;&lt;/span&gt;
&lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;if&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&amp;nbsp; &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;not&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;equals&lt;/span&gt; &lt;span style="color: #000066;"&gt;arg1&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;&amp;quot;${currentuser}&amp;quot;&lt;/span&gt; &lt;span style="color: #000066;"&gt;arg2&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;&amp;quot;root&amp;quot;&lt;/span&gt; &lt;span style="color: #000000; font-weight: bold;"&gt;/&amp;gt;&lt;/span&gt;&lt;/span&gt;
&amp;nbsp; &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;/not&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&amp;nbsp; &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;then&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;fail&lt;/span&gt; &lt;span style="color: #000066;"&gt;message&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;&amp;quot;You must run this build as sudo!&amp;quot;&lt;/span&gt; &lt;span style="color: #000000; font-weight: bold;"&gt;/&amp;gt;&lt;/span&gt;&lt;/span&gt;
&amp;nbsp; &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;/then&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;/if&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The &lt;em&gt;&amp;lt;fail&amp;gt;&lt;/em&gt; task here will just stop all execution of the script and show that the build has failed to the user, along with the message text.&lt;/p&gt;

&lt;p&gt;I could have left if there, but I wanted to take it further by creating a custom Phing task that will then create a property called 'issudo' that will tell me if the user has used sudo to run the build script. This involves creating a PHP class that checks the sudo status using PHP, which should therefore work on any platform. To integrate with Phing you just need to create a class that extends &lt;em&gt;Task&lt;/em&gt; and implements a method called &lt;em&gt;main()&lt;/em&gt;. The &lt;em&gt;main()&lt;/em&gt; method is executed when the object is used so it is within this method that the work needs to be done to check the user and create the property.&lt;/p&gt;

&lt;p&gt;To find out if a user has run a PHP script using sudo is quite easy on Linux hosts. The $_SERVER array will contain a few elements that are to do with who the origin user was and what command was run when used on the command line. If we look for the presence of &lt;em&gt;SUDO_COMMAND&lt;/em&gt; within the $_SERVER array then we can be pretty sure that the user is running the script as a sudo user. Again, if you have more information on how this works on Windows I'd be happy to hear from you.&lt;/p&gt;

&lt;p&gt;To set a property that can be used in the Phing build file the &lt;em&gt;$this-&gt;project-&gt;setProperty()&lt;/em&gt; method needs to be called. This will create a property with a given name and takes two parameters.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;$name&lt;/strong&gt; : The name of property to set.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;$value&lt;/strong&gt; : The value of the property. If the property name already exists then this will overrite the previous value.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The following class was placed into the same directory as my build script with the name of IsSudo.php. The call to &lt;em&gt;log()&lt;/em&gt; just prints out a message to the terminal so that we can see that the class has been run.&lt;/p&gt;

&lt;div class="geshifilter"&gt;&lt;table class="php geshifilter-php" style="font-family:monospace;"&gt;&lt;tbody&gt;&lt;tr class="li1"&gt;&lt;td style="width:1px;text-align:right;margin:0;padding:0 2px;vertical-align:top;"&gt;&lt;pre style="font-family: monospace; font-weight: normal; font-style: normal"&gt;1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
&lt;/pre&gt;&lt;/td&gt;&lt;td style="font-family: monospace; font-weight: normal; font-style: normal"&gt;&lt;pre style="font-family: monospace; font-weight: normal; font-style: normal"&gt;&lt;span style="color: #009933; font-style: italic;"&gt;/**&lt;/span&gt;
&lt;span style="color: #009933; font-style: italic;"&gt;&amp;nbsp;* Sets a phing property called 'issudo' that can be used to see if the user has root privilages.&lt;/span&gt;
&lt;span style="color: #009933; font-style: italic;"&gt;&amp;nbsp;*&lt;/span&gt;
&lt;span style="color: #009933; font-style: italic;"&gt;&amp;nbsp;*/&lt;/span&gt;
&lt;span style="color: #000000; font-weight: bold;"&gt;class&lt;/span&gt; IsSudo &lt;span style="color: #000000; font-weight: bold;"&gt;extends&lt;/span&gt; Task &lt;span style="color: #009900;"&gt;&amp;#123;&lt;/span&gt;
&amp;nbsp;
&amp;nbsp; &lt;span style="color: #009933; font-style: italic;"&gt;/**&lt;/span&gt;
&lt;span style="color: #009933; font-style: italic;"&gt;&amp;nbsp; &amp;nbsp;* Set the property.&lt;/span&gt;
&lt;span style="color: #009933; font-style: italic;"&gt;&amp;nbsp; &amp;nbsp;*/&lt;/span&gt;
&amp;nbsp; &lt;span style="color: #000000; font-weight: bold;"&gt;public&lt;/span&gt; &lt;span style="color: #000000; font-weight: bold;"&gt;function&lt;/span&gt; main&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt; &lt;span style="color: #009900;"&gt;&amp;#123;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &lt;span style="color: #000088;"&gt;$issudo&lt;/span&gt; &lt;span style="color: #339933;"&gt;=&lt;/span&gt; &lt;span style="color: #009900; font-weight: bold;"&gt;false&lt;/span&gt;&lt;span style="color: #339933;"&gt;;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; 
&amp;nbsp; &amp;nbsp; &lt;span style="color: #000088;"&gt;$this&lt;/span&gt;&lt;span style="color: #339933;"&gt;-&amp;gt;&lt;/span&gt;&lt;a href="http://www.php.net/log"&gt;&lt;span style="color: #990000;"&gt;log&lt;/span&gt;&lt;/a&gt;&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #0000ff;"&gt;'Determining sudo.'&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt;&lt;span style="color: #339933;"&gt;;&lt;/span&gt;
&amp;nbsp;
&amp;nbsp; &amp;nbsp; &lt;span style="color: #b1b100;"&gt;if&lt;/span&gt; &lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;&lt;a href="http://www.php.net/isset"&gt;&lt;span style="color: #990000;"&gt;isset&lt;/span&gt;&lt;/a&gt;&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #000088;"&gt;$_SERVER&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#91;&lt;/span&gt;&lt;span style="color: #0000ff;"&gt;'SUDO_COMMAND'&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#93;&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt; &lt;span style="color: #009900;"&gt;&amp;#123;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style="color: #000088;"&gt;$issudo&lt;/span&gt; &lt;span style="color: #339933;"&gt;=&lt;/span&gt; &lt;span style="color: #009900; font-weight: bold;"&gt;true&lt;/span&gt;&lt;span style="color: #339933;"&gt;;&lt;/span&gt; 
&amp;nbsp; &amp;nbsp; &lt;span style="color: #009900;"&gt;&amp;#125;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; 
&amp;nbsp; &amp;nbsp; &lt;span style="color: #000088;"&gt;$this&lt;/span&gt;&lt;span style="color: #339933;"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: #004000;"&gt;project&lt;/span&gt;&lt;span style="color: #339933;"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: #004000;"&gt;setProperty&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #0000ff;"&gt;'issudo'&lt;/span&gt;&lt;span style="color: #339933;"&gt;,&lt;/span&gt; &lt;span style="color: #000088;"&gt;$issudo&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt;&lt;span style="color: #339933;"&gt;;&lt;/span&gt;
&amp;nbsp; &lt;span style="color: #009900;"&gt;&amp;#125;&lt;/span&gt;
&lt;span style="color: #009900;"&gt;&amp;#125;&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The final step here is to tell Phing that we have a class that we want to be used. This is done using the &lt;em&gt;&amp;lt;taskdef&amp;gt;&lt;/em&gt; task. In it's simplest terms this task takes two parameters of the name of the task we want to create and the name of the class that contains the code behind the task. The following code creates a task called &lt;em&gt;&amp;lt;issudo&amp;gt;&lt;/em&gt;, which can be found in the class &lt;em&gt;IsSudo&lt;/em&gt;.&lt;/p&gt;

&lt;span class="geshifilter"&gt;&lt;code class="xml geshifilter-xml"&gt;&lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;taskdef&lt;/span&gt; &lt;span style="color: #000066;"&gt;name&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;&amp;quot;issudo&amp;quot;&lt;/span&gt; &lt;span style="color: #000066;"&gt;classname&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;&amp;quot;IsSudo&amp;quot;&lt;/span&gt; &lt;span style="color: #000000; font-weight: bold;"&gt;/&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/span&gt;

&lt;p&gt;This allows us to use the custom &lt;em&gt;&amp;lt;issudo&amp;gt;&lt;/em&gt; task that creates a property called &lt;em&gt;${issudo}&lt;/em&gt;, which can then be used to see if the user has run the script as sudo.&lt;/p&gt;

&lt;div class="geshifilter"&gt;&lt;table class="xml geshifilter-xml" style="font-family:monospace;"&gt;&lt;tbody&gt;&lt;tr class="li1"&gt;&lt;td style="width:1px;text-align:right;margin:0;padding:0 2px;vertical-align:top;"&gt;&lt;pre style="font-family: monospace; font-weight: normal; font-style: normal"&gt;1
2
3
4
5
6
7
8
9
10
11
12
&lt;/pre&gt;&lt;/td&gt;&lt;td style="font-family: monospace; font-weight: normal; font-style: normal"&gt;&lt;pre style="font-family: monospace; font-weight: normal; font-style: normal"&gt;&lt;span style="color: #808080; font-style: italic;"&gt;&amp;lt;!-- Create issudo property --&amp;gt;&lt;/span&gt;
&lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;issudo&lt;/span&gt; &lt;span style="color: #000000; font-weight: bold;"&gt;/&amp;gt;&lt;/span&gt;&lt;/span&gt;
&amp;nbsp;
&lt;span style="color: #808080; font-style: italic;"&gt;&amp;lt;!-- Check issudo --&amp;gt;&lt;/span&gt;
&lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;if&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&amp;nbsp; &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;not&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;istrue&lt;/span&gt; &lt;span style="color: #000066;"&gt;value&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;&amp;quot;${issudo}&amp;quot;&lt;/span&gt; &lt;span style="color: #000000; font-weight: bold;"&gt;/&amp;gt;&lt;/span&gt;&lt;/span&gt;
&amp;nbsp; &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;/not&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&amp;nbsp; &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;then&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;fail&lt;/span&gt; &lt;span style="color: #000066;"&gt;message&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;&amp;quot;You must run this build as sudo!&amp;quot;&lt;/span&gt; &lt;span style="color: #000000; font-weight: bold;"&gt;/&amp;gt;&lt;/span&gt;&lt;/span&gt;
&amp;nbsp; &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;/then&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;/if&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;This works as intended and will stop the execution of the build file if I've forgotten to run it via sudo.&lt;/p&gt;

&lt;p&gt;I did think that it might be possible to integrate with the comparison operators within Phing and create an element that works in much the same way as the &lt;em&gt;&amp;lt;istrue&amp;gt;&lt;/em&gt; or &lt;em&gt;&amp;lt;isset&amp;gt;&lt;/em&gt; tasks. After some investigation I don't think there is a way of doing this without altering the Phing source code.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Update&lt;/strong&gt; : &lt;a href="http://www.twitter.com/beryllium9" title="Beryllium9"&gt;@Beryllium9&lt;/a&gt; on twitter suggested that a good way of forcing a sudo check is to create a single sudo target and then require that as a prerequisite to running any sudo requiring targets. The way to do this is to use the &lt;em&gt;depends&lt;/em&gt; property of the &lt;em&gt;target&lt;/em&gt; element. Adding a &lt;em&gt;depends&lt;/em&gt; attribute to the &lt;em&gt;target&lt;/em&gt; element will make Phing run the named &lt;em&gt;target&lt;/em&gt; first before running the current &lt;em&gt;target&lt;/em&gt;. This is the case even if the default &lt;em&gt;target&lt;/em&gt; is being called.&lt;/p&gt;

&lt;p&gt;As an example the following build file will only run the &lt;strong&gt;main&lt;/strong&gt; &lt;em&gt;target&lt;/em&gt; if it has been run using sudo.&lt;/p&gt;

&lt;div class="geshifilter"&gt;&lt;table class="xml geshifilter-xml" style="font-family:monospace;"&gt;&lt;tbody&gt;&lt;tr class="li1"&gt;&lt;td style="width:1px;text-align:right;margin:0;padding:0 2px;vertical-align:top;"&gt;&lt;pre style="font-family: monospace; font-weight: normal; font-style: normal"&gt;1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
&lt;/pre&gt;&lt;/td&gt;&lt;td style="font-family: monospace; font-weight: normal; font-style: normal"&gt;&lt;pre style="font-family: monospace; font-weight: normal; font-style: normal"&gt;&lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;?xml&lt;/span&gt; &lt;span style="color: #000066;"&gt;version&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;&amp;quot;1.0&amp;quot;&lt;/span&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;?&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;project&lt;/span&gt; &lt;span style="color: #000066;"&gt;name&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;&amp;quot;sudotest&amp;quot;&lt;/span&gt; &lt;span style="color: #000066;"&gt;default&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;&amp;quot;main&amp;quot;&lt;/span&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&amp;nbsp;
&amp;nbsp; &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;target&lt;/span&gt; &lt;span style="color: #000066;"&gt;name&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;&amp;quot;sudotest&amp;quot;&lt;/span&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &lt;span style="color: #808080; font-style: italic;"&gt;&amp;lt;!-- Create the currentuser property --&amp;gt;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;exec&lt;/span&gt; &lt;span style="color: #000066;"&gt;command&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;&amp;quot;whoami&amp;quot;&lt;/span&gt; &lt;span style="color: #000066;"&gt;outputProperty&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;&amp;quot;currentuser&amp;quot;&lt;/span&gt; &lt;span style="color: #000000; font-weight: bold;"&gt;/&amp;gt;&lt;/span&gt;&lt;/span&gt;
&amp;nbsp;
&amp;nbsp; &amp;nbsp; &lt;span style="color: #808080; font-style: italic;"&gt;&amp;lt;!-- Set the 'issudo' property to be true if the user is root --&amp;gt;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;condition&lt;/span&gt; &lt;span style="color: #000066;"&gt;property&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;&amp;quot;issudo&amp;quot;&lt;/span&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;equals&lt;/span&gt; &lt;span style="color: #000066;"&gt;arg1&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;&amp;quot;${currentuser}&amp;quot;&lt;/span&gt; &lt;span style="color: #000066;"&gt;arg2&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;&amp;quot;root&amp;quot;&lt;/span&gt; &lt;span style="color: #000000; font-weight: bold;"&gt;/&amp;gt;&lt;/span&gt;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;/condition&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&amp;nbsp;
&amp;nbsp; &amp;nbsp; &lt;span style="color: #808080; font-style: italic;"&gt;&amp;lt;!-- Fail the build if the user is not root --&amp;gt;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;if&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;not&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;equals&lt;/span&gt; &lt;span style="color: #000066;"&gt;arg1&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;&amp;quot;${issudo}&amp;quot;&lt;/span&gt; &lt;span style="color: #000066;"&gt;arg2&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;&amp;quot;true&amp;quot;&lt;/span&gt; &lt;span style="color: #000000; font-weight: bold;"&gt;/&amp;gt;&lt;/span&gt;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;/not&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;then&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;fail&lt;/span&gt; &lt;span style="color: #000066;"&gt;message&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;&amp;quot;You must run this build as sudo!&amp;quot;&lt;/span&gt; &lt;span style="color: #000000; font-weight: bold;"&gt;/&amp;gt;&lt;/span&gt;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;/then&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;/if&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&amp;nbsp; &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;/target&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&amp;nbsp;
&amp;nbsp; &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;target&lt;/span&gt; &lt;span style="color: #000066;"&gt;name&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;&amp;quot;main&amp;quot;&lt;/span&gt; &lt;span style="color: #000066;"&gt;depends&lt;/span&gt;=&lt;span style="color: #ff0000;"&gt;&amp;quot;sudotest&amp;quot;&lt;/span&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;echo&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;Running main target&lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;/echo&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&amp;nbsp; &lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;/target&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;
&lt;span style="color: #009900;"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;/project&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="field field-name-field-blogcategory field-type-taxonomy-term-reference field-label-above"&gt;&lt;div class="field-label"&gt;Category:&amp;nbsp;&lt;/div&gt;&lt;div class="field-items"&gt;&lt;div class="field-item even"&gt;&lt;a href="/blog/category/phing"&gt;Phing&lt;/a&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="field field-name-field-blogtags field-type-taxonomy-term-reference field-label-above"&gt;&lt;div class="field-label"&gt;Tags:&amp;nbsp;&lt;/div&gt;&lt;div class="field-items"&gt;&lt;div class="field-item even"&gt;&lt;a href="/category/tags/phing"&gt;Phing&lt;/a&gt;&lt;/div&gt;&lt;div class="field-item odd"&gt;&lt;a href="/category/tags/target"&gt;target&lt;/a&gt;&lt;/div&gt;&lt;div class="field-item even"&gt;&lt;a href="/category/tags/sudo"&gt;sudo&lt;/a&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/hashbangcode/~4/ErCH7YOPmOM" height="1" width="1"/&gt;</description>
 <pubDate>Wed, 10 Apr 2013 19:50:18 +0000</pubDate>
 <dc:creator>philipnorton42</dc:creator>
 <guid isPermaLink="false">710 at http://www.hashbangcode.com</guid>
 <comments>http://www.hashbangcode.com/blog/detecting-sudo-user-phing-710.html#comments</comments>
<feedburner:origLink>http://www.hashbangcode.com/blog/detecting-sudo-user-phing-710.html</feedburner:origLink></item>
<item>
 <title>DrupalCampNW 2012 Videos</title>
 <link>http://feedproxy.google.com/~r/hashbangcode/~3/teOBD92hjsk/drupalcampnw-2012-videos-709.html</link>
 <description>&lt;div class="field field-name-body field-type-text-with-summary field-label-hidden"&gt;&lt;div class="field-items"&gt;&lt;div class="field-item even"&gt;&lt;p&gt;During &lt;a href="http://www.hashbangcode.com/blog/drupalcampnw-2012-review-682.html" title="DrupalCampNW 2012"&gt;DrupalCampNW 2012&lt;/a&gt; last November the company we hired to record the sessions also spent some time interviewing the people who attended. From these interviews we were able to create three videos, two from the main event and one about what Drupal means to different people. These videos are now complete so I'm posting them here so everyone can see. I think they came out really well.&lt;/p&gt;

&lt;p&gt;I would also like to thank &lt;a href="http://association.drupal.org" title="Drupal Association"&gt;Drupal Association&lt;/a&gt;, who very generously financed the creation and production of these videos.&lt;/p&gt;

&lt;h2&gt;DrupalCamp North West Business Day&lt;/h2&gt;
&lt;iframe src="http://player.vimeo.com/video/61755351" width="500" height="281" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen&gt;&lt;/iframe&gt; &lt;p&gt;&lt;a href="http://vimeo.com/61755351"&gt;DrupalCamp North West Business Day&lt;/a&gt; from &lt;a href="http://vimeo.com/user14852728"&gt;DrupalCamp NW&lt;/a&gt; on &lt;a href="http://vimeo.com"&gt;Vimeo&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;DrupalCamp North West 2012&lt;/h2&gt;
&lt;iframe src="http://player.vimeo.com/video/61758649" width="500" height="281" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen&gt;&lt;/iframe&gt; &lt;p&gt;&lt;a href="http://vimeo.com/61758649"&gt;DrupalCamp North West 2012&lt;/a&gt; from &lt;a href="http://vimeo.com/user14852728"&gt;DrupalCamp NW&lt;/a&gt; on &lt;a href="http://vimeo.com"&gt;Vimeo&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;What Does Drupal Mean To Me?&lt;/h2&gt;
&lt;iframe src="http://player.vimeo.com/video/61905399" width="500" height="281" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen&gt;&lt;/iframe&gt; &lt;p&gt;&lt;a href="http://vimeo.com/61905399"&gt;What does Drupal mean to me?&lt;/a&gt; from &lt;a href="http://vimeo.com/user14852728"&gt;DrupalCamp NW&lt;/a&gt; on &lt;a href="http://vimeo.com"&gt;Vimeo&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;To me, Drupal means more than just a CMS or a framework. The core of Drupal is more like a starting block to a whole world of modules, themes, technologies, practices, services and people that make Drupal more than the sum of its parts. The community of people behind Drupal are amazing and some incredible work has been done to make Drupal the enterprise class software it is today. The future of Drupal looks good, especially with the amazing work going into the next version of Drupal.&lt;/p&gt;

&lt;p&gt;When I first started using Drupal I had just started a new job and was dropped into the deep end of a half finished Drupal 6 project that needed to be completed. After working through the project for a couple of weeks I found out about the local user Drupal group in Manchester (NWDUG). After just a few minutes of talking through the issues I was having I went away with some ideas about how to solve them and was able to complete the project. I now help to run NWDUG and try to do the same thing for other people either by directly helping them or by pointing them to the correct resource. If you have a local user group then I urge you to get involved, you will learn so much just by being there that it will make it worth the trip. If you don't have a local user group then maybe you can start one? All you need is a venue and a couple of other people and you can at least get together once a month and talk about your Drupal problems.&lt;/p&gt;

&lt;p&gt;What does Drupal mean to you? Post a comment below and tell us.&lt;/p&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="field field-name-field-blogcategory field-type-taxonomy-term-reference field-label-above"&gt;&lt;div class="field-label"&gt;Category:&amp;nbsp;&lt;/div&gt;&lt;div class="field-items"&gt;&lt;div class="field-item even"&gt;&lt;a href="/blog/category/drupal"&gt;Drupal&lt;/a&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="field field-name-field-blogtags field-type-taxonomy-term-reference field-label-above"&gt;&lt;div class="field-label"&gt;Tags:&amp;nbsp;&lt;/div&gt;&lt;div class="field-items"&gt;&lt;div class="field-item even"&gt;&lt;a href="/blog/tag/drupalcampnw"&gt;DrupalCampNW&lt;/a&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/hashbangcode/~4/teOBD92hjsk" height="1" width="1"/&gt;</description>
 <pubDate>Sun, 31 Mar 2013 12:42:02 +0000</pubDate>
 <dc:creator>philipnorton42</dc:creator>
 <guid isPermaLink="false">709 at http://www.hashbangcode.com</guid>
 <comments>http://www.hashbangcode.com/blog/drupalcampnw-2012-videos-709.html#comments</comments>
<feedburner:origLink>http://www.hashbangcode.com/blog/drupalcampnw-2012-videos-709.html</feedburner:origLink></item>
<item>
 <title>Drupal 7: Setting Default Value For A Field</title>
 <link>http://feedproxy.google.com/~r/hashbangcode/~3/tdrvuACzQss/drupal-7-setting-default-value-field-708.html</link>
 <description>&lt;div class="field field-name-body field-type-text-with-summary field-label-hidden"&gt;&lt;div class="field-items"&gt;&lt;div class="field-item even"&gt;&lt;p&gt;I was recently working on a module that contained a content type as a feature. When the module was enabled the &lt;em&gt;hook_install()&lt;/em&gt; hook set up a collection of taxonomy terms which were used within the content type as a field. Whilst testing this out I realised that although the terms were installed correctly the default value of the field changed depending on which system the module was installed on. The reason for this was that the term ID was being used to pull out the default term from the database, which is the normal behaviour in Drupal. The problem here was that if a term the ID of the term was different (because one had been added) then this had a knock-on effect of changing the default value of the field.&lt;/p&gt;

&lt;p&gt;To get around this I needed a way of setting the default value of the field based on a given taxonomy term. After some research and reading of the Drupal source code I found a way to programatically set the default value of a taxonomy field.&lt;/p&gt;

&lt;p&gt;Before getting into that there is a little bit of setup to do beforehand. As I said before, the module I was working on contained a feature that held a content type and a few fields, one of which was a taxonomy reference. In order to set a default value on a taxonomy field on a content type I needed to ensure that all of the components are correctly assembled first. In the &lt;em&gt;hook_install()&lt;/em&gt; hook of the module I added a call to features_revert() so that I could be sure that the rest of the actions I was about to complete would work correctly. The &lt;em&gt;features_revert()&lt;/em&gt; function works like clicking the 'Revert Features' button in the features module administration section and will force the given arguments to be reverted. In order for this to work during the install hook the correct feature files also need to be included and so the start of the hook includes the features files involved with nodes, fields and taxonomies.&lt;/p&gt;

&lt;div class="geshifilter"&gt;&lt;table class="php geshifilter-php" style="font-family:monospace;"&gt;&lt;tbody&gt;&lt;tr class="li1"&gt;&lt;td style="width:1px;text-align:right;margin:0;padding:0 2px;vertical-align:top;"&gt;&lt;pre style="font-family: monospace; font-weight: normal; font-style: normal"&gt;1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
&lt;/pre&gt;&lt;/td&gt;&lt;td style="font-family: monospace; font-weight: normal; font-style: normal"&gt;&lt;pre style="font-family: monospace; font-weight: normal; font-style: normal"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;function&lt;/span&gt; my_module_install&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt; &lt;span style="color: #009900;"&gt;&amp;#123;&lt;/span&gt;
&amp;nbsp; &lt;span style="color: #666666; font-style: italic;"&gt;// Ensure the correct features files are included&lt;/span&gt;
&amp;nbsp; module_load_include&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #0000ff;"&gt;'inc'&lt;/span&gt;&lt;span style="color: #339933;"&gt;,&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;'my_module'&lt;/span&gt;&lt;span style="color: #339933;"&gt;,&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;'my_module.features'&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt;&lt;span style="color: #339933;"&gt;;&lt;/span&gt;
&amp;nbsp; module_load_include&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #0000ff;"&gt;'inc'&lt;/span&gt;&lt;span style="color: #339933;"&gt;,&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;'my_module'&lt;/span&gt;&lt;span style="color: #339933;"&gt;,&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;'my_module.features.field'&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt;&lt;span style="color: #339933;"&gt;;&lt;/span&gt;
&amp;nbsp; module_load_include&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #0000ff;"&gt;'inc'&lt;/span&gt;&lt;span style="color: #339933;"&gt;,&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;'my_module'&lt;/span&gt;&lt;span style="color: #339933;"&gt;,&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;'my_module.features.taxonomy'&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt;&lt;span style="color: #339933;"&gt;;&lt;/span&gt;
&amp;nbsp;
&amp;nbsp; &lt;span style="color: #666666; font-style: italic;"&gt;// Ensure that the content type, fields and taxonomy vocabularies are actually there.&lt;/span&gt;
&amp;nbsp; features_revert&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &lt;a href="http://www.php.net/array"&gt;&lt;span style="color: #990000;"&gt;array&lt;/span&gt;&lt;/a&gt;&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style="color: #0000ff;"&gt;'my_module'&lt;/span&gt; &lt;span style="color: #339933;"&gt;=&amp;gt;&lt;/span&gt; &lt;a href="http://www.php.net/array"&gt;&lt;span style="color: #990000;"&gt;array&lt;/span&gt;&lt;/a&gt;&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style="color: #0000ff;"&gt;'taxonomy'&lt;/span&gt;&lt;span style="color: #339933;"&gt;,&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style="color: #0000ff;"&gt;'node'&lt;/span&gt;&lt;span style="color: #339933;"&gt;,&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style="color: #0000ff;"&gt;'field'&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &amp;nbsp; &lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt;
&amp;nbsp; &amp;nbsp; &lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt;
&amp;nbsp; &lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt;&lt;span style="color: #339933;"&gt;;&lt;/span&gt;
&amp;nbsp;
&lt;span style="color: #009900;"&gt;&amp;#125;&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;As a side note it may have been better to run this code in a different hook (e.g., &lt;em&gt;hook_enable()&lt;/em&gt; or &lt;em&gt;hook_modules_enabled()&lt;/em&gt;) so that the &lt;em&gt;features_revert()&lt;/em&gt; function isn't needed. I have found, however, that you can't rely on features being fully implemented in any of the installing and enabling hooks. Forcing just the components you need to use in a feature is a good way of ensuring that everything you need is ready to go.&lt;/p&gt;

&lt;p&gt;The next step was to load the vocabulary and add the default terms to it. The following code adds six terms to the vocabulary called 'task_status', which is also included in the install hook.&lt;/p&gt;

&lt;div class="geshifilter"&gt;&lt;table class="php geshifilter-php" style="font-family:monospace;"&gt;&lt;tbody&gt;&lt;tr class="li1"&gt;&lt;td style="width:1px;text-align:right;margin:0;padding:0 2px;vertical-align:top;"&gt;&lt;pre style="font-family: monospace; font-weight: normal; font-style: normal"&gt;1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
&lt;/pre&gt;&lt;/td&gt;&lt;td style="font-family: monospace; font-weight: normal; font-style: normal"&gt;&lt;pre style="font-family: monospace; font-weight: normal; font-style: normal"&gt;&lt;span style="color: #666666; font-style: italic;"&gt;// Set up 'Status' vocabulary terms.&lt;/span&gt;
&lt;span style="color: #000088;"&gt;$task_status_vocab&lt;/span&gt; &lt;span style="color: #339933;"&gt;=&lt;/span&gt; taxonomy_vocabulary_machine_name_load&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #0000ff;"&gt;'task_status'&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt;&lt;span style="color: #339933;"&gt;;&lt;/span&gt;
&lt;span style="color: #000088;"&gt;$statuses&lt;/span&gt; &lt;span style="color: #339933;"&gt;=&lt;/span&gt; &lt;a href="http://www.php.net/array"&gt;&lt;span style="color: #990000;"&gt;array&lt;/span&gt;&lt;/a&gt;&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;
&amp;nbsp; &lt;span style="color: #0000ff;"&gt;'New'&lt;/span&gt;&lt;span style="color: #339933;"&gt;,&lt;/span&gt;
&amp;nbsp; &lt;span style="color: #0000ff;"&gt;'Being Worked On'&lt;/span&gt;&lt;span style="color: #339933;"&gt;,&lt;/span&gt;
&amp;nbsp; &lt;span style="color: #0000ff;"&gt;'Postponed'&lt;/span&gt;&lt;span style="color: #339933;"&gt;,&lt;/span&gt;
&amp;nbsp; &lt;span style="color: #0000ff;"&gt;'Failed QA/Testing'&lt;/span&gt;&lt;span style="color: #339933;"&gt;,&lt;/span&gt;
&amp;nbsp; &lt;span style="color: #0000ff;"&gt;'Cancelled'&lt;/span&gt;&lt;span style="color: #339933;"&gt;,&lt;/span&gt;
&amp;nbsp; &lt;span style="color: #0000ff;"&gt;'Ready For QA/Testing'&lt;/span&gt;
&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt;&lt;span style="color: #339933;"&gt;;&lt;/span&gt;
&lt;span style="color: #b1b100;"&gt;foreach&lt;/span&gt; &lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #000088;"&gt;$statuses&lt;/span&gt; &lt;span style="color: #b1b100;"&gt;as&lt;/span&gt; &lt;span style="color: #000088;"&gt;$status&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt; &lt;span style="color: #009900;"&gt;&amp;#123;&lt;/span&gt;
&amp;nbsp; &lt;span style="color: #000088;"&gt;$term&lt;/span&gt; &lt;span style="color: #339933;"&gt;=&lt;/span&gt; &lt;span style="color: #000000; font-weight: bold;"&gt;new&lt;/span&gt; stdClass&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt;&lt;span style="color: #339933;"&gt;;&lt;/span&gt;
&amp;nbsp; &lt;span style="color: #000088;"&gt;$term&lt;/span&gt;&lt;span style="color: #339933;"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: #004000;"&gt;name&lt;/span&gt; &lt;span style="color: #339933;"&gt;=&lt;/span&gt; &lt;span style="color: #000088;"&gt;$status&lt;/span&gt;&lt;span style="color: #339933;"&gt;;&lt;/span&gt;
&amp;nbsp; &lt;span style="color: #000088;"&gt;$term&lt;/span&gt;&lt;span style="color: #339933;"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: #004000;"&gt;vid&lt;/span&gt; &lt;span style="color: #339933;"&gt;=&lt;/span&gt; &lt;span style="color: #000088;"&gt;$task_status_vocab&lt;/span&gt;&lt;span style="color: #339933;"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: #004000;"&gt;vid&lt;/span&gt;&lt;span style="color: #339933;"&gt;;&lt;/span&gt;
&amp;nbsp; taxonomy_term_save&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #000088;"&gt;$term&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt;&lt;span style="color: #339933;"&gt;;&lt;/span&gt;
&lt;span style="color: #009900;"&gt;&amp;#125;&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Now that everything is set up the default field value can be set. What we need to do first is to run a database query to pick out the correct term so that we can set the taxonomy field to the correct value.&lt;/p&gt;

&lt;div class="geshifilter"&gt;&lt;table class="php geshifilter-php" style="font-family:monospace;"&gt;&lt;tbody&gt;&lt;tr class="li1"&gt;&lt;td style="width:1px;text-align:right;margin:0;padding:0 2px;vertical-align:top;"&gt;&lt;pre style="font-family: monospace; font-weight: normal; font-style: normal"&gt;1
2
3
4
5
6
7
&lt;/pre&gt;&lt;/td&gt;&lt;td style="font-family: monospace; font-weight: normal; font-style: normal"&gt;&lt;pre style="font-family: monospace; font-weight: normal; font-style: normal"&gt;&lt;span style="color: #666666; font-style: italic;"&gt;// Get the tid for the default status&lt;/span&gt;
&lt;span style="color: #000088;"&gt;$term&lt;/span&gt; &lt;span style="color: #339933;"&gt;=&lt;/span&gt; db_select&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #0000ff;"&gt;'taxonomy_term_data'&lt;/span&gt;&lt;span style="color: #339933;"&gt;,&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;'td'&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt;
&amp;nbsp; &lt;span style="color: #339933;"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: #004000;"&gt;fields&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #0000ff;"&gt;'td'&lt;/span&gt;&lt;span style="color: #339933;"&gt;,&lt;/span&gt; &lt;a href="http://www.php.net/array"&gt;&lt;span style="color: #990000;"&gt;array&lt;/span&gt;&lt;/a&gt;&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #0000ff;"&gt;'tid'&lt;/span&gt;&lt;span style="color: #339933;"&gt;,&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;'name'&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt;
&amp;nbsp; &lt;span style="color: #339933;"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: #004000;"&gt;condition&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #0000ff;"&gt;'name'&lt;/span&gt;&lt;span style="color: #339933;"&gt;,&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;'New'&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt;
&amp;nbsp; &lt;span style="color: #339933;"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: #004000;"&gt;condition&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #0000ff;"&gt;'vid'&lt;/span&gt;&lt;span style="color: #339933;"&gt;,&lt;/span&gt; &lt;span style="color: #000088;"&gt;$task_status_vocab&lt;/span&gt;&lt;span style="color: #339933;"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: #004000;"&gt;vid&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt;
&amp;nbsp; &lt;span style="color: #339933;"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: #004000;"&gt;execute&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt;
&amp;nbsp; &lt;span style="color: #339933;"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: #004000;"&gt;fetchObject&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt;&lt;span style="color: #339933;"&gt;;&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Now we need to take the term ID we just found and set it to be the default for the field, which involves three steps. The first step is to get an instance of the field using the function &lt;em&gt;field_read_instance()&lt;/em&gt; that we can modify. The second step is to then modify the field with the new default value of the taxonomy term ID (referred to as tid). The final step is to update the field instance with the new field information using the &lt;em&gt;field_update_instance()&lt;/em&gt; function.&lt;/p&gt;

&lt;div class="geshifilter"&gt;&lt;table class="php geshifilter-php" style="font-family:monospace;"&gt;&lt;tbody&gt;&lt;tr class="li1"&gt;&lt;td style="width:1px;text-align:right;margin:0;padding:0 2px;vertical-align:top;"&gt;&lt;pre style="font-family: monospace; font-weight: normal; font-style: normal"&gt;1
2
3
4
5
6
7
8
9
10
&lt;/pre&gt;&lt;/td&gt;&lt;td style="font-family: monospace; font-weight: normal; font-style: normal"&gt;&lt;pre style="font-family: monospace; font-weight: normal; font-style: normal"&gt;&lt;span style="color: #666666; font-style: italic;"&gt;// Set the default taxonomy term for the taxonomy field.&lt;/span&gt;
&amp;nbsp;
&lt;span style="color: #666666; font-style: italic;"&gt;// Get an instance of the field that we can modify&lt;/span&gt;
&lt;span style="color: #000088;"&gt;$instance&lt;/span&gt; &lt;span style="color: #339933;"&gt;=&lt;/span&gt; field_read_instance&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #0000ff;"&gt;'node'&lt;/span&gt;&lt;span style="color: #339933;"&gt;,&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;'field_task_status'&lt;/span&gt;&lt;span style="color: #339933;"&gt;,&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;'task'&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt;&lt;span style="color: #339933;"&gt;;&lt;/span&gt;
&amp;nbsp;
&lt;span style="color: #666666; font-style: italic;"&gt;// Update the default value of the field&lt;/span&gt;
&lt;span style="color: #000088;"&gt;$instance&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#91;&lt;/span&gt;&lt;span style="color: #0000ff;"&gt;'default_value'&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#93;&lt;/span&gt; &lt;span style="color: #339933;"&gt;=&lt;/span&gt; &lt;a href="http://www.php.net/array"&gt;&lt;span style="color: #990000;"&gt;array&lt;/span&gt;&lt;/a&gt;&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;&lt;a href="http://www.php.net/array"&gt;&lt;span style="color: #990000;"&gt;array&lt;/span&gt;&lt;/a&gt;&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #0000ff;"&gt;'tid'&lt;/span&gt; &lt;span style="color: #339933;"&gt;=&amp;gt;&lt;/span&gt; &lt;span style="color: #000088;"&gt;$term&lt;/span&gt;&lt;span style="color: #339933;"&gt;-&amp;gt;&lt;/span&gt;&lt;span style="color: #004000;"&gt;tid&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt;&lt;span style="color: #339933;"&gt;;&lt;/span&gt;
&amp;nbsp;
&lt;span style="color: #666666; font-style: italic;"&gt;// Save the field back to the database.&lt;/span&gt;
field_update_instance&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #000088;"&gt;$instance&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt;&lt;span style="color: #339933;"&gt;;&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The only problem here is that once the default value has been updated the Feature will report itself as being overridden (which it is). This is unavoidable, but it's not something that will cause many problems. The one main problem is that if you ever need to revert the fields components of the feature again in the future then you'll need a way of updating the field with the correct default value. Even without Features this is a good trick to know for any fields.&lt;/p&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="field field-name-field-blogcategory field-type-taxonomy-term-reference field-label-above"&gt;&lt;div class="field-label"&gt;Category:&amp;nbsp;&lt;/div&gt;&lt;div class="field-items"&gt;&lt;div class="field-item even"&gt;&lt;a href="/blog/category/drupal"&gt;Drupal&lt;/a&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="field field-name-field-blogtags field-type-taxonomy-term-reference field-label-above"&gt;&lt;div class="field-label"&gt;Tags:&amp;nbsp;&lt;/div&gt;&lt;div class="field-items"&gt;&lt;div class="field-item even"&gt;&lt;a href="/category/tags/drupal"&gt;Drupal&lt;/a&gt;&lt;/div&gt;&lt;div class="field-item odd"&gt;&lt;a href="/category/tags/features"&gt;Features&lt;/a&gt;&lt;/div&gt;&lt;div class="field-item even"&gt;&lt;a href="/category/tags/field"&gt;field&lt;/a&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/hashbangcode/~4/tdrvuACzQss" height="1" width="1"/&gt;</description>
 <pubDate>Sat, 30 Mar 2013 11:48:31 +0000</pubDate>
 <dc:creator>philipnorton42</dc:creator>
 <guid isPermaLink="false">708 at http://www.hashbangcode.com</guid>
 <comments>http://www.hashbangcode.com/blog/drupal-7-setting-default-value-field-708.html#comments</comments>
<feedburner:origLink>http://www.hashbangcode.com/blog/drupal-7-setting-default-value-field-708.html</feedburner:origLink></item>
<item>
 <title>Drupal Camp London 2013</title>
 <link>http://feedproxy.google.com/~r/hashbangcode/~3/ApZf95hHb-E/drupal-camp-london-2013-704.html</link>
 <description>&lt;div class="field field-name-body field-type-text-with-summary field-label-hidden"&gt;&lt;div class="field-items"&gt;&lt;div class="field-item even"&gt;&lt;p&gt;I'm just letting all my neurons cool down after a fantastic Drupal Camp at City University, London.  These Drupal camps are getting bigger, better, and much more frequent in a really, feely, organic way.  They never feel like there is a sponsor driven sales push to use this service or that, moreover, it genuinely does feel like a community of like minded people, groups of people, willingly sharing their knowledge, ideas, and enthusiasm in what it is they are doing, using, or believe in.&lt;/p&gt;

&lt;p&gt;The keynote was by &lt;a href="http://2013.drupalcamplondon.co.uk/session/david-axmark-mysql-ab-co-founder"&gt;David Axmark&lt;/a&gt;, co-founder of MySQL AB, and one of the real players in the shaping of how the open source World evolved.  It was a very engaging talk covering the inception of MySQL, to it's $1 Billion sale to Sun Micro-systems and subsequent sale to Oracle.  He is a man who is obviously still passionate about licence issues, the tyranny of software patents, and the rise of nosql databases.  He is still very active with MariaDB and put forward some good arguments using Maria against NoSQL. There is also a &lt;a href="http://youtu.be/OfPzs2XCZGE" title="DCLondon2013 David Axmark Interview"&gt;Drupal Camp London 2013 Interview&lt;/a&gt; available on YouTube. &lt;a href="http://www.flickr.com/photos/peterlozano/8520539197/" title="Untitled by Pedro Lozano, on Flickr" class="pull-right"&gt;&lt;img src="http://farm9.staticflickr.com/8389/8520539197_9a297a8974.jpg" width="500" height="333" alt="Untitled"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;For me, every camp has a different theme, flavour, running through it.  I'm not sure if that's because of the tracks I choose, or, whether that is the way the tonal centre of the camp evolves.  The fantastic Manchester Camp, at Media City in November, had a themer theme running through it.  Whereas the London Camp, had more of an emphasis on developers, development, development environments.  My first session was &lt;a href="http://2013.drupalcamplondon.co.uk/session/drush-make-driven-development"&gt;Drush Make Driven Development&lt;/a&gt; with Steven Jones.  As someone reasonably new to Drush and Git (About 12 Months) I was a little concerned that I was going to be left cross eyed and bewildered, but Steven's skills as not just a developer, but a speaker, kept me totally engaged and with it, knowing full well that he would be uploading the slides to refresh my informationally bombarded brain.&lt;/p&gt;

&lt;p&gt;Over the entire weekend I had a bit of a Vagrant fest.  My next session was what sparked that off.  I've dabbled with Vagrant before, but not knowing to what ends.  &lt;a href="http://2013.drupalcamplondon.co.uk/session/my-vagrant-love-developer%E2%80%99s-kiss-and-tell"&gt;My Vagrant Love: A Developer’s Kiss-and-Tell&lt;/a&gt; by Joe Baker gave me a lot more to go at, and a better understanding of what I could do with it when I got there.  I've used "vagrant up" numerous times since this talk.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://2013.drupalcamplondon.co.uk/session/wake-and-smell-coffeescript"&gt;Wake up and smell the CoffeeScript&lt;/a&gt; with Rob Knight was also an engaging talk.  He very eloquently explained and live demo'd using coffee script.  As someone who's a regular semi-colon excluder, and lover of DRY, I can see the benefits.  It reminds me of SASS for Javascript.&lt;/p&gt;

&lt;p&gt;After lunch it was &lt;a href="http://2013.drupalcamplondon.co.uk/session/manage-your-media-assets-scald-module"&gt;Manage your media assets with the Scald Module&lt;/a&gt; by Sylvain Moreau and Olivier Friesse.  I'm still an IMCE kinda guy myself, but this module does have lots of bells and whistles.  Which is probably why I'm still an IMCE kinda guy ;)  It was a good presentation though, with lots of live demos (very brave).&lt;/p&gt;

&lt;p&gt;More vagrant! &lt;a href="http://2013.drupalcamplondon.co.uk/session/make-your-cheap-vm-fly"&gt;Make your cheap VM fly&lt;/a&gt; with Greg Harvey was very exciting.  I feel quite antiquated using LAMP stack now.  This was all about serving drupal on nginx, with varnish, percona, and all the trimmings.  It was a really good talk for addressing some of the criticisms Drupal hosting sometimes unfairly receives.&lt;a href="http://www.flickr.com/photos/peterlozano/8521653318/" title="Untitled by Pedro Lozano, on Flickr" class="pull-right"&gt;&lt;img src="http://farm9.staticflickr.com/8235/8521653318_c450563298.jpg" width="500" height="333" alt="Untitled"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Next up was &lt;a href="http://2013.drupalcamplondon.co.uk/session/drupal-commerce-and-sagepay-reboot-added-doughnuts"&gt;Drupal Commerce and SagePay&lt;/a&gt; - the reboot (with added doughnuts) by Richard Jones.  It was really good and showed how Commerce is now more than a one trick pony with payment gateways.  Commerce is awesome, but the lack of payment gateways has been holding it's World domination back.&lt;/p&gt;

&lt;p&gt;Yea, more vagrant, but I'd not had enough.  &lt;a href="http://2013.drupalcamplondon.co.uk/session/vagrant-crash-course"&gt;Vagrant: A crash course&lt;/a&gt; by Marcus Deglos was entertaining and educating.  This talk gave me a lot more practical uses for rapidly setting up development environments, and what to do with them when I've got them.  I was wondering on the bus on the way home whether you could use Jenkins to move those config's about?  But then came the social sponsored by Agile Collective.  It's like a dream night having a pub full of Drupalistas, and talking Drupal progressivly louder and more slurred as the evening went on, and good laughing at some people who developed jelly legs.&lt;/p&gt;

&lt;p&gt;The Sunday was a little more subdued to with a few self pitying groans from here and there.  The keynote was &lt;a href="http://2013.drupalcamplondon.co.uk/session/step-4-distributions-could-help-if-we-do-them-right"&gt;Step 4: Distributions could help, if we do them right&lt;/a&gt; by Robert Douglass    He started off by talking about what's needed in Drupal in terms of distributions and modules.  However, he was actually reading from a 2006 blog.  Highlighting that not enough has changed and more can be done to compete against Wordpress's theme forrest, and Google Apps.  There is a lack of Drupal Apps in the App server.  It did have me thinking about the UI in all what I develop.&lt;/p&gt;

&lt;p&gt;Next was &lt;a href="http://2013.drupalcamplondon.co.uk/session/drupal-8-configuration-system-coders"&gt;Drupal 8 Configuration system for Coders&lt;/a&gt; by Alex Pott which offered an insight into settings, state and config in Drupal 8.  It was a good talk, and heads up for developers to look out for en route to Drupal 8.  I was feeling a bit jaded, but Alex kept me engaged. &lt;a href="http://www.flickr.com/photos/peterlozano/8521655084/" title="Untitled by Pedro Lozano, on Flickr" class="pull-left"&gt;&lt;img src="http://farm9.staticflickr.com/8516/8521655084_603e7305d5.jpg" width="500" height="333" alt="Untitled"&gt;&lt;/a&gt; &lt;/p&gt;

&lt;p&gt;&lt;a href="http://2013.drupalcamplondon.co.uk/session/death-themer"&gt;Death of a Themer&lt;/a&gt; by James Panton was a really interesting presentation which highlighted a cool way of tidying up your markup.  This was also a great tool provider.  In essence it was about stripping bare the tpl.php and using Display Suite, Panels and Panels Everywhere to render the page.  I've not put it into practice yet.  I can see some pitfalls, like slideshows etc, but we are definitely looking at it as a way to dramatically reduce the &amp;lt;div&amp;gt; count.&lt;/p&gt;

&lt;p&gt;My final talk was by Steve Cowie from Code Enigma explaining &lt;a href="http://2013.drupalcamplondon.co.uk/session/distributed-working-how-work-anywhere"&gt;Distributed working - how to work from anywhere&lt;/a&gt;  It was an honest insight into distributed working which highlighted some factors I'd never thought about: like different country employment laws, and how to use Agile and Scrum with a distributed team.  Definitely looking at our organisation becoming distributed using a lot of the structures Steve highlighted.&lt;/p&gt;

&lt;p&gt;All in all, it was a fantastic weekend, and I only wish I could have been in loads of places at once.  I wanted to do the BOFF sessions and I definitely would love to have joined in with some of the sprints, but having too much choice is a good thing right?  I can't wait for the next Drupal camp.  Drupal rocks my disco, from our local user groups to the Drupal camps, and this year I'm hoping to to go to the &lt;a href="http://prague2013.drupal.org/"&gt;European Drupal conference&lt;/a&gt; in Prague.&lt;/p&gt;

&lt;p&gt;Thanks go to &lt;a href="http://www.flickr.com/photos/peterlozano/" title="Pedro Lozano"&gt;Pedro Lozano&lt;/a&gt; for allowing me to use some of his amazing pictures from the event in this post.&lt;/p&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="field field-name-field-blogcategory field-type-taxonomy-term-reference field-label-above"&gt;&lt;div class="field-label"&gt;Category:&amp;nbsp;&lt;/div&gt;&lt;div class="field-items"&gt;&lt;div class="field-item even"&gt;&lt;a href="/blog/category/drupal"&gt;Drupal&lt;/a&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="field field-name-field-blogtags field-type-taxonomy-term-reference field-label-above"&gt;&lt;div class="field-label"&gt;Tags:&amp;nbsp;&lt;/div&gt;&lt;div class="field-items"&gt;&lt;div class="field-item even"&gt;&lt;a href="/blog/tag/drupalcamp"&gt;DrupalCamp&lt;/a&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/hashbangcode/~4/ApZf95hHb-E" height="1" width="1"/&gt;</description>
 <pubDate>Tue, 26 Mar 2013 22:04:13 +0000</pubDate>
 <dc:creator>PaulWoodhead</dc:creator>
 <guid isPermaLink="false">704 at http://www.hashbangcode.com</guid>
 <comments>http://www.hashbangcode.com/blog/drupal-camp-london-2013-704.html#comments</comments>
<feedburner:origLink>http://www.hashbangcode.com/blog/drupal-camp-london-2013-704.html</feedburner:origLink></item>
<item>
 <title>Finding The First And Last Items In An Array In PHP</title>
 <link>http://feedproxy.google.com/~r/hashbangcode/~3/Ty4FuiKVlso/finding-first-and-last-items-array-php-707.html</link>
 <description>&lt;div class="field field-name-body field-type-text-with-summary field-label-hidden"&gt;&lt;div class="field-items"&gt;&lt;div class="field-item even"&gt;&lt;p&gt;Getting the first or last item from an array in PHP is usually quite easy. If you create an array and then add a bunch of values to it then the array index will increment by 1 for every element you add.  This means that in order to get the first element from an array you just reference the default stating position (0). To get the last item in the array the index key will be the length of the array, minus 1. Here is some example code showing this.&lt;/p&gt;

&lt;div class="geshifilter"&gt;&lt;table class="php geshifilter-php" style="font-family:monospace;"&gt;&lt;tbody&gt;&lt;tr class="li1"&gt;&lt;td style="width:1px;text-align:right;margin:0;padding:0 2px;vertical-align:top;"&gt;&lt;pre style="font-family: monospace; font-weight: normal; font-style: normal"&gt;1
2
3
4
5
6
7
8
9
10
&lt;/pre&gt;&lt;/td&gt;&lt;td style="font-family: monospace; font-weight: normal; font-style: normal"&gt;&lt;pre style="font-family: monospace; font-weight: normal; font-style: normal"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;?php&lt;/span&gt;
&lt;span style="color: #000088;"&gt;$array&lt;/span&gt; &lt;span style="color: #339933;"&gt;=&lt;/span&gt; &lt;a href="http://www.php.net/array"&gt;&lt;span style="color: #990000;"&gt;array&lt;/span&gt;&lt;/a&gt;&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt;&lt;span style="color: #339933;"&gt;;&lt;/span&gt;
&lt;span style="color: #000088;"&gt;$array&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#91;&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#93;&lt;/span&gt; &lt;span style="color: #339933;"&gt;=&lt;/span&gt; &lt;span style="color: #cc66cc;"&gt;1&lt;/span&gt;&lt;span style="color: #339933;"&gt;;&lt;/span&gt;
&lt;span style="color: #000088;"&gt;$array&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#91;&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#93;&lt;/span&gt; &lt;span style="color: #339933;"&gt;=&lt;/span&gt; &lt;span style="color: #cc66cc;"&gt;2&lt;/span&gt;&lt;span style="color: #339933;"&gt;;&lt;/span&gt;
&amp;nbsp;
&lt;span style="color: #666666; font-style: italic;"&gt;// get the first item in the array&lt;/span&gt;
&lt;span style="color: #b1b100;"&gt;print&lt;/span&gt; &lt;span style="color: #000088;"&gt;$array&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#91;&lt;/span&gt;&lt;span style="color: #cc66cc;"&gt;0&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#93;&lt;/span&gt;&lt;span style="color: #339933;"&gt;;&lt;/span&gt; &lt;span style="color: #666666; font-style: italic;"&gt;// prints 1&lt;/span&gt;
&amp;nbsp;
&lt;span style="color: #666666; font-style: italic;"&gt;// get the last item in the array&lt;/span&gt;
&lt;span style="color: #b1b100;"&gt;print&lt;/span&gt; &lt;span style="color: #000088;"&gt;$array&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#91;&lt;/span&gt;&lt;a href="http://www.php.net/count"&gt;&lt;span style="color: #990000;"&gt;count&lt;/span&gt;&lt;/a&gt;&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #000088;"&gt;$array&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt; &lt;span style="color: #339933;"&gt;-&lt;/span&gt; &lt;span style="color: #cc66cc;"&gt;1&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#93;&lt;/span&gt;&lt;span style="color: #339933;"&gt;;&lt;/span&gt; &lt;span style="color: #666666; font-style: italic;"&gt;// prints 2&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Things become slightly more complicated when the array has non standard key values. Take the following array for example in which the array count it started at 1.&lt;/p&gt;

&lt;div class="geshifilter"&gt;&lt;table class="php geshifilter-php" style="font-family:monospace;"&gt;&lt;tbody&gt;&lt;tr class="li1"&gt;&lt;td style="width:1px;text-align:right;margin:0;padding:0 2px;vertical-align:top;"&gt;&lt;pre style="font-family: monospace; font-weight: normal; font-style: normal"&gt;1
2
&lt;/pre&gt;&lt;/td&gt;&lt;td style="font-family: monospace; font-weight: normal; font-style: normal"&gt;&lt;pre style="font-family: monospace; font-weight: normal; font-style: normal"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;?php&lt;/span&gt;
&lt;span style="color: #000088;"&gt;$array&lt;/span&gt; &lt;span style="color: #339933;"&gt;=&lt;/span&gt; &lt;a href="http://www.php.net/array"&gt;&lt;span style="color: #990000;"&gt;array&lt;/span&gt;&lt;/a&gt;&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #cc66cc;"&gt;1&lt;/span&gt; &lt;span style="color: #339933;"&gt;=&amp;gt;&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;'one'&lt;/span&gt;&lt;span style="color: #339933;"&gt;,&lt;/span&gt; &lt;span style="color: #cc66cc;"&gt;2&lt;/span&gt; &lt;span style="color: #339933;"&gt;=&amp;gt;&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;'two'&lt;/span&gt;&lt;span style="color: #339933;"&gt;,&lt;/span&gt; &lt;span style="color: #cc66cc;"&gt;3&lt;/span&gt; &lt;span style="color: #339933;"&gt;=&amp;gt;&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;'three'&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt;&lt;span style="color: #339933;"&gt;;&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;It isn't possible to reference the first item of the array as 0 here because that array key doesn't exist (we would get an 'undefined offset' error in this case). Also, if we try to access the last item in the array in the same way as before then we will return the value 'two' as the length of the array (3) minus 1 will point to the key in the middle of this array.&lt;/p&gt;

&lt;p&gt;PHP comes with a couple of functions that can help when pulling out items in an array. One way of doing this is to use the &lt;em&gt;array_values()&lt;/em&gt; function, which will return an array with all of the key values reset. This means that the first item in the array will have a key of 0 and the last item will have a key of the length of the array, minus 1. Therefore, you can just use the original method to find the first and last items.&lt;/p&gt;

&lt;div class="geshifilter"&gt;&lt;table class="php geshifilter-php" style="font-family:monospace;"&gt;&lt;tbody&gt;&lt;tr class="li1"&gt;&lt;td style="width:1px;text-align:right;margin:0;padding:0 2px;vertical-align:top;"&gt;&lt;pre style="font-family: monospace; font-weight: normal; font-style: normal"&gt;1
2
3
4
5
6
7
8
9
10
&lt;/pre&gt;&lt;/td&gt;&lt;td style="font-family: monospace; font-weight: normal; font-style: normal"&gt;&lt;pre style="font-family: monospace; font-weight: normal; font-style: normal"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;?php&lt;/span&gt;
&lt;span style="color: #000088;"&gt;$array&lt;/span&gt; &lt;span style="color: #339933;"&gt;=&lt;/span&gt; &lt;a href="http://www.php.net/array"&gt;&lt;span style="color: #990000;"&gt;array&lt;/span&gt;&lt;/a&gt;&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #cc66cc;"&gt;1&lt;/span&gt; &lt;span style="color: #339933;"&gt;=&amp;gt;&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;'one'&lt;/span&gt;&lt;span style="color: #339933;"&gt;,&lt;/span&gt; &lt;span style="color: #cc66cc;"&gt;2&lt;/span&gt; &lt;span style="color: #339933;"&gt;=&amp;gt;&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;'two'&lt;/span&gt;&lt;span style="color: #339933;"&gt;,&lt;/span&gt; &lt;span style="color: #cc66cc;"&gt;3&lt;/span&gt; &lt;span style="color: #339933;"&gt;=&amp;gt;&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;'three'&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt;&lt;span style="color: #339933;"&gt;;&lt;/span&gt;
&amp;nbsp;
&lt;span style="color: #000088;"&gt;$array_values&lt;/span&gt; &lt;span style="color: #339933;"&gt;=&lt;/span&gt; &lt;a href="http://www.php.net/array_values"&gt;&lt;span style="color: #990000;"&gt;array_values&lt;/span&gt;&lt;/a&gt;&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #000088;"&gt;$array&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt;&lt;span style="color: #339933;"&gt;;&lt;/span&gt;
&amp;nbsp;
&lt;span style="color: #666666; font-style: italic;"&gt;// get the first item in the array&lt;/span&gt;
&lt;span style="color: #b1b100;"&gt;print&lt;/span&gt; &lt;span style="color: #000088;"&gt;$array_values&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#91;&lt;/span&gt;&lt;span style="color: #cc66cc;"&gt;0&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#93;&lt;/span&gt;&lt;span style="color: #339933;"&gt;;&lt;/span&gt; &lt;span style="color: #666666; font-style: italic;"&gt;// prints 'one'&lt;/span&gt;
&amp;nbsp;
&lt;span style="color: #666666; font-style: italic;"&gt;// get the last item in the array&lt;/span&gt;
&lt;span style="color: #b1b100;"&gt;print&lt;/span&gt; &lt;span style="color: #000088;"&gt;$array_values&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#91;&lt;/span&gt;&lt;a href="http://www.php.net/count"&gt;&lt;span style="color: #990000;"&gt;count&lt;/span&gt;&lt;/a&gt;&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #000088;"&gt;$array_values&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt; &lt;span style="color: #339933;"&gt;-&lt;/span&gt; &lt;span style="color: #cc66cc;"&gt;1&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#93;&lt;/span&gt;&lt;span style="color: #339933;"&gt;;&lt;/span&gt; &lt;span style="color: #666666; font-style: italic;"&gt;// prints 'three'&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;As the &lt;em&gt;array_values()&lt;/em&gt; function creates returns a copy of the array we can use the a couple of functions to snip off the beginning and end of the array without altering the original array. The &lt;em&gt;array_shift()&lt;/em&gt; function takes an array as it's only parameter and will cut off and return the first item found in that array. The &lt;em&gt;array_pop()&lt;/em&gt; function takes an array as it's only parameter and will cut off and return the last item found in the array. This provides us with an easy mechanism to complete our original task without these functions altering the original array.&lt;/p&gt;

&lt;div class="geshifilter"&gt;&lt;table class="php geshifilter-php" style="font-family:monospace;"&gt;&lt;tbody&gt;&lt;tr class="li1"&gt;&lt;td style="width:1px;text-align:right;margin:0;padding:0 2px;vertical-align:top;"&gt;&lt;pre style="font-family: monospace; font-weight: normal; font-style: normal"&gt;1
2
3
4
5
6
7
8
9
10
&lt;/pre&gt;&lt;/td&gt;&lt;td style="font-family: monospace; font-weight: normal; font-style: normal"&gt;&lt;pre style="font-family: monospace; font-weight: normal; font-style: normal"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;?php&lt;/span&gt;
&lt;span style="color: #000088;"&gt;$array&lt;/span&gt; &lt;span style="color: #339933;"&gt;=&lt;/span&gt; &lt;a href="http://www.php.net/array"&gt;&lt;span style="color: #990000;"&gt;array&lt;/span&gt;&lt;/a&gt;&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #cc66cc;"&gt;1&lt;/span&gt; &lt;span style="color: #339933;"&gt;=&amp;gt;&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;'one'&lt;/span&gt;&lt;span style="color: #339933;"&gt;,&lt;/span&gt; &lt;span style="color: #cc66cc;"&gt;2&lt;/span&gt; &lt;span style="color: #339933;"&gt;=&amp;gt;&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;'two'&lt;/span&gt;&lt;span style="color: #339933;"&gt;,&lt;/span&gt; &lt;span style="color: #cc66cc;"&gt;3&lt;/span&gt; &lt;span style="color: #339933;"&gt;=&amp;gt;&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;'three'&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt;&lt;span style="color: #339933;"&gt;;&lt;/span&gt;
&amp;nbsp;
&lt;span style="color: #000088;"&gt;$array_values&lt;/span&gt; &lt;span style="color: #339933;"&gt;=&lt;/span&gt; &lt;a href="http://www.php.net/array_values"&gt;&lt;span style="color: #990000;"&gt;array_values&lt;/span&gt;&lt;/a&gt;&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #000088;"&gt;$array&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt;&lt;span style="color: #339933;"&gt;;&lt;/span&gt;
&amp;nbsp;
&lt;span style="color: #666666; font-style: italic;"&gt;// get the first item in the array&lt;/span&gt;
&lt;span style="color: #b1b100;"&gt;print&lt;/span&gt; &lt;a href="http://www.php.net/array_shift"&gt;&lt;span style="color: #990000;"&gt;array_shift&lt;/span&gt;&lt;/a&gt;&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #000088;"&gt;$array_values&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt;&lt;span style="color: #339933;"&gt;;&lt;/span&gt; &lt;span style="color: #666666; font-style: italic;"&gt;// prints 'one'&lt;/span&gt;
&amp;nbsp;
&lt;span style="color: #666666; font-style: italic;"&gt;// get the last item in the array&lt;/span&gt;
&lt;span style="color: #b1b100;"&gt;print&lt;/span&gt; &lt;a href="http://www.php.net/array_pop"&gt;&lt;span style="color: #990000;"&gt;array_pop&lt;/span&gt;&lt;/a&gt;&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #000088;"&gt;$array_values&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt;&lt;span style="color: #339933;"&gt;;&lt;/span&gt; &lt;span style="color: #666666; font-style: italic;"&gt;// prints 'three'&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Another way that is similar to this would to use the &lt;em&gt;array_keys()&lt;/em&gt; function to return the first and last keys of the array using &lt;em&gt;array_shift()&lt;/em&gt; and &lt;em&gt;array_pop()&lt;/em&gt;, which would then be referenced directly within the original array.&lt;/p&gt;

&lt;div class="geshifilter"&gt;&lt;table class="php geshifilter-php" style="font-family:monospace;"&gt;&lt;tbody&gt;&lt;tr class="li1"&gt;&lt;td style="width:1px;text-align:right;margin:0;padding:0 2px;vertical-align:top;"&gt;&lt;pre style="font-family: monospace; font-weight: normal; font-style: normal"&gt;1
2
3
4
5
6
7
8
9
10
&lt;/pre&gt;&lt;/td&gt;&lt;td style="font-family: monospace; font-weight: normal; font-style: normal"&gt;&lt;pre style="font-family: monospace; font-weight: normal; font-style: normal"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;?php&lt;/span&gt;
&lt;span style="color: #000088;"&gt;$array&lt;/span&gt; &lt;span style="color: #339933;"&gt;=&lt;/span&gt; &lt;a href="http://www.php.net/array"&gt;&lt;span style="color: #990000;"&gt;array&lt;/span&gt;&lt;/a&gt;&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #cc66cc;"&gt;1&lt;/span&gt; &lt;span style="color: #339933;"&gt;=&amp;gt;&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;'one'&lt;/span&gt;&lt;span style="color: #339933;"&gt;,&lt;/span&gt; &lt;span style="color: #cc66cc;"&gt;2&lt;/span&gt; &lt;span style="color: #339933;"&gt;=&amp;gt;&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;'two'&lt;/span&gt;&lt;span style="color: #339933;"&gt;,&lt;/span&gt; &lt;span style="color: #cc66cc;"&gt;3&lt;/span&gt; &lt;span style="color: #339933;"&gt;=&amp;gt;&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;'three'&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt;&lt;span style="color: #339933;"&gt;;&lt;/span&gt;
&amp;nbsp;
&lt;span style="color: #000088;"&gt;$array_keys&lt;/span&gt; &lt;span style="color: #339933;"&gt;=&lt;/span&gt; &lt;a href="http://www.php.net/array_keys"&gt;&lt;span style="color: #990000;"&gt;array_keys&lt;/span&gt;&lt;/a&gt;&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #000088;"&gt;$array&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt;&lt;span style="color: #339933;"&gt;;&lt;/span&gt;
&amp;nbsp;
&lt;span style="color: #666666; font-style: italic;"&gt;// get the first item in the array&lt;/span&gt;
&lt;span style="color: #b1b100;"&gt;print&lt;/span&gt; &lt;span style="color: #000088;"&gt;$array&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#91;&lt;/span&gt;&lt;a href="http://www.php.net/array_shift"&gt;&lt;span style="color: #990000;"&gt;array_shift&lt;/span&gt;&lt;/a&gt;&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #000088;"&gt;$array_keys&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#93;&lt;/span&gt;&lt;span style="color: #339933;"&gt;;&lt;/span&gt; &lt;span style="color: #666666; font-style: italic;"&gt;// prints 'one'&lt;/span&gt;
&amp;nbsp;
&lt;span style="color: #666666; font-style: italic;"&gt;// get the last item in the array&lt;/span&gt;
&lt;span style="color: #b1b100;"&gt;print&lt;/span&gt; &lt;span style="color: #000088;"&gt;$array&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#91;&lt;/span&gt;&lt;a href="http://www.php.net/array_pop"&gt;&lt;span style="color: #990000;"&gt;array_pop&lt;/span&gt;&lt;/a&gt;&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #000088;"&gt;$array_keys&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#93;&lt;/span&gt;&lt;span style="color: #339933;"&gt;;&lt;/span&gt; &lt;span style="color: #666666; font-style: italic;"&gt;// prints 'three'&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;There are clearly a few ways of doing this simple task in PHP. However, the best way to extract the first and last items from an array is by using the internal PHP array pointer functions. Every array has an internal pointer that points to the 'current' element. When the array is created this pointer is initialised to be the first element in the array. There are a few PHP functions that deal with moving this pointer around an array, but for the purposes of finding the first and last element we only need to look at &lt;em&gt;current()&lt;/em&gt;, &lt;em&gt;reset()&lt;/em&gt;, and &lt;em&gt;end()&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;The end() function will move the pointer to the last element in the array and return the value of it. The &lt;em&gt;reset()&lt;/em&gt; function will move the pointer to the start of the array and return the first element. The &lt;em&gt;current()&lt;/em&gt; function will return the current item in the array, which should be the first element if the array is newly initialised. Using the &lt;em&gt;current()&lt;/em&gt; function can be unpredictable unless you know exactly where the array pointer is, in which case the &lt;em&gt;reset()&lt;/em&gt; function might be a better choice.&lt;/p&gt;

&lt;div class="geshifilter"&gt;&lt;table class="php geshifilter-php" style="font-family:monospace;"&gt;&lt;tbody&gt;&lt;tr class="li1"&gt;&lt;td style="width:1px;text-align:right;margin:0;padding:0 2px;vertical-align:top;"&gt;&lt;pre style="font-family: monospace; font-weight: normal; font-style: normal"&gt;1
2
3
4
5
6
7
8
9
10
11
&lt;/pre&gt;&lt;/td&gt;&lt;td style="font-family: monospace; font-weight: normal; font-style: normal"&gt;&lt;pre style="font-family: monospace; font-weight: normal; font-style: normal"&gt;&lt;span style="color: #000000; font-weight: bold;"&gt;&amp;lt;?php&lt;/span&gt;
&lt;span style="color: #000088;"&gt;$array&lt;/span&gt; &lt;span style="color: #339933;"&gt;=&lt;/span&gt; &lt;a href="http://www.php.net/array"&gt;&lt;span style="color: #990000;"&gt;array&lt;/span&gt;&lt;/a&gt;&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #cc66cc;"&gt;1&lt;/span&gt; &lt;span style="color: #339933;"&gt;=&amp;gt;&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;'one'&lt;/span&gt;&lt;span style="color: #339933;"&gt;,&lt;/span&gt; &lt;span style="color: #cc66cc;"&gt;2&lt;/span&gt; &lt;span style="color: #339933;"&gt;=&amp;gt;&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;'two'&lt;/span&gt;&lt;span style="color: #339933;"&gt;,&lt;/span&gt; &lt;span style="color: #cc66cc;"&gt;3&lt;/span&gt; &lt;span style="color: #339933;"&gt;=&amp;gt;&lt;/span&gt; &lt;span style="color: #0000ff;"&gt;'three'&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt;&lt;span style="color: #339933;"&gt;;&lt;/span&gt;
&amp;nbsp;
&lt;span style="color: #666666; font-style: italic;"&gt;// get the first item in the array (probably)&lt;/span&gt;
&lt;span style="color: #b1b100;"&gt;print&lt;/span&gt; &lt;a href="http://www.php.net/current"&gt;&lt;span style="color: #990000;"&gt;current&lt;/span&gt;&lt;/a&gt;&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #000088;"&gt;$array&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt;&lt;span style="color: #339933;"&gt;;&lt;/span&gt; &lt;span style="color: #666666; font-style: italic;"&gt;// prints 'one'&lt;/span&gt;
&amp;nbsp;
&lt;span style="color: #666666; font-style: italic;"&gt;// get the first item in the array (definitely)&lt;/span&gt;
&lt;span style="color: #b1b100;"&gt;print&lt;/span&gt; &lt;a href="http://www.php.net/reset"&gt;&lt;span style="color: #990000;"&gt;reset&lt;/span&gt;&lt;/a&gt;&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #000088;"&gt;$array&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt;&lt;span style="color: #339933;"&gt;;&lt;/span&gt; &lt;span style="color: #666666; font-style: italic;"&gt;// prints 'one'&lt;/span&gt;
&amp;nbsp;
&lt;span style="color: #666666; font-style: italic;"&gt;// get the last item in the array&lt;/span&gt;
&lt;span style="color: #b1b100;"&gt;print&lt;/span&gt; &lt;a href="http://www.php.net/end"&gt;&lt;span style="color: #990000;"&gt;end&lt;/span&gt;&lt;/a&gt;&lt;span style="color: #009900;"&gt;&amp;#40;&lt;/span&gt;&lt;span style="color: #000088;"&gt;$array&lt;/span&gt;&lt;span style="color: #009900;"&gt;&amp;#41;&lt;/span&gt;&lt;span style="color: #339933;"&gt;;&lt;/span&gt; &lt;span style="color: #666666; font-style: italic;"&gt;// prints 'three'&lt;/span&gt;&lt;/pre&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Using the internal PHP array pointer like this is many times more performant than using a combination of array_values(), array_pop() or count(). This is an important consideration, especially when dealing with multiple arrays in large applications.&lt;/p&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="field field-name-field-blogcategory field-type-taxonomy-term-reference field-label-above"&gt;&lt;div class="field-label"&gt;Category:&amp;nbsp;&lt;/div&gt;&lt;div class="field-items"&gt;&lt;div class="field-item even"&gt;&lt;a href="/blog/category/php-arrays"&gt;PHP Arrays&lt;/a&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;div class="field field-name-field-blogtags field-type-taxonomy-term-reference field-label-above"&gt;&lt;div class="field-label"&gt;Tags:&amp;nbsp;&lt;/div&gt;&lt;div class="field-items"&gt;&lt;div class="field-item even"&gt;&lt;a href="/category/tags/php"&gt;PHP&lt;/a&gt;&lt;/div&gt;&lt;div class="field-item odd"&gt;&lt;a href="/category/tags/current"&gt;current&lt;/a&gt;&lt;/div&gt;&lt;div class="field-item even"&gt;&lt;a href="/blog/tag/end"&gt;end&lt;/a&gt;&lt;/div&gt;&lt;div class="field-item odd"&gt;&lt;a href="/blog/tag/restart"&gt;restart&lt;/a&gt;&lt;/div&gt;&lt;div class="field-item even"&gt;&lt;a href="/category/tags/arraykeys"&gt;array_keys&lt;/a&gt;&lt;/div&gt;&lt;/div&gt;&lt;/div&gt;&lt;img src="http://feeds.feedburner.com/~r/hashbangcode/~4/Ty4FuiKVlso" height="1" width="1"/&gt;</description>
 <pubDate>Sun, 24 Mar 2013 12:41:37 +0000</pubDate>
 <dc:creator>philipnorton42</dc:creator>
 <guid isPermaLink="false">707 at http://www.hashbangcode.com</guid>
 <comments>http://www.hashbangcode.com/blog/finding-first-and-last-items-array-php-707.html#comments</comments>
<feedburner:origLink>http://www.hashbangcode.com/blog/finding-first-and-last-items-array-php-707.html</feedburner:origLink></item>
</channel>
</rss>
