<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>WhileIf Blog</title>
	<atom:link href="http://www.whileifblog.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.whileifblog.com</link>
	<description>Programming, Computers and Stuff</description>
	<lastBuildDate>Fri, 18 May 2012 21:50:08 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>YUI &#8211; DOM Manipulation</title>
		<link>http://www.whileifblog.com/2012/05/18/yui-dom-manipulation/</link>
		<comments>http://www.whileifblog.com/2012/05/18/yui-dom-manipulation/#comments</comments>
		<pubDate>Fri, 18 May 2012 21:50:08 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Software]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[yui]]></category>

		<guid isPermaLink="false">http://www.whileifblog.com/?p=943</guid>
		<description><![CDATA[After being frustrated of not getting consistent and accurate result via standard DOM methods especially html_element.getAttribute(&#8216;key&#8217;); and html_element.setAttribute(&#8216;key&#8217;, &#8216;value&#8217;);, came across some YUI library components that provides abstractions to various DOM methods. Some interesting DOM-related tools covered in this post &#8230;<p class="read-more"><a href="http://www.whileifblog.com/2012/05/18/yui-dom-manipulation/">Read more &#187;</a></p><p><a href="http://www.whileifblog.com/2012/05/18/yui-dom-manipulation/">YUI &#8211; DOM Manipulation</a> is a post from: <a href="http://www.whileifblog.com">WhileIf Blog</a></p>
]]></description>
			<content:encoded><![CDATA[<p>After being frustrated of not getting consistent and accurate result via standard DOM methods especially html_element.getAttribute(&#8216;key&#8217;); and html_element.setAttribute(&#8216;key&#8217;, &#8216;value&#8217;);, came across some YUI library components that provides abstractions to various DOM methods.</p>
<p>Some interesting DOM-related tools covered in this post are YAHOO.util.Element, YAHOO.util.DOM and YAHOO.util.Selector.</p>
<p><strong>YAHOO.util.Dom</strong></p>
<p>YAHOO.util.Element is basically a singleton instance that provides various DOM methods. Following are some of the methods that one may find useful. For more information, please refer to the API documentation.</p>
<p><strong>Finding an Element by Id</strong></p>
<p>To find an element by its Id, instead of document.getElementById, another way through YUI would be through YAHOO.util.Dom.get.</p>
<pre class="brush: jscript; title: ; notranslate">
var element = YAHOO.util.Dom.get('some_element');
</pre>
<p><strong>Finding Element(s) by Class Name</strong></p>
<p>To find elements by class name,</p>
<pre class="brush: jscript; title: ; notranslate">
var elements = YAHOO.util.Dom.getElementsByClassName('some_class');
</pre>
<p><span id="more-943"></span></p>
<p><strong>YAHOO.util.Element</strong></p>
<p>Before I discover how powerful is jQuery, I have relied on YAHOO.util.Element a lot in various tasks. Basically, YAHOO.util.Element wraps a HTMLElement and provide some methods to simplify the code used in certain DOM operations such as getting and setting attributes, manipulating element position etc. Detailed API documentation is here.</p>
<p><strong>Creating a New Element</strong></p>
<p>To create a new element, one typically pass in the result from document.createElement into the constructor as follows. For example, to create a new link element.</p>
<pre class="brush: jscript; title: ; notranslate">
// create a new element
var element = new YAHOO.util.Element(document.createElement('a'));

// set the href
element.set('href', 'http://www.google.com/');

// insert caption
element.appendChild(document.createTextNode(
    'Click me to the search engine.'
));

element.appendTo(document.body);
</pre>
<p><strong>Uses an existing Element</strong></p>
<p>To wrap YUI Element component methods to an existing HTMLElement, pass in the element OR the element Id into the constructor.</p>
<pre class="brush: jscript; title: ; notranslate">
var by_element = new YAHOO.util.Element(
    YAHOO.util.Dom.get('some_id')
);
var by_id = new YAHOO.util.Element('some_id');
</pre>
<p><strong>Getting the original HTMLElement</strong></p>
<p>Sooner or later one would find out that it is impossible to call some DOM methods through the YAHOO.util.Element instance, for instance the setAttribute method. Besides that, as of 2.6.0, the appendChild method also doesn’t seem to accept instance from YAHOO.util.Element. Therefore to get the wrapped element,</p>
<pre class="brush: jscript; title: ; notranslate">
var some_container = new YAHOO.util.Element(container_element);
var the_yui_element = new YAHOO.util.Element(original_element);

// this will result in an error
some_container.appendChild(the_yui_element);

// but this won't
some_container.appendChild(the_yui_element.get('element'));
</pre>
<p>Hopefully instances of YAHOO.util.Element are also accepted by all methods requiring HTML element in future releases.</p>
<p><strong>YAHOO.util.Selector</strong></p>
<p><strong>DOM Collection via CSS3 Selector</strong></p>
<p>Those who are from jQuery may be interested in knowing that YUI also provides similar functionality via YAHOO.util.Selector. For those who are interested, the API documentation is here. As an example,</p>
<pre class="brush: jscript; title: ; notranslate">
var elements = YAHOO.util.Selector.query('body p');
</pre>
<p>For those who are interested, the official site provides a great amount of information, guide, examples and tutorials. However, for those who are new to javascript, the tutorials may take them some time to get used to. However, after investing enough time in it, then the extensive API documentation will be extremely useful. I will document down some notes on YAHOO.util.Event, which is my favorite component next time.</p>
<div id="crp_related"><h2 class="title" style="padding-top:15px">What others are reading:</h2><ul><li><a href="http://www.whileifblog.com/2012/02/02/yui-panel-close/" rel="bookmark" class="crp_title">YUI &#8211; Panel close</a></li><li><a href="http://www.whileifblog.com/2011/02/28/yui-define-anonymous-function/" rel="bookmark" class="crp_title">YUI &#8211; Define anonymous function</a></li><li><a href="http://www.whileifblog.com/2012/02/01/yui-define-anonymous-function-2/" rel="bookmark" class="crp_title">YUI &#8211; Define anonymous function</a></li><li><a href="http://www.whileifblog.com/2011/03/02/yui-yui-event/" rel="bookmark" class="crp_title">YUI &#8211; YUI Event</a></li></ul></div><p><a href="http://www.whileifblog.com/2012/05/18/yui-dom-manipulation/">YUI &#8211; DOM Manipulation</a> is a post from: <a href="http://www.whileifblog.com">WhileIf Blog</a></p>
<div id="seo_alrp_related"><h2>Posts Related to YUI - DOM Manipulation</h2><ul><li><div class="seo_alrp_rl_content"><h3><a href="http://www.whileifblog.com/2012/02/11/jquery-accessing-native-properties-and-methods/" rel="bookmark">JQuery &#8211; Accessing Native Properties and Methods</a></h3><p>So you've learned a bit of JavaScript, and have learned that, for instance, on anchor tags, you can access attribute values directly: The only problem ...</p></div></li><li><div class="seo_alrp_rl_content"><h3><a href="http://www.whileifblog.com/2010/07/18/jquery-manipulating-dom-examples/" rel="bookmark">JQuery &#8211; Manipulating DOM examples</a></h3><p>Add a CSS class to a specific element A very clean way to change an element look and feel is to add a css class, ...</p></div></li><li><div class="seo_alrp_rl_content"><h3><a href="http://www.whileifblog.com/2010/11/11/jquery-access-iframe-elements/" rel="bookmark">Jquery &#8211; Access iFrame Elements</a></h3><p>Iframes aren’t the best solution to most problems, but when you do need to use one it’s very handy to know how to access the ...</p></div></li><li><div class="seo_alrp_rl_content"><h3><a href="http://www.whileifblog.com/2012/02/09/jquery-passing-an-attribute-object/" rel="bookmark">JQuery &#8211; Passing an Attribute Object</a></h3><p>As of jQuery 1.4, we can now pass an object as the second parameter of the jQuery function. This is helpful when we need to ...</p></div></li><li><div class="seo_alrp_rl_content"><h3><a href="http://www.whileifblog.com/2010/10/05/jquery-get-the-total-number-of-matched-elements/" rel="bookmark">Jquery &#8211; Get the total number of matched elements</a></h3><p>That what I call a very simple, but very useful tip: This will return the number of matched elements:</p></div></li></ul></div>]]></content:encoded>
			<wfw:commentRss>http://www.whileifblog.com/2012/05/18/yui-dom-manipulation/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP &#8211; Checking Exceptions speed</title>
		<link>http://www.whileifblog.com/2012/05/12/php-checking-exceptions-speed/</link>
		<comments>http://www.whileifblog.com/2012/05/12/php-checking-exceptions-speed/#comments</comments>
		<pubDate>Sat, 12 May 2012 21:26:36 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[benchmark]]></category>
		<category><![CDATA[exceptions]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://www.whileifblog.com/?p=933</guid>
		<description><![CDATA[To verify this, I will do a small benchmark test to measure the performance difference in execution of a simple script configured to throw exceptions and once without them. First a not so useful script to find odd numbers (it’s &#8230;<p class="read-more"><a href="http://www.whileifblog.com/2012/05/12/php-checking-exceptions-speed/">Read more &#187;</a></p><p><a href="http://www.whileifblog.com/2012/05/12/php-checking-exceptions-speed/">PHP &#8211; Checking Exceptions speed</a> is a post from: <a href="http://www.whileifblog.com">WhileIf Blog</a></p>
]]></description>
			<content:encoded><![CDATA[<p>To verify this, I will do a small benchmark test to measure the performance difference in execution of a simple script configured to throw exceptions and once without them.</p>
<p>First a not so useful script to find odd numbers (it’s just for the bench marking purposes)</p>
<pre class="brush: php; title: ; notranslate">
error_reporting(-1);
$time = microtime(TRUE);
$mem = memory_get_usage();

$evens = $odds = array();
foreach (range(1, 500000) as $j) {
 try {
 if ($j % 2 != 0) {
 throw new Exception(&quot;odd number&quot;);
 } else {
 $even[] = $j;
 }
 } catch (Exception $e) {
 $odd[] = $j;
 }
}
echo &quot;evens: &quot; . count($evens) . &quot;, odds &quot; . count($odds);
print_r(array('memory' =&gt; (memory_get_usage() - $mem) / (1024 * 1024), 'microtime' =&gt; microtime(TRUE) - $time));
</pre>
<p>And now the same silly script without using exceptions.</p>
<p><span id="more-933"></span></p>
<pre class="brush: php; title: ; notranslate">
error_reporting(-1);
$time = microtime(TRUE);
$mem = memory_get_usage();

$evens = $odds = array();
foreach (range(1, 500000) as $k) {
 if ($k % 2 != 0) {
 $odds[] = $j;
 } else {
 $evens[] = $j;
 }
}

echo &quot;odds: &quot; . count($odds) . &quot;, evens &quot; . count($evens);
print_r(array('memory' =&gt; (memory_get_usage() - $mem) / (1024 * 1024), 'microtime' =&gt; microtime(TRUE) - $time));
</pre>
<p>The outcomes are as follows:</p>
<p><strong>with exceptions</strong><br />
memory: 15.329871597534<br />
microtime: 2.1678985214796<br />
<strong>without exceptions</strong><br />
memory: 15.216841537290<br />
microtime: 0.2632134548652<br />
As we can see the exploitation of memory is approximately the same. But the script turns out to be almost ten times faster without the use of exceptions.</p>
<p>I have performed these tests using apache on my PC with 2048 MB of memory and PHP 5.3<br />
Now we are going to execute the tests with an almost similar host. This time the same configuration, but PHP 5.4 instead of 5.3.</p>
<p><strong>PHP 5.4:</strong></p>
<p><strong>With exceptions</strong><br />
memory: 10.574567984512<br />
microtime: 0.23456897254687<br />
<strong>without exceptions</strong><br />
memory: 10.569843616487<br />
microtime: 0.10698432145785<br />
I’m very much impressed with the results. The memory usage here in <strong>PHP 5.4</strong> is much efficient now and better run time too (nearly ten times faster).</p>
<p>The results indicate that my conclusion is that the use of exceptions in the flow of our scripts is not as bad as I thought. OK, in this example, the use of exceptions is not a good idea, but other exceptions, if used in complex scripts prove to be really useful. I must also take into account the tests when done over 100,000 iterations so as to get a general idea in the performance differences.</p>
<div id="crp_related"><h2 class="title" style="padding-top:15px">What others are reading:</h2><ul><li><a href="http://www.whileifblog.com/2012/02/18/php-memory-usage-information/" rel="bookmark" class="crp_title">PHP &#8211; Memory Usage Information</a></li><li><a href="http://www.whileifblog.com/2010/07/19/php-calculate-script-execution-time/" rel="bookmark" class="crp_title">PHP &#8211; Calculate script execution time</a></li><li><a href="http://www.whileifblog.com/2012/05/08/php-cpu-usage-information/" rel="bookmark" class="crp_title">PHP &#8211; CPU Usage Information</a></li><li><a href="http://www.whileifblog.com/2011/11/06/php-exceptions-in-php5/" rel="bookmark" class="crp_title">PHP &#8211; Exceptions in PHP5</a></li></ul></div><p><a href="http://www.whileifblog.com/2012/05/12/php-checking-exceptions-speed/">PHP &#8211; Checking Exceptions speed</a> is a post from: <a href="http://www.whileifblog.com">WhileIf Blog</a></p>
<div id="seo_alrp_related"><h2>Posts Related to PHP - Checking Exceptions speed</h2><ul><li><div class="seo_alrp_rl_content"><h3><a href="http://www.whileifblog.com/2012/02/18/php-memory-usage-information/" rel="bookmark">PHP &#8211; Memory Usage Information</a></h3><p>By observing the memory usage of your scripts, you may be able optimize your code better. PHP has a garbage collector and a pretty complex ...</p></div></li><li><div class="seo_alrp_rl_content"><h3><a href="http://www.whileifblog.com/2010/07/19/php-calculate-script-execution-time/" rel="bookmark">PHP &#8211; Calculate script execution time</a></h3><p>For debugging purposes, it is a good thing to be able to calculate the execution time of a script. This is exactly what this piece ...</p></div></li><li><div class="seo_alrp_rl_content"><h3><a href="http://www.whileifblog.com/2012/05/08/php-cpu-usage-information/" rel="bookmark">PHP &#8211; CPU Usage Information</a></h3><p>For this, we are going to utilize the getrusage() function. Keep in mind that this is not available on Windows platforms. That may look a ...</p></div></li><li><div class="seo_alrp_rl_content"><h3><a href="http://www.whileifblog.com/2011/11/06/php-exceptions-in-php5/" rel="bookmark">PHP &#8211; Exceptions in PHP5</a></h3><p>PHP5 supports Exception model, like C++/C# and Java. syntax to catch exception: This code prints: “internal exception” Structure of “Exception” built-in class and inheritance In ...</p></div></li><li><div class="seo_alrp_rl_content"><h3><a href="http://www.whileifblog.com/2010/07/01/computer-quotes-%e2%80%93-quality/" rel="bookmark">Computer Quotes – Quality</a></h3><p>“I don’t care if it works on your machine!  We are not shipping your machine!” (Vidiu Platon) “Programming is like sex: one mistake and you’re ...</p></div></li></ul></div>]]></content:encoded>
			<wfw:commentRss>http://www.whileifblog.com/2012/05/12/php-checking-exceptions-speed/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP &#8211; CPU Usage Information</title>
		<link>http://www.whileifblog.com/2012/05/08/php-cpu-usage-information/</link>
		<comments>http://www.whileifblog.com/2012/05/08/php-cpu-usage-information/#comments</comments>
		<pubDate>Tue, 08 May 2012 18:49:26 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://www.whileifblog.com/?p=930</guid>
		<description><![CDATA[For this, we are going to utilize the getrusage() function. Keep in mind that this is not available on Windows platforms. That may look a bit cryptic unless you already have a system administration background. Here is the explanation of &#8230;<p class="read-more"><a href="http://www.whileifblog.com/2012/05/08/php-cpu-usage-information/">Read more &#187;</a></p><p><a href="http://www.whileifblog.com/2012/05/08/php-cpu-usage-information/">PHP &#8211; CPU Usage Information</a> is a post from: <a href="http://www.whileifblog.com">WhileIf Blog</a></p>
]]></description>
			<content:encoded><![CDATA[<p>For this, we are going to utilize the <span style="text-decoration: underline;">getrusage</span>() function. Keep in mind that this is not available on Windows platforms.</p>
<pre class="brush: php; title: ; notranslate">
print_r(getrusage());
/* prints
Array
(
[ru_oublock] =&gt; 0
[ru_inblock] =&gt; 0
[ru_msgsnd] =&gt; 2
[ru_msgrcv] =&gt; 3
[ru_maxrss] =&gt; 12692
[ru_ixrss] =&gt; 764
[ru_idrss] =&gt; 3864
[ru_minflt] =&gt; 94
[ru_majflt] =&gt; 0
[ru_nsignals] =&gt; 1
[ru_nvcsw] =&gt; 67
[ru_nivcsw] =&gt; 4
[ru_nswap] =&gt; 0
[ru_utime.tv_usec] =&gt; 0
[ru_utime.tv_sec] =&gt; 0
[ru_stime.tv_usec] =&gt; 6269
[ru_stime.tv_sec] =&gt; 0
)

*/
</pre>
<p>That may look a bit cryptic unless you already have a system administration background. Here is the explanation of each value (you don&#8217;t need to memorize these):</p>
<p><strong>ru_oublock</strong>: block output operations<br />
<strong>ru_inblock:</strong> block input operations<br />
<strong>ru_msgsnd:</strong> messages sent<br />
<strong> ru_msgrcv:</strong> messages received<br />
<strong>ru_maxrss:</strong> maximum resident set size<br />
<strong>ru_ixrss:</strong> integral shared memory size<br />
<strong> ru_idrss:</strong> integral unshared data size<br />
<strong>ru_minflt</strong>: page reclaims<br />
<strong>ru_majflt:</strong> page faults<br />
<strong>ru_nsignals:</strong> signals received<br />
<strong>ru_nvcsw:</strong> voluntary context switches<br />
<strong>ru_nivcsw:</strong> involuntary context switches<br />
<strong>ru_nswap:</strong> swaps<br />
<strong>ru_utime.tv_usec</strong>: user time used (microseconds)<br />
<strong>ru_utime.tv_sec</strong>: user time used (seconds)<br />
<strong>ru_stime.tv_usec:</strong> system time used (microseconds)<br />
<strong>ru_stime.tv_sec:</strong> system time used (seconds)</p>
<p>To see how much CPU power the script has consumed, we need to look at the &#8216;user time&#8217; and &#8216;system time&#8217; values. The seconds and microseconds portions are provided separately by default. You can divide the microseconds value by 1 million, and add it to the seconds value, to get the total seconds as a decimal number.</p>
<p>Let&#8217;s see an example:</p>
<pre class="brush: php; title: ; notranslate">
// sleep for 3 seconds (non-busy)
sleep(3);

$data = getrusage();
echo &quot;User time: &quot;.
($data['ru_utime.tv_sec'] +
$data['ru_utime.tv_usec'] / 1000000);
echo &quot;System time: &quot;.
($data['ru_stime.tv_sec'] +
$data['ru_stime.tv_usec'] / 1000000);

/* prints
User time: 0.011552
System time: 0
*/
</pre>
<p>Even though the script took about 3 seconds to run, the CPU usage was very very low. Because during the sleep operation, the script actually does not consume CPU resources. There are many other tasks that may take real time, but may not use CPU time, like waiting for disk operations. So as you see, the CPU usage and the actual length of the runtime are not always the same.</p>
<p>Here is another example:</p>
<pre class="brush: php; title: ; notranslate">
// loop 10 million times (busy)
for($i=0;$i&lt;10000000;$i++) {

}

$data = getrusage();
echo &quot;User time: &quot;.
($data['ru_utime.tv_sec'] +
$data['ru_utime.tv_usec'] / 1000000);
echo &quot;System time: &quot;.
($data['ru_stime.tv_sec'] +
$data['ru_stime.tv_usec'] / 1000000);

/* prints
User time: 1.424592
System time: 0.004204
*/
</pre>
<p>That took about 1.4 seconds of CPU time, almost all of which was user time, since there were no system calls.</p>
<p>System Time is the amount of time the CPU spends performing system calls for the kernel on the program&#8217;s behalf. Here is an example of that:</p>
<pre class="brush: php; title: ; notranslate">
$start = microtime(true);
// keep calling microtime for about 3 seconds
while(microtime(true) - $start &lt; 3) {

}

$data = getrusage();
echo &quot;User time: &quot;.
($data['ru_utime.tv_sec'] +
$data['ru_utime.tv_usec'] / 1000000);
echo &quot;System time: &quot;.
($data['ru_stime.tv_sec'] +
$data['ru_stime.tv_usec'] / 1000000);

/* prints
User time: 1.088171
System time: 1.675315
*/
</pre>
<p>Now we have quite a bit of system time usage. This is because the script calls the microtime() function many times, which performs a request through the operating system to fetch the time.</p>
<p>Also you may notice the numbers do not quite add up to 3 seconds. This is because there were probably other processes on the server as well, and the script was not using 100% CPU for the whole duration of the 3 seconds.</p>
<p>For this, we are going to utilize the getrusage() function. Keep in mind that this is not available on Windows platforms.</p>
<pre class="brush: php; title: ; notranslate">
print_r(getrusage());
/* prints
Array
(
[ru_oublock] =&gt; 0
[ru_inblock] =&gt; 0
[ru_msgsnd] =&gt; 2
[ru_msgrcv] =&gt; 3
[ru_maxrss] =&gt; 12692
[ru_ixrss] =&gt; 764
[ru_idrss] =&gt; 3864
[ru_minflt] =&gt; 94
[ru_majflt] =&gt; 0
[ru_nsignals] =&gt; 1
[ru_nvcsw] =&gt; 67
[ru_nivcsw] =&gt; 4
[ru_nswap] =&gt; 0
[ru_utime.tv_usec] =&gt; 0
[ru_utime.tv_sec] =&gt; 0
[ru_stime.tv_usec] =&gt; 6269
[ru_stime.tv_sec] =&gt; 0
)

*/
</pre>
<p>That may look a bit cryptic unless you already have a system administration background. Here is the explanation of each value (you don&#8217;t need to memorize these):</p>
<p>ru_oublock: block output operations<br />
ru_inblock: block input operations<br />
ru_msgsnd: messages sent<br />
ru_msgrcv: messages received<br />
ru_maxrss: maximum resident set size<br />
ru_ixrss: integral shared memory size<br />
ru_idrss: integral unshared data size<br />
ru_minflt: page reclaims<br />
ru_majflt: page faults<br />
ru_nsignals: signals received<br />
ru_nvcsw: voluntary context switches<br />
ru_nivcsw: involuntary context switches<br />
ru_nswap: swaps<br />
ru_utime.tv_usec: user time used (microseconds)<br />
ru_utime.tv_sec: user time used (seconds)<br />
ru_stime.tv_usec: system time used (microseconds)<br />
ru_stime.tv_sec: system time used (seconds)</p>
<p>To see how much CPU power the script has consumed, we need to look at the &#8216;user time&#8217; and &#8216;system time&#8217; values. The seconds and microseconds portions are provided separately by default. You can divide the microseconds value by 1 million, and add it to the seconds value, to get the total seconds as a decimal number.</p>
<p>Let&#8217;s see an example:</p>
<pre class="brush: php; title: ; notranslate">
// sleep for 3 seconds (non-busy)
sleep(3);

$data = getrusage();
echo &quot;User time: &quot;.
($data['ru_utime.tv_sec'] +
$data['ru_utime.tv_usec'] / 1000000);
echo &quot;System time: &quot;.
($data['ru_stime.tv_sec'] +
$data['ru_stime.tv_usec'] / 1000000);

/* prints
User time: 0.011552
System time: 0
*/
</pre>
<p>Even though the script took about 3 seconds to run, the CPU usage was very very low. Because during the sleep operation, the script actually does not consume CPU resources. There are many other tasks that may take real time, but may not use CPU time, like waiting for disk operations. So as you see, the CPU usage and the actual length of the runtime are not always the same.</p>
<p>Here is another example:</p>
<pre class="brush: php; title: ; notranslate">
// loop 10 million times (busy)
for($i=0;$i&lt;10000000;$i++) {

}

$data = getrusage();
echo &quot;User time: &quot;.
($data['ru_utime.tv_sec'] +
$data['ru_utime.tv_usec'] / 1000000);
echo &quot;System time: &quot;.
($data['ru_stime.tv_sec'] +
$data['ru_stime.tv_usec'] / 1000000);

/* prints
User time: 1.424592
System time: 0.004204
*/
</pre>
<p>That took about 1.4 seconds of CPU time, almost all of which was user time, since there were no system calls.</p>
<p>System Time is the amount of time the CPU spends performing system calls for the kernel on the program&#8217;s behalf. Here is an example of that:</p>
<pre class="brush: php; title: ; notranslate">
$start = microtime(true);
// keep calling microtime for about 3 seconds
while(microtime(true) - $start &lt; 3) {

}

$data = getrusage();
echo &quot;User time: &quot;.
($data['ru_utime.tv_sec'] +
$data['ru_utime.tv_usec'] / 1000000);
echo &quot;System time: &quot;.
($data['ru_stime.tv_sec'] +
$data['ru_stime.tv_usec'] / 1000000);

/* prints
User time: 1.088171
System time: 1.675315
*/
</pre>
<p>Now we have quite a bit of system time usage. This is because the script calls the microtime() function many times, which performs a request through the operating system to fetch the time.</p>
<p>Also you may notice the numbers do not quite add up to 3 seconds. This is because there were probably other processes on the server as well, and the script was not using 100% CPU for the whole duration of the 3 seconds.</p>
<div id="crp_related"><h2 class="title" style="padding-top:15px">What others are reading:</h2><ul><li><a href="http://www.whileifblog.com/2010/07/19/php-calculate-script-execution-time/" rel="bookmark" class="crp_title">PHP &#8211; Calculate script execution time</a></li><li><a href="http://www.whileifblog.com/2012/03/28/linux-set-date-and-time-from-a-command-prompt/" rel="bookmark" class="crp_title">Linux &#8211; Set Date and Time From a Command Prompt</a></li><li><a href="http://www.whileifblog.com/2012/04/19/linux-what-is-tmpfs/" rel="bookmark" class="crp_title">Linux &#8211; What is tmpfs ?</a></li><li><a href="http://www.whileifblog.com/2012/05/12/php-checking-exceptions-speed/" rel="bookmark" class="crp_title">PHP &#8211; Checking Exceptions speed</a></li></ul></div><p><a href="http://www.whileifblog.com/2012/05/08/php-cpu-usage-information/">PHP &#8211; CPU Usage Information</a> is a post from: <a href="http://www.whileifblog.com">WhileIf Blog</a></p>
<div id="seo_alrp_related"><h2>Posts Related to PHP - CPU Usage Information</h2><ul><li><div class="seo_alrp_rl_content"><h3><a href="http://www.whileifblog.com/2010/07/19/php-calculate-script-execution-time/" rel="bookmark">PHP &#8211; Calculate script execution time</a></h3><p>For debugging purposes, it is a good thing to be able to calculate the execution time of a script. This is exactly what this piece ...</p></div></li><li><div class="seo_alrp_rl_content"><h3><a href="http://www.whileifblog.com/2012/05/12/php-checking-exceptions-speed/" rel="bookmark">PHP &#8211; Checking Exceptions speed</a></h3><p>To verify this, I will do a small benchmark test to measure the performance difference in execution of a simple script configured to throw exceptions ...</p></div></li><li><div class="seo_alrp_rl_content"><h3><a href="http://www.whileifblog.com/2012/03/28/linux-set-date-and-time-from-a-command-prompt/" rel="bookmark">Linux &#8211; Set Date and Time From a Command Prompt</a></h3><p>Use the date command to display the current date and time or set the system date / time over ssh session. You can also run ...</p></div></li><li><div class="seo_alrp_rl_content"><h3><a href="http://www.whileifblog.com/2011/09/24/php-determining-the-frequency-of-a-string%e2%80%99s-appearance/" rel="bookmark">PHP &#8211; Determining the Frequency of a String’s Appearance</a></h3><p>The substr_count() function returns the number of times one string occurs within another. Its prototype follows: The following example determines the number of times an ...</p></div></li><li><div class="seo_alrp_rl_content"><h3><a href="http://www.whileifblog.com/2011/01/14/ubuntu-shorten-boot-time-with-profiling/" rel="bookmark">Ubuntu &#8211; Shorten boot time with profiling</a></h3><p>Ubuntu Linux devs have done a great job with the boot time. There is however a bit more you can do by profiling your boot. ...</p></div></li></ul></div>]]></content:encoded>
			<wfw:commentRss>http://www.whileifblog.com/2012/05/08/php-cpu-usage-information/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP 5.4 &#8211; Code reuse using Traits</title>
		<link>http://www.whileifblog.com/2012/05/01/php-5-4-code-reuse-using-traits/</link>
		<comments>http://www.whileifblog.com/2012/05/01/php-5-4-code-reuse-using-traits/#comments</comments>
		<pubDate>Tue, 01 May 2012 15:51:27 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[traits]]></category>

		<guid isPermaLink="false">http://www.whileifblog.com/?p=927</guid>
		<description><![CDATA[Traits enables a developer to reuse sets of methods freely in several independent classes living in different class hierarchies. PHP being a single inheritance language, traits enables horizontal composition of behavior – the application of class members without requiring inheritance. &#8230;<p class="read-more"><a href="http://www.whileifblog.com/2012/05/01/php-5-4-code-reuse-using-traits/">Read more &#187;</a></p><p><a href="http://www.whileifblog.com/2012/05/01/php-5-4-code-reuse-using-traits/">PHP 5.4 &#8211; Code reuse using Traits</a> is a post from: <a href="http://www.whileifblog.com">WhileIf Blog</a></p>
]]></description>
			<content:encoded><![CDATA[<p>Traits enables a developer to reuse sets of methods freely in several independent classes living in different class hierarchies. <strong>PHP</strong> being a single inheritance language, traits enables horizontal composition of behavior – the application of class members without requiring inheritance. I’ll cover <strong>Traits</strong> in a separate post itself.</p>
<p>Below is a artificial example from the PHP documentation.</p>
<pre class="brush: php; title: ; notranslate">

&lt;?php

trait Hello
{
 public function sayHello()
 {
 echo 'Hello ';
 }
}

trait World
{
 public function sayWorld()
 {
 echo 'World';
 }
}

class MyHelloWorld
{
 use Hello, World;
 public function sayExclamationMark()
 {
 echo '!';
 }
}

$o = new MyHelloWorld();
$o-&gt;sayHello();
$o-&gt;sayWorld();
$o-&gt;sayExclamationMark();

?&gt;
</pre>
<p>Which will output:<br />
<strong>Hello World!</strong></p>
<div id="crp_related"><h2 class="title" style="padding-top:15px">What others are reading:</h2><ul><li><a href="http://www.whileifblog.com/2012/04/30/php-5-4-core-changes/" rel="bookmark" class="crp_title">PHP 5.4 &#8211; Core changes</a></li><li><a href="http://www.whileifblog.com/2012/04/24/php-5-4-traits-closures-and-prototype-based-programming/" rel="bookmark" class="crp_title">PHP 5.4 &#8211; Traits, Closures, and Prototype-based Programming</a></li><li><a href="http://www.whileifblog.com/2011/09/11/php-obtain-information-about-a-class/" rel="bookmark" class="crp_title">PHP &#8211; Obtain information about a class</a></li><li><a href="http://www.whileifblog.com/2010/08/24/php-final-keyword/" rel="bookmark" class="crp_title">PHP &#8211; Final keyword</a></li></ul></div><p><a href="http://www.whileifblog.com/2012/05/01/php-5-4-code-reuse-using-traits/">PHP 5.4 &#8211; Code reuse using Traits</a> is a post from: <a href="http://www.whileifblog.com">WhileIf Blog</a></p>
<div id="seo_alrp_related"><h2>Posts Related to PHP 5.4 - Code reuse using Traits</h2><ul><li><div class="seo_alrp_rl_content"><h3><a href="http://www.whileifblog.com/2012/04/24/php-5-4-traits-closures-and-prototype-based-programming/" rel="bookmark">PHP 5.4 &#8211; Traits, Closures, and Prototype-based Programming</a></h3><p>According to Wikipedia, prototype-based programming is “a style of object-oriented programming in which classes are not present, and behavior reuse (known as inheritance in class-based ...</p></div></li><li><div class="seo_alrp_rl_content"><h3><a href="http://www.whileifblog.com/2012/04/30/php-5-4-core-changes/" rel="bookmark">PHP 5.4 &#8211; Core changes</a></h3><p>Along with the above mentioned additions, a variety of new functions and additional methods to existing classes have been added. Classes now support the Class::{expr}() ...</p></div></li><li><div class="seo_alrp_rl_content"><h3><a href="http://www.whileifblog.com/2011/09/11/php-obtain-information-about-a-class/" rel="bookmark">PHP &#8211; Obtain information about a class</a></h3><p>PHP5 includes interesting APIs to obtain information (list of members and methods) about the internal classes or interfaces. Example of PHP5 interesting classes/interfaces: Exception, Iterator. ...</p></div></li><li><div class="seo_alrp_rl_content"><h3><a href="http://www.whileifblog.com/2012/02/15/php-functions-with-arbitrary-number-of-arguments/" rel="bookmark">PHP &#8211; Functions with Arbitrary Number of Arguments</a></h3><p>You may already know that PHP allows you to define functions with optional arguments. But there is also a method for allowing completely arbitrary number ...</p></div></li><li><div class="seo_alrp_rl_content"><h3><a href="http://www.whileifblog.com/2010/08/24/php-final-keyword/" rel="bookmark">PHP &#8211; Final keyword</a></h3><p>Like in Java, the meaning of the keyword "final" in php5 is: Final classes: not extendable Final methods: not overridable final class members(properties) not supported ...</p></div></li></ul></div>]]></content:encoded>
			<wfw:commentRss>http://www.whileifblog.com/2012/05/01/php-5-4-code-reuse-using-traits/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP 5.4 &#8211; Core changes</title>
		<link>http://www.whileifblog.com/2012/04/30/php-5-4-core-changes/</link>
		<comments>http://www.whileifblog.com/2012/04/30/php-5-4-core-changes/#comments</comments>
		<pubDate>Mon, 30 Apr 2012 17:51:57 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://www.whileifblog.com/?p=923</guid>
		<description><![CDATA[Along with the above mentioned additions, a variety of new functions and additional methods to existing classes have been added. Classes now support the Class::{expr}() syntax. Now you can accomplish something like the following for static calls: Although not a &#8230;<p class="read-more"><a href="http://www.whileifblog.com/2012/04/30/php-5-4-core-changes/">Read more &#187;</a></p><p><a href="http://www.whileifblog.com/2012/04/30/php-5-4-core-changes/">PHP 5.4 &#8211; Core changes</a> is a post from: <a href="http://www.whileifblog.com">WhileIf Blog</a></p>
]]></description>
			<content:encoded><![CDATA[<p>Along with the above mentioned additions, a variety of new functions and additional methods to existing classes have been added.<br />
Classes now support the Class::{expr}() syntax. Now you can accomplish something like the following for static calls:</p>
<pre class="brush: php; title: ; notranslate">
class Test
{
 public static function parseH4()
 {
 echo &quot;H4 Parsed&quot;;
 }

 public static function parseH2()
 {
 echo &quot;H2 Parsed&quot;;
 }

}

$t = new Test;
$method_prefix = &quot;parse&quot;;

$t::{$method_prefix . &quot;h4&quot;}();
$t::{$method_prefix . &quot;h2&quot;}();
</pre>
<p>Although not a very creative use of the idea, the real power of the above will manifest itself when using dynamic method calls. Along with the support for expressions while calling static class methods, class member access on instantiation has also been added, as shown below.</p>
<p><span id="more-923"></span></p>
<pre class="brush: php; title: ; notranslate">

class Test
{
 public function SayHello()
 {
 echo &quot;Hello World!&quot;;
 }
}

(new Test)-&gt;SayHello();
</pre>
<p>Magic quotes has been completely removed. Applications dependent on this feature may need to be updated, to avoid security issues. <strong>get_magic_quotes_gpc()</strong> and <strong>get_magic_quotes_runtime()</strong> now always return FALSE. <strong>set_magic_quotes_runtime()</strong> raises an <strong>E_CORE_ERROR</strong> level error.</p>
<p>The following functions have been removed from PHP 5.4, so if you are developing any new applications which will probably be migrated to PHP 5.4, avoid using the following functions.</p>
<pre class="brush: php; title: ; notranslate">
define_syslog_variables()
import_request_variables()
session_is_registered()
session_register()
session_unregister()
mysqli_bind_param()
mysqli_bind_result()
mysqli_client_encoding()
mysqli_fetch()
mysqli_param_count()
mysqli_get_metadata()
mysqli_send_long_data()
mysqli::client_encoding()
mysqli_stmt::stmt()
</pre>
<p>The register_globals and register_long_arrays php.ini directives have been removed. Safe mode also is no longer supported. Any applications that rely on safe mode for security reasons may need adjustment. This feature had already been deprecated as of PHP 5.3.0 and is now completely removed. This will now generate a fatal <strong>E_CORE_ERROR</strong> level error when enabled.</p>
<div id="crp_related"><h2 class="title" style="padding-top:15px">What others are reading:</h2><ul><li><a href="http://www.whileifblog.com/2011/09/11/php-obtain-information-about-a-class/" rel="bookmark" class="crp_title">PHP &#8211; Obtain information about a class</a></li><li><a href="http://www.whileifblog.com/2012/05/01/php-5-4-code-reuse-using-traits/" rel="bookmark" class="crp_title">PHP 5.4 &#8211; Code reuse using Traits</a></li><li><a href="http://www.whileifblog.com/2011/02/17/wordpress-dynamic-highlight-menu/" rel="bookmark" class="crp_title">WordPress &#8211; Dynamic Highlight Menu</a></li><li><a href="http://www.whileifblog.com/2012/04/24/php-5-4-traits-closures-and-prototype-based-programming/" rel="bookmark" class="crp_title">PHP 5.4 &#8211; Traits, Closures, and Prototype-based Programming</a></li></ul></div><p><a href="http://www.whileifblog.com/2012/04/30/php-5-4-core-changes/">PHP 5.4 &#8211; Core changes</a> is a post from: <a href="http://www.whileifblog.com">WhileIf Blog</a></p>
<div id="seo_alrp_related"><h2>Posts Related to PHP 5.4 - Core changes</h2><ul><li><div class="seo_alrp_rl_content"><h3><a href="http://www.whileifblog.com/2011/09/11/php-obtain-information-about-a-class/" rel="bookmark">PHP &#8211; Obtain information about a class</a></h3><p>PHP5 includes interesting APIs to obtain information (list of members and methods) about the internal classes or interfaces. Example of PHP5 interesting classes/interfaces: Exception, Iterator. ...</p></div></li><li><div class="seo_alrp_rl_content"><h3><a href="http://www.whileifblog.com/2012/05/01/php-5-4-code-reuse-using-traits/" rel="bookmark">PHP 5.4 &#8211; Code reuse using Traits</a></h3><p>Traits enables a developer to reuse sets of methods freely in several independent classes living in different class hierarchies. PHP being a single inheritance language, ...</p></div></li><li><div class="seo_alrp_rl_content"><h3><a href="http://www.whileifblog.com/2012/04/24/php-5-4-traits-closures-and-prototype-based-programming/" rel="bookmark">PHP 5.4 &#8211; Traits, Closures, and Prototype-based Programming</a></h3><p>According to Wikipedia, prototype-based programming is “a style of object-oriented programming in which classes are not present, and behavior reuse (known as inheritance in class-based ...</p></div></li><li><div class="seo_alrp_rl_content"><h3><a href="http://www.whileifblog.com/2010/08/24/php-final-keyword/" rel="bookmark">PHP &#8211; Final keyword</a></h3><p>Like in Java, the meaning of the keyword "final" in php5 is: Final classes: not extendable Final methods: not overridable final class members(properties) not supported ...</p></div></li><li><div class="seo_alrp_rl_content"><h3><a href="http://www.whileifblog.com/2011/02/17/wordpress-dynamic-highlight-menu/" rel="bookmark">WordPress &#8211; Dynamic Highlight Menu</a></h3><p>This allows you to theme/style and control the currently selected menu tab in CSS by adding a class="current" on it. Line 2: If Home, or ...</p></div></li></ul></div>]]></content:encoded>
			<wfw:commentRss>http://www.whileifblog.com/2012/04/30/php-5-4-core-changes/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP 5.4 &#8211; Built-in Web Server</title>
		<link>http://www.whileifblog.com/2012/04/26/php-5-4-built-in-web-server/</link>
		<comments>http://www.whileifblog.com/2012/04/26/php-5-4-built-in-web-server/#comments</comments>
		<pubDate>Thu, 26 Apr 2012 19:32:29 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://www.whileifblog.com/?p=919</guid>
		<description><![CDATA[The CLI SAPI provides a built-in web server for development purposes – extremely helpful to quickly start  testing your php pages. Just enter the following on the command line and you are ready to go. The server will now be &#8230;<p class="read-more"><a href="http://www.whileifblog.com/2012/04/26/php-5-4-built-in-web-server/">Read more &#187;</a></p><p><a href="http://www.whileifblog.com/2012/04/26/php-5-4-built-in-web-server/">PHP 5.4 &#8211; Built-in Web Server</a> is a post from: <a href="http://www.whileifblog.com">WhileIf Blog</a></p>
]]></description>
			<content:encoded><![CDATA[<p>The CLI SAPI provides a built-in web server for development purposes – extremely helpful to quickly start  testing your php pages. Just enter the following on the command line and you are ready to go.</p>
<p>The server will now be listening to your requests on port 8000.</p>
<p><strong>php -S localhost:8000</strong></p>
<p>Which will display the following:</p>
<p><strong>PHP 5.4.0 Development Server started at Fri Apr 06 13:56:26 2012</strong></p>
<p><strong>Listening on localhost:8000</strong></p>
<p><strong>Document root is C:\php-5.4.0</strong></p>
<p><strong>Press Ctrl-C to quit.</strong></p>
<p>Of course don’t expect something along the lines of Apache or IIS. This is a small built-in service that lets you quickly check your pages. Absolutely not recommended to use on a production basis.</p>
<div id="crp_related"><h2 class="title" style="padding-top:15px">What others are reading:</h2><ul><li><a href="http://www.whileifblog.com/2012/03/29/memcached-flush-contents-of-a-server-using-command-line/" rel="bookmark" class="crp_title">Memcached &#8211; Flush Contents Of a Server Using Command Line</a></li><li><a href="http://www.whileifblog.com/2012/03/26/linux-network-comands/" rel="bookmark" class="crp_title">Linux &#8211; Network comands</a></li><li><a href="http://www.whileifblog.com/2011/08/09/ubuntu-command-line-magic/" rel="bookmark" class="crp_title">Ubuntu &#8211; Command line magic</a></li><li><a href="http://www.whileifblog.com/2011/09/25/netbeans-tips-tricks/" rel="bookmark" class="crp_title">Netbeans &#8211; Tips &#038; tricks</a></li></ul></div><p><a href="http://www.whileifblog.com/2012/04/26/php-5-4-built-in-web-server/">PHP 5.4 &#8211; Built-in Web Server</a> is a post from: <a href="http://www.whileifblog.com">WhileIf Blog</a></p>
<div id="seo_alrp_related"><h2>Posts Related to PHP 5.4 - Built-in Web Server</h2><ul><li><div class="seo_alrp_rl_content"><h3><a href="http://www.whileifblog.com/2012/03/29/memcached-flush-contents-of-a-server-using-command-line/" rel="bookmark">Memcached &#8211; Flush Contents Of a Server Using Command Line</a></h3><p>You can invalidate all existing cache items using the flush_all command. This command does not pause the server, as it returns immediately. It does not ...</p></div></li><li><div class="seo_alrp_rl_content"><h3><a href="http://www.whileifblog.com/2012/03/26/linux-network-comands/" rel="bookmark">Linux &#8211; Network comands</a></h3><p>This article provides a summary of the most important or frequently used commands. Please keep in mind that these tips assume you already have a ...</p></div></li><li><div class="seo_alrp_rl_content"><h3><a href="http://www.whileifblog.com/2012/03/25/gzip-using-mod_gzip-mod_deflate-and-htaccess/" rel="bookmark">GZip &#8211; using mod_gzip, mod_deflate and htaccess</a></h3><p>Apache server supports server level GZip compression with the help of module mod_gzip and mod_deflate. You can use this module and enable GZip compression for ...</p></div></li><li><div class="seo_alrp_rl_content"><h3><a href="http://www.whileifblog.com/2012/01/26/php-basic-whois/" rel="bookmark">PHP &#8211; Basic Whois</a></h3><p>Whois services are extremely useful to get basic information about a domain name: owner, creation date, registrar, etc. Using PHP and the whois unix command, ...</p></div></li><li><div class="seo_alrp_rl_content"><h3><a href="http://www.whileifblog.com/2012/03/09/linux-how-to-be-faster-at-the-command-line/" rel="bookmark">Linux &#8211; How to Be Faster at the Command Line</a></h3><p>Want to be faster at the Linux command line interface? Since most Linux distributions provide Bash as the default CLI, here are some Bash tricks ...</p></div></li></ul></div>]]></content:encoded>
			<wfw:commentRss>http://www.whileifblog.com/2012/04/26/php-5-4-built-in-web-server/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP 5.4 &#8211; Function Array Dereferencing</title>
		<link>http://www.whileifblog.com/2012/04/25/php-5-4-function-array-dereferencing/</link>
		<comments>http://www.whileifblog.com/2012/04/25/php-5-4-function-array-dereferencing/#comments</comments>
		<pubDate>Wed, 25 Apr 2012 16:58:15 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://www.whileifblog.com/?p=914</guid>
		<description><![CDATA[PHP now supports array dereferencing directly from a function call. Prior to version 5.4 you had to store the returning value from a function into a variable, and then use the variable. The most frequent uses will be when using &#8230;<p class="read-more"><a href="http://www.whileifblog.com/2012/04/25/php-5-4-function-array-dereferencing/">Read more &#187;</a></p><p><a href="http://www.whileifblog.com/2012/04/25/php-5-4-function-array-dereferencing/">PHP 5.4 &#8211; Function Array Dereferencing</a> is a post from: <a href="http://www.whileifblog.com">WhileIf Blog</a></p>
]]></description>
			<content:encoded><![CDATA[<p>PHP now supports array dereferencing directly from a function call.<br />
Prior to version 5.4 you had to store the returning value from a function into a variable, and then use the variable.<br />
The most frequent uses will be when using ‘preg_match’ or ‘explode’ kind of functions, functions that return an array. So instead of the following:</p>
<pre class="brush: php; title: ; notranslate">
$data  = &quot;piece1 piece2 piece3 piece4&quot;;
$pieces = explode(&quot; &quot;, $data);
echo $pieces[0]; // piece1
echo $pieces[1]; // piece2
</pre>
<p>We could instead shorten this to:</p>
<pre class="brush: php; title: ; notranslate">
$data  = &quot;piece1 piece2 piece3 piece4&quot;;
echo explode(&quot; &quot;, $data)[0];
</pre>
<p>Another example using the ‘getdate()’ function.</p>
<p><span id="more-914"></span></p>
<p><strong># Without Function Array dereferencing </strong></p>
<pre class="brush: php; title: ; notranslate">
date_default_timezone_set('Asia/Kolkata');

$temp = getdate();

echo $temp['year'];
echo $temp['mon'];
</pre>
<p><strong># With Function Array dereferencing </strong></p>
<pre class="brush: php; title: ; notranslate">
date_default_timezone_set('Asia/Kolkata');

echo getdate()['mon'];
You can use the above in a conditional as shown below:
date_default_timezone_set('Asia/Kolkata');

if(getdate()['mon'] == 4) {
    echo &quot;Month of April&quot;;
}
</pre>
<p><strong>Another example using a custom function.</strong></p>
<pre class="brush: php; title: ; notranslate">
function getColors ()
{
  return [
          'red' =&gt; '#FF0000',
          'green' =&gt; '#00FF00',
          'blue' =&gt; '#0000FF'
         ];
}

echo getColors()['green']; // returns #00FF00
</pre>
<div id="crp_related"><h2 class="title" style="padding-top:15px">What others are reading:</h2><ul><li><a href="http://www.whileifblog.com/2010/09/07/php-reference-is_array-function/" rel="bookmark" class="crp_title">PHP Reference &#8211; is_array() function</a></li><li><a href="http://www.whileifblog.com/2012/02/18/php-memory-usage-information/" rel="bookmark" class="crp_title">PHP &#8211; Memory Usage Information</a></li><li><a href="http://www.whileifblog.com/2012/02/15/php-functions-with-arbitrary-number-of-arguments/" rel="bookmark" class="crp_title">PHP &#8211; Functions with Arbitrary Number of Arguments</a></li><li><a href="http://www.whileifblog.com/2012/03/24/php-read-filescontentfile-details-from-a-zip-archive-without-extracting-them/" rel="bookmark" class="crp_title">PHP &#8211; Read files/content/file details from a .zip archive, without extracting them</a></li></ul></div><p><a href="http://www.whileifblog.com/2012/04/25/php-5-4-function-array-dereferencing/">PHP 5.4 &#8211; Function Array Dereferencing</a> is a post from: <a href="http://www.whileifblog.com">WhileIf Blog</a></p>
<div id="seo_alrp_related"><h2>Posts Related to PHP 5.4 - Function Array Dereferencing</h2><ul><li><div class="seo_alrp_rl_content"><h3><a href="http://www.whileifblog.com/2010/09/07/php-reference-is_array-function/" rel="bookmark">PHP Reference &#8211; is_array() function</a></h3><p>(PHP 4, PHP 5) is_array() — Finds whether a variable is an array Parameters The variable being evaluated. Return Values Returns TRUE if var is ...</p></div></li><li><div class="seo_alrp_rl_content"><h3><a href="http://www.whileifblog.com/2012/03/22/php-anonymous-functions-and-closures-part-ii/" rel="bookmark">PHP &#8211; Anonymous Functions and Closures (Part II)</a></h3><p>Suppose the application should reduce the list of percentages to those that are equal to or greater than a user-specified value. For this we can ...</p></div></li><li><div class="seo_alrp_rl_content"><h3><a href="http://www.whileifblog.com/2010/09/16/php-reference-is_float-function/" rel="bookmark">PHP Reference &#8211; is_float() function</a></h3><p>(PHP 4, PHP 5) is_float — Finds whether the type of a variable is float Parameters The variable being evaluated. Return Values Returns TRUE if ...</p></div></li><li><div class="seo_alrp_rl_content"><h3><a href="http://www.whileifblog.com/2010/09/18/php-reference-%e2%80%93-is_int-function/" rel="bookmark">PHP Reference – is_int() function</a></h3><p>(PHP 4, PHP 5) is_int — Find whether the type of a variable is integer Parameters The variable being evaluated. Note: To test if a ...</p></div></li><li><div class="seo_alrp_rl_content"><h3><a href="http://www.whileifblog.com/2012/02/18/php-memory-usage-information/" rel="bookmark">PHP &#8211; Memory Usage Information</a></h3><p>By observing the memory usage of your scripts, you may be able optimize your code better. PHP has a garbage collector and a pretty complex ...</p></div></li></ul></div>]]></content:encoded>
			<wfw:commentRss>http://www.whileifblog.com/2012/04/25/php-5-4-function-array-dereferencing/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP 5.4 &#8211; Traits, Closures, and Prototype-based Programming</title>
		<link>http://www.whileifblog.com/2012/04/24/php-5-4-traits-closures-and-prototype-based-programming/</link>
		<comments>http://www.whileifblog.com/2012/04/24/php-5-4-traits-closures-and-prototype-based-programming/#comments</comments>
		<pubDate>Tue, 24 Apr 2012 08:12:44 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[closures]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[traits]]></category>

		<guid isPermaLink="false">http://www.whileifblog.com/?p=909</guid>
		<description><![CDATA[According to Wikipedia, prototype-based programming is “a style of object-oriented programming in which classes are not present, and behavior reuse (known as inheritance in class-based languages) is performed via a process of cloning existing objects that serve as prototypes”With magic &#8230;<p class="read-more"><a href="http://www.whileifblog.com/2012/04/24/php-5-4-traits-closures-and-prototype-based-programming/">Read more &#187;</a></p><p><a href="http://www.whileifblog.com/2012/04/24/php-5-4-traits-closures-and-prototype-based-programming/">PHP 5.4 &#8211; Traits, Closures, and Prototype-based Programming</a> is a post from: <a href="http://www.whileifblog.com">WhileIf Blog</a></p>
]]></description>
			<content:encoded><![CDATA[<p><em>According to Wikipedia, prototype-based programming is “a style of object-oriented programming in which classes are not present, and behavior reuse (known as inheritance in class-based languages) is performed via a process of cloning existing objects that serve as prototypes”</em>With magic methods, traits, and anonymous functions &#8211; this is now possible in PHP.</p>
<pre class="brush: php; title: ; notranslate">
    class SimpleXML
    {
        use \Prototype;
    }

    $SimpleXML = new SimpleXML();
    $SimpleXML-&gt;load = function( $path )
    {
        if( file_exists( $path ))
            return simplexml_load_file( $path );

        return null;
    };

    $SimpleXML-&gt;load = function( $data )
    {
        return simplexml_load_string( $data );
    };
</pre>
<p>The previous code uses SimpleXML and contains a class declaration by that name with a single trait that is shown later. The class is then instantiated and 2 anonymous functions are added to the prototype. With the above code and the Prototype trait, the following statements are both possible.</p>
<pre class="brush: php; title: ; notranslate">
    $doc = $SimpleXML-&gt;load( 'test.xml' );
    $doc = $SimpleXML-&gt;load( 'test' );
</pre>
<p>The first method triggers the Closure that uses simplexml_load_file and the second method triggers simplexml_load_string respectively. You may notice that both functions are assigned to the same property. Using the prototype trait, it is possible to define 2 functions with the same name and number of arguments. This is a lot like traditional method overloading which was previously not possible with PHP. Overloading in this way is primitive and requires that overloaded methods meet certain criteria, namely that a given method return null if the input is not valid.</p>
<p><span id="more-909"></span></p>
<pre class="brush: php; title: ; notranslate">
trait Prototype
{
    private
            $_props = array(),
            $_methods = array();

    function &amp;__get( $name )
    {
        if( array_key_exists( $name, $this-&gt;_props ))
        {
            return $this-&gt;_props[$name];
        }
        elseif( array_key_exists( $name, $this-&gt;_methods ))
        {
            return $this-&gt;_methods[$name];
        }

        return null;
    }

    function __set( $name, $value )
    {
        if( is_object( $value ) &amp;&amp; is_callable( $value ))
        {
            if( !array_key_exists( $name, $this-&gt;_methods ))
            {
                $this-&gt;_methods[ $name ] = array();
            }

            if( $value instanceof \Closure )
            {
                $value = $value-&gt;bindTo( $this );
            }

            $this-&amp;gt;_methods[ $name ][] = $value;
        }
        else
        {
            $this-&gt;_props[ $name ] = $value;
        }
    }

    function __call( $name, $args = array() )
    {
        if( array_key_exists( $name, $this-&gt;_methods ))
        {
            $methods = $this-&gt;_methods[ $name ];

            if( is_array( $methods ))
            {
                foreach( $methods as $method )
                {
                    if( !is_null( $result = call_user_func_array( $method, $args ) ) )
                    {
                        return $result;
                    }
                }
            }
        }

        return null;
    }
}
</pre>
<p>The __get method This one is fairly self-explanatory as it simply returns a property value or a Closure stored in _props and _methods respectively. The __set method This method first checks to see if the value is in-fact callable. An example of a callable value would be an anonymous function or a class that implements the __invoke method. If the value is a Closure, the BindTo method is called. BindTo simply allows the use of the $this keyword in the closure as you would use it in a normal method. Finally, if the value is not callable, the prototype assumes it is a property and assigns it to the _props array. The __call method This method iterates through all of the matching methods until one returns something other than null. Now the prototype above is well, a prototype. As per the original definition of prototype-based programming, it must also be possible to easily extend your prototype by cloning all of it’s methods and properties.</p>
<div id="crp_related"><h2 class="title" style="padding-top:15px">What others are reading:</h2><ul><li><a href="http://www.whileifblog.com/2011/09/11/php-obtain-information-about-a-class/" rel="bookmark" class="crp_title">PHP &#8211; Obtain information about a class</a></li><li><a href="http://www.whileifblog.com/2012/05/01/php-5-4-code-reuse-using-traits/" rel="bookmark" class="crp_title">PHP 5.4 &#8211; Code reuse using Traits</a></li><li><a href="http://www.whileifblog.com/2012/04/30/php-5-4-core-changes/" rel="bookmark" class="crp_title">PHP 5.4 &#8211; Core changes</a></li><li><a href="http://www.whileifblog.com/2012/02/08/jquery-a-single-hover-function/" rel="bookmark" class="crp_title">JQuery &#8211; A Single Hover Function</a></li></ul></div><p><a href="http://www.whileifblog.com/2012/04/24/php-5-4-traits-closures-and-prototype-based-programming/">PHP 5.4 &#8211; Traits, Closures, and Prototype-based Programming</a> is a post from: <a href="http://www.whileifblog.com">WhileIf Blog</a></p>
<div id="seo_alrp_related"><h2>Posts Related to PHP 5.4 - Traits, Closures, and Prototype-based Programming</h2><ul><li><div class="seo_alrp_rl_content"><h3><a href="http://www.whileifblog.com/2011/09/11/php-obtain-information-about-a-class/" rel="bookmark">PHP &#8211; Obtain information about a class</a></h3><p>PHP5 includes interesting APIs to obtain information (list of members and methods) about the internal classes or interfaces. Example of PHP5 interesting classes/interfaces: Exception, Iterator. ...</p></div></li><li><div class="seo_alrp_rl_content"><h3><a href="http://www.whileifblog.com/2012/05/01/php-5-4-code-reuse-using-traits/" rel="bookmark">PHP 5.4 &#8211; Code reuse using Traits</a></h3><p>Traits enables a developer to reuse sets of methods freely in several independent classes living in different class hierarchies. PHP being a single inheritance language, ...</p></div></li><li><div class="seo_alrp_rl_content"><h3><a href="http://www.whileifblog.com/2012/04/30/php-5-4-core-changes/" rel="bookmark">PHP 5.4 &#8211; Core changes</a></h3><p>Along with the above mentioned additions, a variety of new functions and additional methods to existing classes have been added. Classes now support the Class::{expr}() ...</p></div></li><li><div class="seo_alrp_rl_content"><h3><a href="http://www.whileifblog.com/2012/02/08/jquery-a-single-hover-function/" rel="bookmark">JQuery &#8211; A Single Hover Function</a></h3><p>As of jQuery 1.4, we can now pass only a single function to the hover method. Before, both the in and out methods were required. ...</p></div></li><li><div class="seo_alrp_rl_content"><h3><a href="http://www.whileifblog.com/2012/03/22/php-anonymous-functions-and-closures-part-i/" rel="bookmark">PHP &#8211; Anonymous Functions and Closures (Part I)</a></h3><p>Anonymous functions are functions that are defined without being bound to a proper name. Typically, anonymous functions are used only a limited number of times ...</p></div></li></ul></div>]]></content:encoded>
			<wfw:commentRss>http://www.whileifblog.com/2012/04/24/php-5-4-traits-closures-and-prototype-based-programming/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Linux &#8211; scp copy all hidden dot files</title>
		<link>http://www.whileifblog.com/2012/04/22/linux-scp-copy-all-hidden-dot-files/</link>
		<comments>http://www.whileifblog.com/2012/04/22/linux-scp-copy-all-hidden-dot-files/#comments</comments>
		<pubDate>Sun, 22 Apr 2012 17:01:08 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Manual/Reference]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[kubuntu]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[linux mint]]></category>
		<category><![CDATA[Operating Systems]]></category>
		<category><![CDATA[ubuntu]]></category>

		<guid isPermaLink="false">http://www.whileifblog.com/?p=904</guid>
		<description><![CDATA[The scp command copies files between servers (computers) on a network. It uses ssh for data transfer, and uses the same authentication and provides the same security as ssh. scp Command The correct syntax is as follows to copy all &#8230;<p class="read-more"><a href="http://www.whileifblog.com/2012/04/22/linux-scp-copy-all-hidden-dot-files/">Read more &#187;</a></p><p><a href="http://www.whileifblog.com/2012/04/22/linux-scp-copy-all-hidden-dot-files/">Linux &#8211; scp copy all hidden dot files</a> is a post from: <a href="http://www.whileifblog.com">WhileIf Blog</a></p>
]]></description>
			<content:encoded><![CDATA[<p>The <strong>scp</strong> command copies files between servers (computers) on a network. It uses <strong>ssh</strong> for data transfer, and uses the same authentication and provides the same security as ssh.</p>
<p><strong>scp Command</strong></p>
<p>The correct syntax is as follows to copy all files including hidden dot files:</p>
<pre class="brush: php; title: ; notranslate">
$ scp -rp /path/to/source/. user@server2:/path/to/dest/
</pre>
<p>Where,</p>
<p>-r : Recursively copy entire directories. Note that scp follows symbolic links encountered in the tree trave<br />
-p : Preserves modification times, access times, and modes from the original file.<br />
/path/to/source/. : Appending a dot (.) symbol is very important when you specify /path/to/source as a source path. If you skip dot in path it will only copy normal files and scp will skip all hidden files.<br />
rsync Command</p>
<p>I recommend that you use rsync command to copy files between Unix / Linux based servers and workstations.</p>
<pre class="brush: php; title: ; notranslate">
$ rsync -avzP /path/to/source/ user@server2:/path/to/dest/
</pre>
<p>OR</p>
<pre class="brush: php; title: ; notranslate">
$ rsync -avzP /path/to/source/ user@192.168.1.5:/path/to/dest/
</pre>
<div id="crp_related"><h2 class="title" style="padding-top:15px">What others are reading:</h2><ul><li><a href="http://www.whileifblog.com/2011/01/30/linux-exporting-files-from-git-similar-to-svn-export/" rel="bookmark" class="crp_title">Linux &#8211; Exporting Files From Git (similar to SVN export)</a></li><li><a href="http://www.whileifblog.com/2012/04/12/linux-set-your-path-variable-using-set-or-export-command/" rel="bookmark" class="crp_title">Linux &#8211; Set your PATH Variable using set or export command</a></li><li><a href="http://www.whileifblog.com/2010/07/12/php-optimize-your-jpeg-images-with-imagemagick/" rel="bookmark" class="crp_title">PHP &#8211; Optimize your JPEG images with ImageMagick</a></li><li><a href="http://www.whileifblog.com/2012/04/02/linux-how-to-use-rsync-command-to-backup-directory/" rel="bookmark" class="crp_title">Linux &#8211; How To Use rsync Command To Backup Directory</a></li></ul></div><p><a href="http://www.whileifblog.com/2012/04/22/linux-scp-copy-all-hidden-dot-files/">Linux &#8211; scp copy all hidden dot files</a> is a post from: <a href="http://www.whileifblog.com">WhileIf Blog</a></p>
<div id="seo_alrp_related"><h2>Posts Related to Linux - scp copy all hidden dot files</h2><ul><li><div class="seo_alrp_rl_content"><h3><a href="http://www.whileifblog.com/2011/01/30/linux-exporting-files-from-git-similar-to-svn-export/" rel="bookmark">Linux &#8211; Exporting Files From Git (similar to SVN export)</a></h3><p>I use git for just about all my code now. For some of my work, I need to export portions of or all of a ...</p></div></li><li><div class="seo_alrp_rl_content"><h3><a href="http://www.whileifblog.com/2012/04/02/linux-how-to-use-rsync-command-to-backup-directory/" rel="bookmark">Linux &#8211; How To Use rsync Command To Backup Directory</a></h3><p>rsync command easily backup your home directory to local secondary hard disk or remote server using ssh protocol. rsync is a software application for Unix ...</p></div></li><li><div class="seo_alrp_rl_content"><h3><a href="http://www.whileifblog.com/2011/02/09/ubuntu-merge-avi-mpg-or-mpeg-files/" rel="bookmark">Ubuntu &#8211; Merge avi (mpg or mpeg) files</a></h3><p>We will merge the files using cat and mencoder. For that purpose we will first install the appropriate software to use mencoder, then we will ...</p></div></li><li><div class="seo_alrp_rl_content"><h3><a href="http://www.whileifblog.com/2011/09/28/ubuntu-change-the-date-and-time-or-any-other-exif-image-meta-data/" rel="bookmark">Ubuntu &#8211; Change the date and time (or any other EXIF image meta-data)</a></h3><p>There is a very flexible and easy Linux tool that helps you change the EXIF meta-data of images. It allows you to change individual files ...</p></div></li><li><div class="seo_alrp_rl_content"><h3><a href="http://www.whileifblog.com/2011/09/01/ubuntu-create-iso-images-from-command-line/" rel="bookmark">Ubuntu &#8211; Create ISO Images from Command-Line</a></h3><p>For this tutorial we’ll use the genisoimage utility, developed as part of the cdrkit project. genisoimage is a command-line tool for creating ISO 9660 filesystem ...</p></div></li></ul></div>]]></content:encoded>
			<wfw:commentRss>http://www.whileifblog.com/2012/04/22/linux-scp-copy-all-hidden-dot-files/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Linux &#8211; What is tmpfs ?</title>
		<link>http://www.whileifblog.com/2012/04/19/linux-what-is-tmpfs/</link>
		<comments>http://www.whileifblog.com/2012/04/19/linux-what-is-tmpfs/#comments</comments>
		<pubDate>Thu, 19 Apr 2012 21:20:51 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Manual/Reference]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[fedora]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[suse]]></category>
		<category><![CDATA[tmpfs]]></category>
		<category><![CDATA[ubuntu]]></category>

		<guid isPermaLink="false">http://www.whileifblog.com/?p=901</guid>
		<description><![CDATA[tmpfs is a file system that stores all the files in virtual memory. tmpfs doesn&#8217;t create any files on your hard drive. So if you unmount a tmpfs file system, all the files residing in it are lost for ever. &#8230;<p class="read-more"><a href="http://www.whileifblog.com/2012/04/19/linux-what-is-tmpfs/">Read more &#187;</a></p><p><a href="http://www.whileifblog.com/2012/04/19/linux-what-is-tmpfs/">Linux &#8211; What is tmpfs ?</a> is a post from: <a href="http://www.whileifblog.com">WhileIf Blog</a></p>
]]></description>
			<content:encoded><![CDATA[<p><strong>tmpfs</strong> is a file system that stores all the files in virtual memory. tmpfs doesn&#8217;t create any files on your hard drive. So if you unmount a tmpfs file system, all the files residing in it are lost for ever.</p>
<p><strong>tmpfs</strong> is similar to ramfs and RAM disk but with a few additional features. tmpfs is able to grow or shrink its space to accommodate files,and it can use swap space to store unneeded data. ramfs and RAM disk doesn&#8217;t have this capability.</p>
<p><strong>Use of tmpfs in Linux</strong></p>
<p>Debian, Ubuntu, Fedora, openSUSE, and many other Linux distributions use tmpfs file system to store all their run time data.</p>
<p>For example, in Ubuntu, if I execute the following command, I find that /run is mounted on a tmpfs filesystem.</p>
<pre class="brush: php; title: ; notranslate">

$ df -h | grep tmpfs
tmpfs 401M 880K 400M 1% /run
</pre>
<p>The /run directory and its sub-directories store run time data of Linux processes and any running applications that may want to store their data.</p>
<p>You can check the actual [RAM + Swap] use of a tmpfs using df and du commands<br />
Since <strong>tmpfs</strong> lives completely in the page cache and on swap, all tmpfs pages currently in memory will show up as cached.</p>
<p><strong>tmpfs</strong> in some form or other is currently implemented in Sun/Solaris, Linux, and BSDs.</p>
<div id="crp_related"><h2 class="title" style="padding-top:15px">What others are reading:</h2><ul><li><a href="http://www.whileifblog.com/2011/08/29/linux-termina-system-info-cheat-sheet/" rel="bookmark" class="crp_title">Linux &#8211; Terminal system info, cheat sheet</a></li><li><a href="http://www.whileifblog.com/2011/08/23/linux-free-some-space-with-apt-get/" rel="bookmark" class="crp_title">Linux &#8211; Free some space with apt-get</a></li><li><a href="http://www.whileifblog.com/2012/04/02/linux-how-to-use-rsync-command-to-backup-directory/" rel="bookmark" class="crp_title">Linux &#8211; How To Use rsync Command To Backup Directory</a></li><li><a href="http://www.whileifblog.com/2012/03/06/linux-view-running-processes/" rel="bookmark" class="crp_title">Linux &#8211; View running processes</a></li></ul></div><p><a href="http://www.whileifblog.com/2012/04/19/linux-what-is-tmpfs/">Linux &#8211; What is tmpfs ?</a> is a post from: <a href="http://www.whileifblog.com">WhileIf Blog</a></p>
<div id="seo_alrp_related"><h2>Posts Related to Linux - What is tmpfs ?</h2><ul><li><div class="seo_alrp_rl_content"><h3><a href="http://www.whileifblog.com/2012/04/02/linux-how-to-use-rsync-command-to-backup-directory/" rel="bookmark">Linux &#8211; How To Use rsync Command To Backup Directory</a></h3><p>rsync command easily backup your home directory to local secondary hard disk or remote server using ssh protocol. rsync is a software application for Unix ...</p></div></li><li><div class="seo_alrp_rl_content"><h3><a href="http://www.whileifblog.com/2011/08/23/linux-free-some-space-with-apt-get/" rel="bookmark">Linux &#8211; Free some space with apt-get</a></h3><p>Problems of space? Perhaps the fault is not entirely of your music files, images and video, but installation packages and unnecessary dependencies in the system. ...</p></div></li><li><div class="seo_alrp_rl_content"><h3><a href="http://www.whileifblog.com/2011/01/14/ubuntu-shorten-boot-time-with-profiling/" rel="bookmark">Ubuntu &#8211; Shorten boot time with profiling</a></h3><p>Ubuntu Linux devs have done a great job with the boot time. There is however a bit more you can do by profiling your boot. ...</p></div></li><li><div class="seo_alrp_rl_content"><h3><a href="http://www.whileifblog.com/2011/09/28/ubuntu-change-the-date-and-time-or-any-other-exif-image-meta-data/" rel="bookmark">Ubuntu &#8211; Change the date and time (or any other EXIF image meta-data)</a></h3><p>There is a very flexible and easy Linux tool that helps you change the EXIF meta-data of images. It allows you to change individual files ...</p></div></li><li><div class="seo_alrp_rl_content"><h3><a href="http://www.whileifblog.com/2011/02/06/linux-log-files-and-usage/" rel="bookmark">Linux &#8211; Log files and usage</a></h3><p>All logs are stored in /var/log directory under Ubuntu (and other Linux distro). /var/log/messages : General log messages /var/log/boot : System boot log /var/log/debug : ...</p></div></li></ul></div>]]></content:encoded>
			<wfw:commentRss>http://www.whileifblog.com/2012/04/19/linux-what-is-tmpfs/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Linux &#8211; Howto find uptime and system load</title>
		<link>http://www.whileifblog.com/2012/04/18/linux-howto-find-uptime-and-system-load/</link>
		<comments>http://www.whileifblog.com/2012/04/18/linux-howto-find-uptime-and-system-load/#comments</comments>
		<pubDate>Wed, 18 Apr 2012 18:36:10 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Software]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[linux mint]]></category>
		<category><![CDATA[Maverick Meerkat]]></category>
		<category><![CDATA[Operating Systems]]></category>
		<category><![CDATA[ubuntu]]></category>

		<guid isPermaLink="false">http://www.whileifblog.com/?p=896</guid>
		<description><![CDATA[There is a simple command in Linux that tells you how long your system has been running. The name of the command is uptime. Uptime command gives a one line display that offers the following information - The current time, &#8230;<p class="read-more"><a href="http://www.whileifblog.com/2012/04/18/linux-howto-find-uptime-and-system-load/">Read more &#187;</a></p><p><a href="http://www.whileifblog.com/2012/04/18/linux-howto-find-uptime-and-system-load/">Linux &#8211; Howto find uptime and system load</a> is a post from: <a href="http://www.whileifblog.com">WhileIf Blog</a></p>
]]></description>
			<content:encoded><![CDATA[<p>There is a simple command in Linux that tells you how long your system has been running. The name of the command is <strong>uptime</strong>.</p>
<p>Uptime command gives a one line display that offers the following information -</p>
<p>The current time,<br />
How long the system has been running,<br />
How many users are currently logged on, and<br />
The system load averages for the past 1, 5, and 15 minutes.</p>
<p>System load averages is the average number of processes that are either in a runnable or un-interruptable state.</p>
<p>When I ran the uptime command on my Ubuntu machine, I got the following output.</p>
<pre class="brush: php; title: ; notranslate">

$ uptime
08:25:48 up 1:21, 2 users, load average: 1.00, 1.01, 1.03
</pre>
<p>&nbsp;</p>
<p>From the output, we can infer that the current time is 8:25 AM, the machine has been running continuously without a reboot for 1 hr and 21 minutes, and there are 2 users currently logged in to the system.</p>
<p>The load average for the past 1, 5 and 15 minutes is 1.00, 1.01, and 1.03 respectively.</p>
<p>Uptime is a simple command that offers useful information to the system administrator.</p>
<div id="crp_related"><h2 class="title" style="padding-top:15px">What others are reading:</h2><ul><li><a href="http://www.whileifblog.com/2012/04/07/ubuntu-how-to-use-shutdown-command/" rel="bookmark" class="crp_title">Ubuntu &#8211; How to use shutdown command</a></li><li><a href="http://www.whileifblog.com/2012/03/06/linux-view-running-processes/" rel="bookmark" class="crp_title">Linux &#8211; View running processes</a></li><li><a href="http://www.whileifblog.com/2012/04/10/php-program-execution-functions/" rel="bookmark" class="crp_title">PHP &#8211; Program execution functions</a></li><li><a href="http://www.whileifblog.com/2012/04/19/linux-what-is-tmpfs/" rel="bookmark" class="crp_title">Linux &#8211; What is tmpfs ?</a></li></ul></div><p><a href="http://www.whileifblog.com/2012/04/18/linux-howto-find-uptime-and-system-load/">Linux &#8211; Howto find uptime and system load</a> is a post from: <a href="http://www.whileifblog.com">WhileIf Blog</a></p>
<div id="seo_alrp_related"><h2>Posts Related to Linux - Howto find uptime and system load</h2><ul><li><div class="seo_alrp_rl_content"><h3><a href="http://www.whileifblog.com/2012/03/06/linux-view-running-processes/" rel="bookmark">Linux &#8211; View running processes</a></h3><p>Anyone that has used a Windows Operating System should be familiar with Task Manager, the program that allows you to end processes and to view ...</p></div></li><li><div class="seo_alrp_rl_content"><h3><a href="http://www.whileifblog.com/2012/04/07/ubuntu-how-to-use-shutdown-command/" rel="bookmark">Ubuntu &#8211; How to use shutdown command</a></h3><p>shutdown arranges for the system to be brought down in a safe way. All logged-in users are notified that the system is going down and, ...</p></div></li><li><div class="seo_alrp_rl_content"><h3><a href="http://www.whileifblog.com/2012/04/10/php-program-execution-functions/" rel="bookmark">PHP &#8211; Program execution functions</a></h3><p>This post introduces several functions (in addition to the backticks execution operator) used to execute system-level programs via a PHP script. Although at first glance ...</p></div></li><li><div class="seo_alrp_rl_content"><h3><a href="http://www.whileifblog.com/2010/09/28/jquery-lazy-load-content-for-speed-and-seo-benefits/" rel="bookmark">Jquery &#8211; Lazy load content for speed and SEO benefits</a></h3><p>Another way to speed up your page loads and neaten up the HTML that search spiders see is to lazy load whole chunks of it ...</p></div></li><li><div class="seo_alrp_rl_content"><h3><a href="http://www.whileifblog.com/2012/04/03/linux-how-to-schedule-tasks-using-the-at-command/" rel="bookmark">Linux &#8211; How to schedule tasks using the &#8216;at&#8217; command</a></h3><p>Scheduling jobs is an essential part of administering Linux servers. We took a look at how to schedule jobs on Linux machine using the cron ...</p></div></li></ul></div>]]></content:encoded>
			<wfw:commentRss>http://www.whileifblog.com/2012/04/18/linux-howto-find-uptime-and-system-load/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Linux- Find Out Open Files Command</title>
		<link>http://www.whileifblog.com/2012/04/17/linux-find-out-open-files-command/</link>
		<comments>http://www.whileifblog.com/2012/04/17/linux-find-out-open-files-command/#comments</comments>
		<pubDate>Tue, 17 Apr 2012 18:24:19 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Manual/Reference]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[linux mint]]></category>
		<category><![CDATA[ubuntu]]></category>

		<guid isPermaLink="false">http://www.whileifblog.com/?p=892</guid>
		<description><![CDATA[By default Linux kernel place an limit (for security purpose) on how many open file descriptors are allowed on the Linux server or desktop system. The /proc/sys/fs/file-nr is a read-only file and provides the the number of files presently opened. &#8230;<p class="read-more"><a href="http://www.whileifblog.com/2012/04/17/linux-find-out-open-files-command/">Read more &#187;</a></p><p><a href="http://www.whileifblog.com/2012/04/17/linux-find-out-open-files-command/">Linux- Find Out Open Files Command</a> is a post from: <a href="http://www.whileifblog.com">WhileIf Blog</a></p>
]]></description>
			<content:encoded><![CDATA[<p>By default Linux kernel place an limit (for security purpose) on how many open file descriptors are allowed on the Linux server or desktop system. The /proc/sys/fs/file-nr is a read-only file and provides the the number of files presently opened.</p>
<p>To see current status, enter:</p>
<p>
<pre class="brush: php; title: ; notranslate">&lt;br /&gt;$ cat /proc/sys/fs/file-nr&lt;/p&gt;&lt;p&gt;</pre>
</p>
<p>OR</p>
<p>
<pre class="brush: php; title: ; notranslate">&lt;br /&gt;$ /sbin/sysctl fs.file-nr&lt;/p&gt;&lt;p&gt;</pre>
</p>
<p>Sample outputs:</p>
<p>fs.file-nr = 6272 0 70000<br />The above output contains three numbers as follows:</p>
<p>6272: The number of allocated file handles.<br />0: The number of free file handles.<br />70000: The maximum number of file handles.</p>
<p>The Linux kernel allocates file handles dynamically, but it doesn&#8217;t free them again. If the number of allocated files is close to the maximum, you should consider increasing the maximum open file by editing <strong>/etc/sysctl.conf</strong> file.</p>
<div id="crp_related"><h2 class="title" style="padding-top:15px">What others are reading:</h2><ul><li><a href="http://www.whileifblog.com/2011/02/06/linux-log-files-and-usage/" rel="bookmark" class="crp_title">Linux &#8211; Log files and usage</a></li><li><a href="http://www.whileifblog.com/2012/04/19/linux-what-is-tmpfs/" rel="bookmark" class="crp_title">Linux &#8211; What is tmpfs ?</a></li><li><a href="http://www.whileifblog.com/2012/04/15/linux-find-out-cpu-architecture-information/" rel="bookmark" class="crp_title">Linux &#8211; Find Out CPU Architecture Information</a></li><li><a href="http://www.whileifblog.com/2011/08/29/linux-termina-system-info-cheat-sheet/" rel="bookmark" class="crp_title">Linux &#8211; Terminal system info, cheat sheet</a></li></ul></div><p><a href="http://www.whileifblog.com/2012/04/17/linux-find-out-open-files-command/">Linux- Find Out Open Files Command</a> is a post from: <a href="http://www.whileifblog.com">WhileIf Blog</a></p>
<div id="seo_alrp_related"><h2>Posts Related to Linux- Find Out Open Files Command</h2><ul><li><div class="seo_alrp_rl_content"><h3><a href="http://www.whileifblog.com/2011/02/06/linux-log-files-and-usage/" rel="bookmark">Linux &#8211; Log files and usage</a></h3><p>All logs are stored in /var/log directory under Ubuntu (and other Linux distro). /var/log/messages : General log messages /var/log/boot : System boot log /var/log/debug : ...</p></div></li><li><div class="seo_alrp_rl_content"><h3><a href="http://www.whileifblog.com/2012/03/20/php-creating-a-zip-compressed-archive/" rel="bookmark">PHP &#8211; Creating a ZIP compressed archive</a></h3><p>Steps in above code: - We create an instance of ZipArchive class. - We open (create if not present) a zip archive file by the ...</p></div></li><li><div class="seo_alrp_rl_content"><h3><a href="http://www.whileifblog.com/2012/03/30/ubuntu-install-vlc-2-0-1-in-12-0411-10/" rel="bookmark">Ubuntu &#8211; Install VLC 2.0.1 in 12.04/11.10</a></h3><p>VLC is a free and open source cross-platform multimedia player and framework that plays most multimedia files as well as DVD, Audio CD, VCD, and ...</p></div></li><li><div class="seo_alrp_rl_content"><h3><a href="http://www.whileifblog.com/2012/04/19/linux-what-is-tmpfs/" rel="bookmark">Linux &#8211; What is tmpfs ?</a></h3><p>tmpfs is a file system that stores all the files in virtual memory. tmpfs doesn't create any files on your hard drive. So if you ...</p></div></li><li><div class="seo_alrp_rl_content"><h3><a href="http://www.whileifblog.com/2011/01/30/linux-exporting-files-from-git-similar-to-svn-export/" rel="bookmark">Linux &#8211; Exporting Files From Git (similar to SVN export)</a></h3><p>I use git for just about all my code now. For some of my work, I need to export portions of or all of a ...</p></div></li></ul></div>]]></content:encoded>
			<wfw:commentRss>http://www.whileifblog.com/2012/04/17/linux-find-out-open-files-command/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Linux &#8211; Find Out CPU Architecture Information</title>
		<link>http://www.whileifblog.com/2012/04/15/linux-find-out-cpu-architecture-information/</link>
		<comments>http://www.whileifblog.com/2012/04/15/linux-find-out-cpu-architecture-information/#comments</comments>
		<pubDate>Sun, 15 Apr 2012 10:44:01 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Hardware]]></category>
		<category><![CDATA[Software]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[cpu]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[linux mint]]></category>
		<category><![CDATA[Maverick Meerkat]]></category>
		<category><![CDATA[unix]]></category>

		<guid isPermaLink="false">http://www.whileifblog.com/?p=887</guid>
		<description><![CDATA[You can use /proc/cpuinfo file or use the lscpu command to get info about CPU architecture. It will display information like: Number of CPUs Threads Cores Sockets NUMA nodes Information about CPU caches, CPU family, model and stepping. Alternatively, it &#8230;<p class="read-more"><a href="http://www.whileifblog.com/2012/04/15/linux-find-out-cpu-architecture-information/">Read more &#187;</a></p><p><a href="http://www.whileifblog.com/2012/04/15/linux-find-out-cpu-architecture-information/">Linux &#8211; Find Out CPU Architecture Information</a> is a post from: <a href="http://www.whileifblog.com">WhileIf Blog</a></p>
]]></description>
			<content:encoded><![CDATA[<p>You can use <strong>/proc/cpuinfo</strong> file or use the <strong>lscpu</strong> command to get info about CPU architecture.</p>
<p>It will display information like:</p>
<p>Number of CPUs<br />
Threads<br />
Cores<br />
Sockets<br />
NUMA nodes<br />
Information about CPU caches,<br />
CPU family, model and stepping.</p>
<p>Alternatively, it can print out in parsable format including how different caches are shared by different CPUs, which can also be fed to other programs.</p>
<p><span id="more-887"></span></p>
<p>Open a terminal and type the following command:</p>
<pre class="brush: php; title: ; notranslate">

$ less /proc/cpuinfo
</pre>
<p>OR</p>
<pre class="brush: php; title: ; notranslate">
$ lscpu
</pre>
<p>Samp-le outputs:</p>
<pre class="brush: php; title: ; notranslate">

Architecture: x86_64
CPU op-mode(s): 32-bit, 64-bit
CPU(s): 8
Thread(s) per core: 2
Core(s) per socket: 4
CPU socket(s): 1
NUMA node(s): 1
Vendor ID: GenuineIntel
CPU family: 6
Model: 30
Stepping: 5
CPU MHz: 1199.000
Virtualization: VT-x
L1d cache: 32K
L1i cache: 32K
L2 cache: 256K
L3 cache: 8192K
</pre>
<div id="crp_related"><h2 class="title" style="padding-top:15px">What others are reading:</h2><ul><li><a href="http://www.whileifblog.com/2012/05/08/php-cpu-usage-information/" rel="bookmark" class="crp_title">PHP &#8211; CPU Usage Information</a></li><li><a href="http://www.whileifblog.com/2012/03/06/linux-view-running-processes/" rel="bookmark" class="crp_title">Linux &#8211; View running processes</a></li><li><a href="http://www.whileifblog.com/2011/08/29/linux-termina-system-info-cheat-sheet/" rel="bookmark" class="crp_title">Linux &#8211; Terminal system info, cheat sheet</a></li><li><a href="http://www.whileifblog.com/2012/03/05/php-clearstatcache-explained/" rel="bookmark" class="crp_title">PHP &#8211; clearstatcache() Explained</a></li></ul></div><p><a href="http://www.whileifblog.com/2012/04/15/linux-find-out-cpu-architecture-information/">Linux &#8211; Find Out CPU Architecture Information</a> is a post from: <a href="http://www.whileifblog.com">WhileIf Blog</a></p>
<div id="seo_alrp_related"><h2>Posts Related to Linux - Find Out CPU Architecture Information</h2><ul><li><div class="seo_alrp_rl_content"><h3><a href="http://www.whileifblog.com/2012/03/05/php-clearstatcache-explained/" rel="bookmark">PHP &#8211; clearstatcache() Explained</a></h3><p>For the functions like is_file(), file_exists(), etc PHP caches the result of this function for each file for faster performance if function called again. But ...</p></div></li><li><div class="seo_alrp_rl_content"><h3><a href="http://www.whileifblog.com/2012/05/08/php-cpu-usage-information/" rel="bookmark">PHP &#8211; CPU Usage Information</a></h3><p>For this, we are going to utilize the getrusage() function. Keep in mind that this is not available on Windows platforms. That may look a ...</p></div></li><li><div class="seo_alrp_rl_content"><h3><a href="http://www.whileifblog.com/2012/03/06/linux-view-running-processes/" rel="bookmark">Linux &#8211; View running processes</a></h3><p>Anyone that has used a Windows Operating System should be familiar with Task Manager, the program that allows you to end processes and to view ...</p></div></li><li><div class="seo_alrp_rl_content"><h3><a href="http://www.whileifblog.com/2011/08/23/linux-free-some-space-with-apt-get/" rel="bookmark">Linux &#8211; Free some space with apt-get</a></h3><p>Problems of space? Perhaps the fault is not entirely of your music files, images and video, but installation packages and unnecessary dependencies in the system. ...</p></div></li><li><div class="seo_alrp_rl_content"><h3><a href="http://www.whileifblog.com/2012/03/29/memcached-flush-contents-of-a-server-using-command-line/" rel="bookmark">Memcached &#8211; Flush Contents Of a Server Using Command Line</a></h3><p>You can invalidate all existing cache items using the flush_all command. This command does not pause the server, as it returns immediately. It does not ...</p></div></li></ul></div>]]></content:encoded>
			<wfw:commentRss>http://www.whileifblog.com/2012/04/15/linux-find-out-cpu-architecture-information/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Linux &#8211; Set your PATH Variable using set or export command</title>
		<link>http://www.whileifblog.com/2012/04/12/linux-set-your-path-variable-using-set-or-export-command/</link>
		<comments>http://www.whileifblog.com/2012/04/12/linux-set-your-path-variable-using-set-or-export-command/#comments</comments>
		<pubDate>Thu, 12 Apr 2012 20:02:06 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Software]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[linux mint]]></category>
		<category><![CDATA[Operating Systems]]></category>
		<category><![CDATA[ubuntu]]></category>

		<guid isPermaLink="false">http://www.whileifblog.com/?p=884</guid>
		<description><![CDATA[&#160; The syntax for setting path under UNIX / Linux dependent on which shell you are using. BASH / SH shell uses following syntax: export PATH=$PATH:/path/to/dir1:/path/to/dir2 For tcsh or csh, shell enter: set PATH = ($PATH /path/to/dir1 /path/to/dir2) You can &#8230;<p class="read-more"><a href="http://www.whileifblog.com/2012/04/12/linux-set-your-path-variable-using-set-or-export-command/">Read more &#187;</a></p><p><a href="http://www.whileifblog.com/2012/04/12/linux-set-your-path-variable-using-set-or-export-command/">Linux &#8211; Set your PATH Variable using set or export command</a> is a post from: <a href="http://www.whileifblog.com">WhileIf Blog</a></p>
]]></description>
			<content:encoded><![CDATA[<p>&nbsp;</p>
<p>The syntax for setting path under UNIX / Linux dependent on which shell you are using.</p>
<p>BASH / SH shell uses following syntax:</p>
<p>export PATH=$PATH:/path/to/dir1:/path/to/dir2</p>
<p>For tcsh or csh, shell enter:</p>
<p>set PATH = ($PATH /path/to/dir1 /path/to/dir2)</p>
<p>You can type above command at the terminal or add it to your .bashrc (for BASH/sh shell) or .cshrc (for chs / tcsh shell) so that PATH can be set each time you login into box.</p>
<p>For example add /usr/local/bin to your path under BASH, enter:<br />
export PATH=$PATH:/usr/local/bin</p>
<p>Or add as follows to your .bashrc file:</p>
<pre class="brush: php; title: ; notranslate">
echo 'export PATH=$PATH:/usr/local/bin' &gt;&gt; ~/.bashrc
</pre>
<p>If you are using CSH / TCSH, enter:</p>
<pre class="brush: php; title: ; notranslate">
echo 'set PATH = ($PATH /usr/local/bin /scripts/admin)' &gt;&gt; ~/.cshrc
</pre>
<p>To display path settings, enter:</p>
<pre class="brush: php; title: ; notranslate">
$ echo $PATH
</pre>
<div id="crp_related"><h2 class="title" style="padding-top:15px">What others are reading:</h2><ul><li><a href="http://www.whileifblog.com/2011/01/30/linux-exporting-files-from-git-similar-to-svn-export/" rel="bookmark" class="crp_title">Linux &#8211; Exporting Files From Git (similar to SVN export)</a></li><li><a href="http://www.whileifblog.com/2010/07/12/php-optimize-your-jpeg-images-with-imagemagick/" rel="bookmark" class="crp_title">PHP &#8211; Optimize your JPEG images with ImageMagick</a></li><li><a href="http://www.whileifblog.com/2012/04/22/linux-scp-copy-all-hidden-dot-files/" rel="bookmark" class="crp_title">Linux &#8211; scp copy all hidden dot files</a></li><li><a href="http://www.whileifblog.com/2012/02/20/php-magic-constants/" rel="bookmark" class="crp_title">PHP &#8211; Magic Constants</a></li></ul></div><p><a href="http://www.whileifblog.com/2012/04/12/linux-set-your-path-variable-using-set-or-export-command/">Linux &#8211; Set your PATH Variable using set or export command</a> is a post from: <a href="http://www.whileifblog.com">WhileIf Blog</a></p>
<div id="seo_alrp_related"><h2>Posts Related to Linux - Set your PATH Variable using set or export command</h2><ul><li><div class="seo_alrp_rl_content"><h3><a href="http://www.whileifblog.com/2011/01/30/linux-exporting-files-from-git-similar-to-svn-export/" rel="bookmark">Linux &#8211; Exporting Files From Git (similar to SVN export)</a></h3><p>I use git for just about all my code now. For some of my work, I need to export portions of or all of a ...</p></div></li><li><div class="seo_alrp_rl_content"><h3><a href="http://www.whileifblog.com/2010/07/12/php-optimize-your-jpeg-images-with-imagemagick/" rel="bookmark">PHP &#8211; Optimize your JPEG images with ImageMagick</a></h3><p>If you need to optimize the images for your existing website, the following code might be useful: Just enter the path to the the directory ...</p></div></li><li><div class="seo_alrp_rl_content"><h3><a href="http://www.whileifblog.com/2012/04/22/linux-scp-copy-all-hidden-dot-files/" rel="bookmark">Linux &#8211; scp copy all hidden dot files</a></h3><p>The scp command copies files between servers (computers) on a network. It uses ssh for data transfer, and uses the same authentication and provides the ...</p></div></li><li><div class="seo_alrp_rl_content"><h3><a href="http://www.whileifblog.com/2012/02/20/php-magic-constants/" rel="bookmark">PHP &#8211; Magic Constants</a></h3><p>PHP provides useful magic constants for fetching the current line number (__LINE__), file path (__FILE__), directory path (__DIR__), function name (__FUNCTION__), class name (__CLASS__), method ...</p></div></li><li><div class="seo_alrp_rl_content"><h3><a href="http://www.whileifblog.com/2012/03/09/linux-how-to-be-faster-at-the-command-line/" rel="bookmark">Linux &#8211; How to Be Faster at the Command Line</a></h3><p>Want to be faster at the Linux command line interface? Since most Linux distributions provide Bash as the default CLI, here are some Bash tricks ...</p></div></li></ul></div>]]></content:encoded>
			<wfw:commentRss>http://www.whileifblog.com/2012/04/12/linux-set-your-path-variable-using-set-or-export-command/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP &#8211; Validating a URL</title>
		<link>http://www.whileifblog.com/2012/04/11/php-validating-a-url/</link>
		<comments>http://www.whileifblog.com/2012/04/11/php-validating-a-url/#comments</comments>
		<pubDate>Wed, 11 Apr 2012 21:15:38 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Tips]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://www.whileifblog.com/?p=878</guid>
		<description><![CDATA[The simplest and most consistent way to validate a URL in PHP is to use filter_var(). Here is an example: The only problem with this approach is that it has a few caveats. The PHP filter_var function has only been &#8230;<p class="read-more"><a href="http://www.whileifblog.com/2012/04/11/php-validating-a-url/">Read more &#187;</a></p><p><a href="http://www.whileifblog.com/2012/04/11/php-validating-a-url/">PHP &#8211; Validating a URL</a> is a post from: <a href="http://www.whileifblog.com">WhileIf Blog</a></p>
]]></description>
			<content:encoded><![CDATA[<p>The simplest and most consistent way to validate a URL in PHP is to use<strong> filter_var().</strong></p>
<p>Here is an example:</p>
<pre class="brush: php; title: ; notranslate">
$url = &quot;http://whileifblog.com&quot;;
if (filter_var($url, FILTER_VALIDATE_URL)) {
      echo &quot;URL is valid&quot;;
} else {
      echo &quot;URL is invalid&quot;;
}
</pre>
<p>The only problem with this approach is that it has a few caveats. The PHP filter_var function has only been available since PHP 5.2. If you are running a PHP version older than 5.2 you are out of luck.</p>
<p>There is also a bug in PHP 5.2.13 and PHP 5.3.2 that will not allow URLs with dashes in them to validate. If you are unsure of your PHP version you can run <strong>php -v</strong> from the command line if you have command line PHP installed, or create a PHP file with the following contents and navigate to it using your web browser:</p>
<pre class="brush: php; title: ; notranslate">
phpinfo();
</pre>
<p><span id="more-878"></span></p>
<p>Because of the limitations of <strong>filter_var()</strong>, you may have to fall back to using a regular expression. Here is an example of one regular expression that allows dashes in the URL:</p>
<pre class="brush: php; title: ; notranslate">
$url = &quot;http://whileifblog.com&quot;;
if (preg_match(&quot;/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&amp;amp;@#\/%?=~_|!:,.;]*[-a-z0-9+&amp;amp;@#\/%=~_|]/i&quot;, $url)) {
    echo &quot;URL is valid&quot;;
} else {
    echo &quot;URL is invalid&quot;;
}
</pre>
<p>There is a lot of debate about whether using a regular expression is a good idea when doing URL validation. If you are uncertain and looking for a guide then this may help:</p>
<p>If you are using a version of PHP that is older than PHP 5.2:<br />
- Use the regular expression approach<br />
If you are using PHP 5.2.13 or PHP 5.3.2 and need URLs with dashes to validate:<br />
- Use the regular expression approach<br />
If you don&#8217;t fall into one of the above categories:<br />
- Use the filter_var() approach<br />
Some have complained that using the PHP <strong>filter_var()</strong> function is too permissive and allows URLs that should not validate. One solution is to use filter_var for your initial validation and then perform additional validation checks if the original filter_var validation passes.</p>
<div id="crp_related"><h2 class="title" style="padding-top:15px">What others are reading:</h2><ul><li><a href="http://www.whileifblog.com/2010/09/04/regex-regular-expression-to-match-a-url/" rel="bookmark" class="crp_title">REGEX &#8211; Regular expression to match a URL</a></li><li><a href="http://www.whileifblog.com/2010/07/25/php-convert-url-to-tinyurl/" rel="bookmark" class="crp_title">PHP &#8211; Convert url to TinyURL</a></li><li><a href="http://www.whileifblog.com/2010/11/22/php-transform-url-to-hyperlinks/" rel="bookmark" class="crp_title">PHP &#8211; Transform URL to hyperlinks</a></li><li><a href="http://www.whileifblog.com/2010/08/30/regex-validate-an-url/" rel="bookmark" class="crp_title">REGEX &#8211; Validate an URL</a></li></ul></div><p><a href="http://www.whileifblog.com/2012/04/11/php-validating-a-url/">PHP &#8211; Validating a URL</a> is a post from: <a href="http://www.whileifblog.com">WhileIf Blog</a></p>
<div id="seo_alrp_related"><h2>Posts Related to PHP - Validating a URL</h2><ul><li><div class="seo_alrp_rl_content"><h3><a href="http://www.whileifblog.com/2010/08/22/php-%e2%80%93-optimization-tricks-iv/" rel="bookmark">PHP – Optimization Tricks IV</a></h3><p>Many scripts tend to reply on regular expression to validate the input specified by user. While validating input is a superb idea, doing so via ...</p></div></li><li><div class="seo_alrp_rl_content"><h3><a href="http://www.whileifblog.com/2010/07/25/php-convert-url-to-tinyurl/" rel="bookmark">PHP &#8211; Convert url to TinyURL</a></h3><p>Sharing long urls by email or on social media sites is never a good idea, this is why most people are using urls shorteners such ...</p></div></li><li><div class="seo_alrp_rl_content"><h3><a href="http://www.whileifblog.com/2010/09/04/regex-regular-expression-to-match-a-url/" rel="bookmark">REGEX &#8211; Regular expression to match a URL</a></h3><p>This one compares the URL where it will first look for http://, https:// or check for it presence at all. Next it looks up the ...</p></div></li><li><div class="seo_alrp_rl_content"><h3><a href="http://www.whileifblog.com/2010/09/03/regex-validate-email-address/" rel="bookmark">REGEX &#8211; Validate Email address</a></h3><p>This is one of the most common, widely used and important regular expression for validating an email address. This matches a string that has all ...</p></div></li><li><div class="seo_alrp_rl_content"><h3><a href="http://www.whileifblog.com/2010/09/08/regex-banking-numbers-validation/" rel="bookmark">REGEX &#8211; Banking numbers validation</a></h3><p>Validate an IBAN The following one will verify that the given IBAN is valid. Validate a BIC code Another one very useful for any banking ...</p></div></li></ul></div>]]></content:encoded>
			<wfw:commentRss>http://www.whileifblog.com/2012/04/11/php-validating-a-url/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
<!-- This Quick Cache file was built for (  www.whileifblog.com/feed/ ) in 1.15512 seconds, on May 20th, 2012 at 9:38 pm UTC. -->
<!-- This Quick Cache file will automatically expire ( and be re-built automatically ) on May 21st, 2012 at 9:38 pm UTC -->
<!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
<!-- Quick Cache Is Fully Functional :-) ... A Quick Cache file was just served for (  www.whileifblog.com/feed/ ) in 0.00062 seconds, on May 20th, 2012 at 10:36 pm UTC. -->
