<?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>stoimen&#039;s web log &#187; micro tutorial</title>
	<atom:link href="http://www.stoimen.com/blog/category/micro-tutorial/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.stoimen.com/blog</link>
	<description>about web development</description>
	<lastBuildDate>Mon, 06 Feb 2012 20:50:58 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>How to Setup Different Error Messages for Each Zend Form Element Validator</title>
		<link>http://www.stoimen.com/blog/2011/11/23/how-to-setup-different-error-messages-for-each-zend-form-element-validator/</link>
		<comments>http://www.stoimen.com/blog/2011/11/23/how-to-setup-different-error-messages-for-each-zend-form-element-validator/#comments</comments>
		<pubDate>Wed, 23 Nov 2011 08:25:07 +0000</pubDate>
		<dc:creator>Stoimen</dc:creator>
				<category><![CDATA[micro tutorial]]></category>
		<category><![CDATA[zend framework]]></category>
		<category><![CDATA[0]]></category>
		<category><![CDATA[Elementary arithmetic]]></category>
		<category><![CDATA[Error message]]></category>
		<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[measurement]]></category>
		<category><![CDATA[Nothing]]></category>
		<category><![CDATA[Numbers]]></category>
		<category><![CDATA[Pi]]></category>

		<guid isPermaLink="false">http://www.stoimen.com/blog/?p=2469</guid>
		<description><![CDATA[Anyone who has worked with that has come across this problem. I&#8217;d like to show different error message on each validator attached to a Zend_Form_Element. Let&#8217;s say we validate an text input field. We want it to contain only digits, &#8230; <a href="http://www.stoimen.com/blog/2011/11/23/how-to-setup-different-error-messages-for-each-zend-form-element-validator/">Continue reading <span class="meta-nav">&#8594;</span></a>
Related posts:<ol>
<li><a href='http://www.stoimen.com/blog/2010/07/07/default-error-handling-in-zend-framework/' rel='bookmark' title='Default Error Handling in Zend Framework'>Default Error Handling in Zend Framework</a></li>
<li><a href='http://www.stoimen.com/blog/2010/06/04/one-form-multiple-db-records/' rel='bookmark' title='One Form &#8211; Multiple DB Records'>One Form &#8211; Multiple DB Records</a></li>
<li><a href='http://www.stoimen.com/blog/2010/04/09/secure-forms-with-zend-framework/' rel='bookmark' title='Secure Forms with Zend Framework'>Secure Forms with Zend Framework</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>Anyone who has worked with that has come across this problem. I&#8217;d like to show different error message on each validator attached to a <a href="http://framework.zend.com/manual/1.11/en/zend.form.standardElements.html" title="Zend Framework: Standard Form Elements" target="_blank">Zend_Form_Element</a>. Let&#8217;s say we validate an text input field. We want it to contain only digits, but also we&#8217;d like to display different messages when the field is empty and when the user has entered something that is different from digits. </p>
<p>It can be done by attaching to the form element two validators: <a href="http://framework.zend.com/manual/en/zend.validate.set.html#zend.validate.set.digits" title="Zend Framework: Standard Validation Classes, Zend_Validate_Digits" target="_blank">Zend_Validate_Digits</a> and <a href="http://framework.zend.com/manual/en/zend.validate.set.html#zend.validate.set.notempty" title="Zend Framework: Standard Validation Classes, Zend_Validate_NotEmpty" target="_blank">Zend_Validate_NotEmpty</a>, but first let&#8217;s see how to change the default &#8220;Value is required and can&#8217;t be empty&#8221; error message of a form field.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$element</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">createElement</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'text'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'phone'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$element</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setLabel</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Please, enter your phone number:'</span><span style="color: #009900;">&#41;</span>
	<span style="color: #339933;">-&gt;</span><span style="color: #004000;">setRequired</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span>
	<span style="color: #339933;">-&gt;</span><span style="color: #004000;">addValidator</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Digits'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addElement</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$element</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Here we validate the field with Zend_Validate_Digits and we have set it to be required. Thus everything containing characters, i.e. &#8220;my123name&#8221; or &#8220;007bond&#8221;, will be false, while &#8220;1234&#8243; will be true.</p>
<p><div id="attachment_2485" class="wp-caption alignnone" style="width: 630px"><a href="http://www.stoimen.com/blog/category/zend-framework-on-stoimen-com/"><img src="http://www.stoimen.com/blog/wp-content/uploads/2011/11/zend-framework-logo.png" alt="Zend Framework" title="zend-framework-logo" width="620" height="169" class="size-full wp-image-2485" /></a><p class="wp-caption-text">To show different error messages you&#039;ve to attach them per validator and not per form element!</p></div><br />
 <span id="more-2469"></span><br />
First of all this field is set to be required with the line ->setRequired(true), so we cannot submit the form if the input is empty and we&#8217;ll receive the default error message &#8220;Value is required and can&#8217;t be empty&#8221;. The question is how to change this default message, because as you know sometimes you&#8217;d like to say something different to your users or you&#8217;d like to display error messages on a language different from English. Here&#8217;s how.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$element</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">createElement</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'text'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'phone'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$element</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setLabel</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Please, enter your phone number:'</span><span style="color: #009900;">&#41;</span>
	<span style="color: #339933;">-&gt;</span><span style="color: #004000;">setRequired</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span>
	<span style="color: #339933;">-&gt;</span><span style="color: #004000;">addValidator</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Digits'</span><span style="color: #009900;">&#41;</span>
	<span style="color: #339933;">-&gt;</span><span style="color: #004000;">addErrorMessage</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Please, type your phone here!'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addElement</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$element</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Now the error message is changed from &#8220;Value is required and can&#8217;t be empty&#8221; to &#8220;Please, type your phone here!&#8221;. </p>
<p>The problem is that when you add more than one validator to a form field you can still show one message regardless of the validator that has failed.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$element</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">createElement</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'text'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'phone'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$element</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setLabel</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Please, enter your phone number:'</span><span style="color: #009900;">&#41;</span>
	<span style="color: #339933;">-&gt;</span><span style="color: #004000;">setRequired</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span>
	<span style="color: #339933;">-&gt;</span><span style="color: #004000;">addValidator</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'NotEmpty'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span>
	<span style="color: #339933;">-&gt;</span><span style="color: #004000;">addValidator</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Digits'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span>
	<span style="color: #339933;">-&gt;</span><span style="color: #004000;">addErrorMessage</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Please, type your phone here!'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addElement</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$element</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>In this case whenever the field is empty or it contains something different from digits, the message shown to the user will be &#8220;Please, type your phone here!&#8221;. The question is can we show different error messages on every validator. This should look something like &#8220;The field cannot be empty!&#8221; when the field is empty and &#8220;Please, enter only digits!&#8221; when the user has entered something into the field, but it doesn&#8217;t contain only digits.</p>
<h2>The Solution</h2>
<p>Actually we have to attach error messages <em><strong>per validator</strong></em>, and not on a form element. Here&#8217;s how it can be done.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$notEmpty</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Validate_NotEmpty<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$notEmpty</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setMessage</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'The field cannot be empty!'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$digits</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Zend_Validate_Digits<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$digits</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setMessage</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Please, enter only digits'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$element</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">createElement</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'text'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'phone'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$element</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">setLabel</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Please, enter your phone:'</span><span style="color: #009900;">&#41;</span>
	<span style="color: #339933;">-&gt;</span><span style="color: #004000;">setRequired</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span>
	<span style="color: #339933;">-&gt;</span><span style="color: #004000;">addValidator</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$notEmpty</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span>
	<span style="color: #339933;">-&gt;</span><span style="color: #004000;">addValidator</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$digits</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$form</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">addElement</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$element</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Note that we set to &#8220;true&#8221; the second parameter of addValidator. This is important because this way we break the validator&#8217;s chain and when the validation fails on NotEmpty the framework stops the validation of that field against the other validators.</p>
<p>Related posts:<ol>
<li><a href='http://www.stoimen.com/blog/2010/07/07/default-error-handling-in-zend-framework/' rel='bookmark' title='Default Error Handling in Zend Framework'>Default Error Handling in Zend Framework</a></li>
<li><a href='http://www.stoimen.com/blog/2010/06/04/one-form-multiple-db-records/' rel='bookmark' title='One Form &#8211; Multiple DB Records'>One Form &#8211; Multiple DB Records</a></li>
<li><a href='http://www.stoimen.com/blog/2010/04/09/secure-forms-with-zend-framework/' rel='bookmark' title='Secure Forms with Zend Framework'>Secure Forms with Zend Framework</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.stoimen.com/blog/2011/11/23/how-to-setup-different-error-messages-for-each-zend-form-element-validator/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>PHP: Don&#8217;t Call the Destructor Explicitly</title>
		<link>http://www.stoimen.com/blog/2011/11/14/php-dont-call-the-destructor-explicitly/</link>
		<comments>http://www.stoimen.com/blog/2011/11/14/php-dont-call-the-destructor-explicitly/#comments</comments>
		<pubDate>Mon, 14 Nov 2011 16:26:23 +0000</pubDate>
		<dc:creator>Stoimen</dc:creator>
				<category><![CDATA[micro tutorial]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[destructor]]></category>
		<category><![CDATA[how-to]]></category>
		<category><![CDATA[Object-oriented programming]]></category>
		<category><![CDATA[oop]]></category>
		<category><![CDATA[php examples]]></category>
		<category><![CDATA[research]]></category>
		<category><![CDATA[Scripting languages]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.stoimen.com/blog/?p=2461</guid>
		<description><![CDATA[&#8220;PHP 5 introduces a destructor concept similar to that of other object-oriented languages, such as C++&#8221;[1] says the documentation for destructors, but let&#8217;s see the following class. class A &#123; public function __construct&#40;&#41; &#123; echo 'building an object'; &#125; &#160; &#8230; <a href="http://www.stoimen.com/blog/2011/11/14/php-dont-call-the-destructor-explicitly/">Continue reading <span class="meta-nav">&#8594;</span></a>
Related posts:<ol>
<li><a href='http://www.stoimen.com/blog/2011/10/20/some-notes-on-the-object-oriented-model-of-php/' rel='bookmark' title='Some Notes on the Object-oriented Model of PHP'>Some Notes on the Object-oriented Model of PHP</a></li>
<li><a href='http://www.stoimen.com/blog/2011/10/27/object-cloning-and-passing-by-reference-in-php/' rel='bookmark' title='Object Cloning and Passing by Reference in PHP'>Object Cloning and Passing by Reference in PHP</a></li>
<li><a href='http://www.stoimen.com/blog/2011/11/04/how-to-check-if-a-date-is-more-or-less-than-a-month-ago-with-php/' rel='bookmark' title='How to Check if a Date is More or Less Than a Month Ago with PHP'>How to Check if a Date is More or Less Than a Month Ago with PHP</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p><em>&#8220;PHP 5 introduces a destructor concept similar to that of other object-oriented languages, such as C++&#8221;</em><a href="http://www.php.net/manual/en/language.oop5.decon.php" title="PHP: Constructors and Destructors" target="_blank">[1]</a> says the documentation for destructors, but let&#8217;s see the following class.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> A
<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'building an object'</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __destruct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'destroying the object'</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$obj</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> A<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Well, as you can not call the constructor explicitly:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$obj</span><span style="color: #339933;">-&gt;</span>__construct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>So we should not call the destructor explicitly:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$obj</span><span style="color: #339933;">-&gt;</span>__destruct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The problem is that I&#8217;ve seen this many times, but it&#8217;s a pity that this won&#8217;t destroy the object and it is still a valid PHP code.<br />
<span id="more-2461"></span><br />
<div id="attachment_2465" class="wp-caption alignnone" style="width: 630px"><a href="http://www.stoimen.com/blog/wp-content/uploads/2011/11/destruct.jpg"><img src="http://www.stoimen.com/blog/wp-content/uploads/2011/11/destruct.jpg" alt="PHP destructors can&#039;t be called explicitly!" title="destruct" width="620" height="427" class="size-full wp-image-2465" /></a><p class="wp-caption-text">PHP destructors cannot be called explicitely!</p></div></p>
<p>Constructors and destructors in <a href="http://www.stoimen.com/blog/category/php/" title="PHP on stoimen.com">PHP</a> are part of the so called magic methods. Here&#8217;s what the <a href="http://php.net/manual/en/language.oop5.magic.php" title="PHP: Magic Methods" target="_blank">doc page</a> says about them.</p>
<blockquote><p>
The function names __construct(), __destruct(), __call(), __callStatic(), __get(), __set(), __isset(), __unset(), __sleep(), __wakeup(), __toString(), __invoke(), __set_state() and __clone() are magical in PHP classes. You cannot have functions with these names in any of your classes unless you want the magic functionality associated with them.</p></blockquote>
<p>To be more precise let&#8217;s take a look of the definition of destructors. </p>
<blockquote><p>PHP 5 introduces a destructor concept similar to that of other object-oriented languages, such as C++. The destructor method will be called as soon as there are no other references to a particular object, or in any order during the shutdown sequence.</p></blockquote>
<h2>What if I Call the Destructor Explicitly?</h2>
<p>Let&#8217;s see some examples!</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> A
<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __destruct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'destroying the object'</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$obj</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> A<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// prints hello world</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'hello world'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// PHP interpreter stops the script execution and prints</span>
<span style="color: #666666; font-style: italic;">// 'destroying the object'</span></pre></div></div>

<p>This is actually a normal behavior. At the end of the script the interpreter frees the memory. Actually every object has a built-in destructor, just like it has built-in constructor. So even we don&#8217;t define it explicitly, the object has its destructor. Usually this destructor is executed at the end of the script, or whenever the object isn&#8217;t needed anymore. This can happen, for instance, at the end of a function body.</p>
<p>Now if we call the destructor explicitly, which as I said I&#8217;ve seen many times, here&#8217;s what happen.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> A
<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __destruct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'destroying the object'</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$obj</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> A<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// this is valid and it prints 'destroying the object'</span>
<span style="color: #666666; font-style: italic;">// BUT IT DOES NOT DESTROY THE OBJECT</span>
<span style="color: #000088;">$obj</span><span style="color: #339933;">-&gt;</span>__destruct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// prints hello world</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'hello world'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// HERE PHP ACTUALLY DESTROYS THE $obj OBJECT</span>
<span style="color: #666666; font-style: italic;">// ... and again prints 'destroying the object'</span></pre></div></div>

<p>As you can see calling the destructor explicitly doesn&#8217;t destroy the object. So the question is &#8230;</p>
<h2>How to Destroy an Object Before the Script Stops?</h2>
<p>Well, to destroy an object you can assign a NULL value to it.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> A
<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __destruct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'destroying the object'</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$obj</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> A<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// prints 'destroying the object'</span>
<span style="color: #000088;">$obj</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// prints 'hello world'</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'hello world'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// the script stop its execution</span></pre></div></div>

<h2>Caution</h2>
<p>Be aware of that if you don&#8217;t clone the object $obj, and simply assign it to another variable, then $obj = null will be pointless. Let&#8217;s see the following example.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> A 
<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> printMsg<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'I still exist'</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __destruct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'destroying the object'</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$obj</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> A<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// $newObj is pointing to $obj</span>
<span style="color: #000088;">$newObj</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$obj</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// this doesn't destroy $newObj</span>
<span style="color: #666666; font-style: italic;">// as it appears both $obj and $newObj point to the same memory</span>
<span style="color: #666666; font-style: italic;">// so PHP doesn't free this memory</span>
<span style="color: #000088;">$obj</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// prints 'i still exist'</span>
<span style="color: #000088;">$newObj</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">printMsg</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// prints 'hello world'</span>
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'hello world'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// now the scripts destroys the &quot;object&quot;, which in this</span>
<span style="color: #666666; font-style: italic;">// case is $newObj and prints 'destroying the object'</span></pre></div></div>

<p>This example shows us that actually by assigning NULL to an object doesn&#8217;t quite destroy it if there are another objects pointing to the same memory.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> A 
<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> printMsg<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'I still exist'</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __destruct<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'destroying the object'</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$b</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> A<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$d</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$c</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$b</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$b</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$c</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">printMsg</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$d</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">printMsg</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// prints 'destroying the object' ONLY ONCE</span></pre></div></div>

<p>In this last example there are two interesting things to note. First <strong>$b = null</strong> doesn&#8217;t call the destructor, and at the end of the script there&#8217;s only one implicit call of the destructor, although there are two objects.</p>
<h2>Conclusion</h2>
<p>The important thing to note is that you shouldn’t call the destructor of an object explicitly! Not because it will throw an fatal error, but simply because it won’t destroy the object.</p>
<p>[1] <a href="http://www.php.net/manual/en/language.oop5.decon.php" title="PHP: Constructors and Destructors" target="_blank">PHP: Constructors and Destructors</a></p>
<p>Related posts:<ol>
<li><a href='http://www.stoimen.com/blog/2011/10/20/some-notes-on-the-object-oriented-model-of-php/' rel='bookmark' title='Some Notes on the Object-oriented Model of PHP'>Some Notes on the Object-oriented Model of PHP</a></li>
<li><a href='http://www.stoimen.com/blog/2011/10/27/object-cloning-and-passing-by-reference-in-php/' rel='bookmark' title='Object Cloning and Passing by Reference in PHP'>Object Cloning and Passing by Reference in PHP</a></li>
<li><a href='http://www.stoimen.com/blog/2011/11/04/how-to-check-if-a-date-is-more-or-less-than-a-month-ago-with-php/' rel='bookmark' title='How to Check if a Date is More or Less Than a Month Ago with PHP'>How to Check if a Date is More or Less Than a Month Ago with PHP</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.stoimen.com/blog/2011/11/14/php-dont-call-the-destructor-explicitly/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>How to Check if a Date is More or Less Than a Month Ago with PHP</title>
		<link>http://www.stoimen.com/blog/2011/11/04/how-to-check-if-a-date-is-more-or-less-than-a-month-ago-with-php/</link>
		<comments>http://www.stoimen.com/blog/2011/11/04/how-to-check-if-a-date-is-more-or-less-than-a-month-ago-with-php/#comments</comments>
		<pubDate>Fri, 04 Nov 2011 14:45:52 +0000</pubDate>
		<dc:creator>Stoimen</dc:creator>
				<category><![CDATA[micro tutorial]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[date and time]]></category>
		<category><![CDATA[dates]]></category>
		<category><![CDATA[datetime]]></category>
		<category><![CDATA[how-to]]></category>
		<category><![CDATA[strtotime]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[unix timestamp]]></category>

		<guid isPermaLink="false">http://www.stoimen.com/blog/?p=2418</guid>
		<description><![CDATA[Let&#8217;s say we have the following problem: we have to check whether a date is more than a month ago or less than a month ago. Many developers go in the wrong direction by calculating the current month and then &#8230; <a href="http://www.stoimen.com/blog/2011/11/04/how-to-check-if-a-date-is-more-or-less-than-a-month-ago-with-php/">Continue reading <span class="meta-nav">&#8594;</span></a>
Related posts:<ol>
<li><a href='http://www.stoimen.com/blog/2009/08/10/javascript-get-locale-month-with-full-name/' rel='bookmark' title='javascript get locale month with full name'>javascript get locale month with full name</a></li>
<li><a href='http://www.stoimen.com/blog/2011/10/19/thing-to-know-about-php-arrays/' rel='bookmark' title='Thing to Know About PHP Arrays'>Thing to Know About PHP Arrays</a></li>
<li><a href='http://www.stoimen.com/blog/2011/08/18/powerful-php-less-known-string-manipulation/' rel='bookmark' title='Powerful PHP: Less Known String Manipulation'>Powerful PHP: Less Known String Manipulation</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>Let&#8217;s say we have the following problem: we have to check whether a date is more than a month ago or less than a month ago. Many developers go in the wrong direction by calculating the current month and then subtracting the number of months from it. Of course, this approach is slow and full of risks of allowing bugs. Since, two months before January, which is the first month of the year, is actually November, which is the eleventh month. Because of these pitfalls, this approach is entirely wrong.<br />
<div id="attachment_2442" class="wp-caption alignnone" style="width: 490px"><a href="http://www.stoimen.com/blog/wp-content/uploads/2011/11/calendar.jpg"><img src="http://www.stoimen.com/blog/wp-content/uploads/2011/11/calendar.jpg" alt="strtotime() is a lot more powerful than you think!" title="calendar" width="480" height="553" class="size-full wp-image-2442" /></a><p class="wp-caption-text">strtotime() is a lot more powerful than you think!</p></div></p>
<p>The question is whether PHP cannot help us with built-in functions to perform these calculations for us. It is obvious, that from version 5.3.0 and later, there is an OOP section, which is great, but unfortunately this version is still not updated everywhere. So, how to accomplish the task?</p>
<h2>The Wrong Approach</h2>
<p>As I said, there are many ways to go in the wrong direction. One of them is to subtract 30 days from current date. This is completely wrong, because not every month has 30 days. Here, some developers will begin to predefine arrays to indicate the number of days in each month, which then will be used in their complicated calculations. Here is an example of this wrong approach.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">echo</span> <span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Y-m-d'</span><span style="color: #339933;">,</span> <span style="color: #990000;">strtotime</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Y-m-d'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #cc66cc;">60</span><span style="color: #339933;">*</span><span style="color: #cc66cc;">60</span><span style="color: #339933;">*</span><span style="color: #cc66cc;">24</span><span style="color: #339933;">*</span><span style="color: #cc66cc;">30</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>This line is full of mistakes. First of all <strong>strtotime(date(&#8216;Y-m-d&#8217;))</strong> can be replaced by the more elegant <strong>strtotime(&#8216;now&#8217;)</strong>, but for this later. Another big mistake is that <strong>60*60*24*30</strong>, which is <strong>number of seconds in 30 days</strong> can be predefined as a constant. Eventually the result is wrong, because not every month has 30 days.</p>
<h2>The Correct Approach</h2>
<p>A small research of the problem and the functions in versions prior of 5.3.0 of PHP is needed. Typical case study of date formatting happen when working with dates from a database. The following code is a classical example.<span id="more-2418"></span></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// 2008 05 23, 2008-05-23 is stored into the DB</span>
<span style="color: #b1b100;">echo</span> <span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Y m d'</span><span style="color: #339933;">,</span> <span style="color: #990000;">strtotime</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'2008-05-23'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// 2008 May 23</span>
<span style="color: #b1b100;">echo</span> <span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Y F d'</span><span style="color: #339933;">,</span> <span style="color: #990000;">strtotime</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'2008-05-23'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The problem, perhaps, is that too often <a href="http://php.net/manual/en/function.strtotime.php" title="PHP: strtotime" target="_blank">strtotime()</a> is used like this, with exactly this type of strings. However much more interesting is that strtotime() can do much more. </p>
<h2>strtotime() Can Do Much More</h2>
<p>Let us first look at the documentation of this function. What parameters it accepts?</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">int <span style="color: #990000;">strtotime</span> <span style="color: #009900;">&#40;</span> string <span style="color: #000088;">$time</span> <span style="color: #009900;">&#91;</span><span style="color: #339933;">,</span> int <span style="color: #000088;">$now</span> <span style="color: #339933;">=</span> <span style="color: #990000;">time</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#41;</span></pre></div></div>

<blockquote><p>The function expects to be given a string containing an English date format and will try to parse that format into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 UTC), relative to the timestamp given in now, or the current time if now is not supplied.</p></blockquote>
<p>In particular we are interested in the first parameter, <em>time</em>.</p>
<blockquote><p><em>time </em>- A date/time string. Valid formats are explained in Date and Time Formats.</p></blockquote>
<p>It is especially important to note what are the <a href="http://www.php.net/manual/en/datetime.formats.php" title="PHP: Supported Date and Time Formats" target="_blank">valid Date and Time Formats</a>.</p>
<p>Here are the supported formats, but most interesting are those that are <a href="http://www.php.net/manual/en/datetime.formats.relative.php" title="PHP: Relative Formats" target="_blank">Relative</a>.</p>
<p>Exactly these formats are very convenient in our case, because they give us the ability to work with human readable strings, and here are some examples from the documentation of strtotime().</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">echo</span> <span style="color: #990000;">strtotime</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;now&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #990000;">strtotime</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;10 September 2000&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #990000;">strtotime</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;+1 day&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #990000;">strtotime</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;+1 week&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #990000;">strtotime</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;+1 week 2 days 4 hours 2 seconds&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #990000;">strtotime</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;next Thursday&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #990000;">strtotime</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;last Monday&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Thus, a valid string would be &#8220;1 month ago&#8221;.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// if current date is 2011-11-04, this will return 2011-10-04</span>
<span style="color: #b1b100;">echo</span> <span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Y-m-d'</span><span style="color: #339933;">,</span> <span style="color: #990000;">strtotime</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'1 month ago'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span></pre></div></div>

<p>Or &#8220;-1 month&#8221;:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// the same as the example above</span>
<span style="color: #b1b100;">echo</span> <span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Y-m-d'</span><span style="color: #339933;">,</span> <span style="color: #990000;">strtotime</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'-1 month'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>It&#8217;s interesting that &#8220;+1 -1 month&#8221; is also a valid string.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// 2011-10-04, if today's 2011-11-04</span>
<span style="color: #b1b100;">echo</span> <span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Y-m-d'</span><span style="color: #339933;">,</span> <span style="color: #990000;">strtotime</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'+1 -1 month'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>In fact strtotime() can do a lot more than most of the developers have ever imagined. Maybe its frequent use with string formatted dates (2010-01-13) makes it a bit unknown. Here are some interesting use cases.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// 1970-01-01, Calculations in braces are bad!</span>
<span style="color: #b1b100;">echo</span> <span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Y-m-d'</span><span style="color: #339933;">,</span> <span style="color: #990000;">strtotime</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'(60*60) minute'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// 2 months into the future</span>
<span style="color: #b1b100;">echo</span> <span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Y-m-d'</span><span style="color: #339933;">,</span> <span style="color: #990000;">strtotime</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'-2 months ago'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>For instance, do you know how to get the date of the day before yesterday? Yes 2 days before today, but here&#8217;s yet another solution.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// 1 day before yesterday</span>
<span style="color: #b1b100;">echo</span> <span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Y-m-d'</span><span style="color: #339933;">,</span> <span style="color: #990000;">strtotime</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'yesterday -1 day'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Another example is the fully human readable:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// get the first monday of the current month</span>
<span style="color: #b1b100;">echo</span> <span style="color: #990000;">date</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Y-m-d'</span><span style="color: #339933;">,</span> <span style="color: #990000;">strtotime</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'first monday this month'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h2>The Solution of the Task</h2>
<p>Finally, what is the solution of the original task? Well, just have to check whether a date is more or less than a month ago.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// a random date</span>
<span style="color: #000088;">$my_date</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'2011-09-23'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// true if my_date is more than a month ago</span>
<span style="color: #009900;">&#40;</span><span style="color: #990000;">strtotime</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$my_date</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&lt;</span> <span style="color: #990000;">strtotime</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'1 month ago'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span></pre></div></div>

<p>Related posts:<ol>
<li><a href='http://www.stoimen.com/blog/2009/08/10/javascript-get-locale-month-with-full-name/' rel='bookmark' title='javascript get locale month with full name'>javascript get locale month with full name</a></li>
<li><a href='http://www.stoimen.com/blog/2011/10/19/thing-to-know-about-php-arrays/' rel='bookmark' title='Thing to Know About PHP Arrays'>Thing to Know About PHP Arrays</a></li>
<li><a href='http://www.stoimen.com/blog/2011/08/18/powerful-php-less-known-string-manipulation/' rel='bookmark' title='Powerful PHP: Less Known String Manipulation'>Powerful PHP: Less Known String Manipulation</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.stoimen.com/blog/2011/11/04/how-to-check-if-a-date-is-more-or-less-than-a-month-ago-with-php/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Object Cloning and Passing by Reference in PHP</title>
		<link>http://www.stoimen.com/blog/2011/10/27/object-cloning-and-passing-by-reference-in-php/</link>
		<comments>http://www.stoimen.com/blog/2011/10/27/object-cloning-and-passing-by-reference-in-php/#comments</comments>
		<pubDate>Thu, 27 Oct 2011 14:25:17 +0000</pubDate>
		<dc:creator>Stoimen</dc:creator>
				<category><![CDATA[micro tutorial]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[Clone]]></category>
		<category><![CDATA[Cloning]]></category>
		<category><![CDATA[Comparison of programming languages]]></category>
		<category><![CDATA[Computer programming]]></category>
		<category><![CDATA[Computing]]></category>
		<category><![CDATA[Curly bracket programming languages]]></category>
		<category><![CDATA[Java programming language]]></category>
		<category><![CDATA[PHP programming language]]></category>
		<category><![CDATA[Procedural programming languages]]></category>
		<category><![CDATA[Scripting languages]]></category>
		<category><![CDATA[Software engineering]]></category>

		<guid isPermaLink="false">http://www.stoimen.com/blog/?p=2408</guid>
		<description><![CDATA[In PHP everything&#8217;s a reference! I&#8217;ve heard it so many times in my practice. No, these words are too strong! Let&#8217;s see some examples. Passing Parameters by Reference Clearly when we pass parameters to a function it&#8217;s not by reference. &#8230; <a href="http://www.stoimen.com/blog/2011/10/27/object-cloning-and-passing-by-reference-in-php/">Continue reading <span class="meta-nav">&#8594;</span></a>
Related posts:<ol>
<li><a href='http://www.stoimen.com/blog/2011/10/20/some-notes-on-the-object-oriented-model-of-php/' rel='bookmark' title='Some Notes on the Object-oriented Model of PHP'>Some Notes on the Object-oriented Model of PHP</a></li>
<li><a href='http://www.stoimen.com/blog/2010/06/11/friday-algorithms-quicksort-difference-between-php-and-javascript/' rel='bookmark' title='Friday Algorithms: Quicksort &#8211; Difference Between PHP and JavaScript'>Friday Algorithms: Quicksort &#8211; Difference Between PHP and JavaScript</a></li>
<li><a href='http://www.stoimen.com/blog/2010/05/17/php-the-array-element-doesnt-exist-suppress-the-warnings/' rel='bookmark' title='PHP: The Array Element Doesn&#8217;t Exist &#8211; Suppress the Warnings'>PHP: The Array Element Doesn&#8217;t Exist &#8211; Suppress the Warnings</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>In <a href="http://www.stoimen.com/blog/category/php/" title="PHP at stoimen.com">PHP </a>everything&#8217;s a reference! I&#8217;ve heard it so many times in my practice. No, these words are too strong! Let&#8217;s see some examples.<br />
<div id="attachment_2432" class="wp-caption alignnone" style="width: 490px"><a href="http://www.stoimen.com/blog/wp-content/uploads/2011/10/ampersand.jpg"><img src="http://www.stoimen.com/blog/wp-content/uploads/2011/10/ampersand.jpg" alt="Passing by reference in PHP can be tricky!" title="ampersand" width="480" height="480" class="size-full wp-image-2432" /></a><p class="wp-caption-text">Some developers think that everything&#039;s passed by reference in PHP.</p></div></p>
<h2>Passing Parameters by Reference</h2>
<p>Clearly when we pass parameters to a function it&#8217;s not by reference. How to check this? Well, like this.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> f<span style="color: #009900;">&#40;</span><span style="color: #000088;">$param</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$param</span><span style="color: #339933;">++;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$a</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">5</span><span style="color: #339933;">;</span>
f<span style="color: #009900;">&#40;</span><span style="color: #000088;">$a</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$a</span><span style="color: #339933;">;</span></pre></div></div>

<p>Now the value of $a equals 5. If it were passed by reference, it would be 6. With a little change of the code we can get it.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> f<span style="color: #009900;">&#40;</span><span style="color: #339933;">&amp;</span><span style="color: #000088;">$param</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$param</span><span style="color: #339933;">++;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$a</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">5</span><span style="color: #339933;">;</span>
f<span style="color: #009900;">&#40;</span><span style="color: #000088;">$a</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$a</span><span style="color: #339933;">;</span></pre></div></div>

<p>Now the variable&#8217;s value is 6. </p>
<p>So far, so good. Now what about copying objects?<br />
<span id="more-2408"></span></p>
<h2>Objects: A Copy or a Cloning?</h2>
<p>We can check whether by assigning an object to a variable a reference or a copy of the object is passed.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> C
<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$myvar</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$a</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> C<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$b</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$a</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$b</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">myvar</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">20</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// 20, not 10</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$a</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">myvar</span><span style="color: #339933;">;</span></pre></div></div>

<p>The last line outputs 20! This makes it clear. By assigning an object to a variable PHP pass its reference. To make a copy there&#8217;s another approach. We need to change $b = $a, to $b = clone $a;</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> C
<span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$myvar</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000088;">$a</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> C<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$b</span> <span style="color: #339933;">=</span> clone <span style="color: #000088;">$a</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$b</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">myvar</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">20</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// 10</span>
<span style="color: #b1b100;">echo</span> <span style="color: #000088;">$a</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">myvar</span><span style="color: #339933;">;</span></pre></div></div>

<h2>Arrays by Reference</h2>
<p>What about arrays? What if I assign an array to a variable?</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$a</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">20</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$b</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$a</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$b</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">30</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #990000;">var_dump</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$a</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>What do you think is the value of $a[0]? Well, the answer is: 20! So $b is a copy of the array &#8220;a&#8221;. Instead you should assign explicitly its reference to make &#8220;b&#8221; point to &#8220;a&#8221;.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$a</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">20</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000088;">$b</span> <span style="color: #339933;">=</span> <span style="color: #339933;">&amp;</span><span style="color: #000088;">$a</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$b</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">30</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #990000;">var_dump</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$a</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Now $a[0] equals 30!</p>
<p>I think this could be useful!</p>
<p>Related posts:<ol>
<li><a href='http://www.stoimen.com/blog/2011/10/20/some-notes-on-the-object-oriented-model-of-php/' rel='bookmark' title='Some Notes on the Object-oriented Model of PHP'>Some Notes on the Object-oriented Model of PHP</a></li>
<li><a href='http://www.stoimen.com/blog/2010/06/11/friday-algorithms-quicksort-difference-between-php-and-javascript/' rel='bookmark' title='Friday Algorithms: Quicksort &#8211; Difference Between PHP and JavaScript'>Friday Algorithms: Quicksort &#8211; Difference Between PHP and JavaScript</a></li>
<li><a href='http://www.stoimen.com/blog/2010/05/17/php-the-array-element-doesnt-exist-suppress-the-warnings/' rel='bookmark' title='PHP: The Array Element Doesn&#8217;t Exist &#8211; Suppress the Warnings'>PHP: The Array Element Doesn&#8217;t Exist &#8211; Suppress the Warnings</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.stoimen.com/blog/2011/10/27/object-cloning-and-passing-by-reference-in-php/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Thing to Know About PHP Arrays</title>
		<link>http://www.stoimen.com/blog/2011/10/19/thing-to-know-about-php-arrays/</link>
		<comments>http://www.stoimen.com/blog/2011/10/19/thing-to-know-about-php-arrays/#comments</comments>
		<pubDate>Wed, 19 Oct 2011 15:18:47 +0000</pubDate>
		<dc:creator>Stoimen</dc:creator>
				<category><![CDATA[micro tutorial]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Arrays]]></category>
		<category><![CDATA[C programming language]]></category>
		<category><![CDATA[Comparison of programming languages]]></category>
		<category><![CDATA[Computer programming]]></category>
		<category><![CDATA[Computing]]></category>
		<category><![CDATA[Data structures]]></category>
		<category><![CDATA[Data types]]></category>
		<category><![CDATA[Foreach]]></category>
		<category><![CDATA[how-to]]></category>
		<category><![CDATA[interpreter]]></category>
		<category><![CDATA[PHP arrays]]></category>
		<category><![CDATA[PHP micro tutorial]]></category>
		<category><![CDATA[php tutorial]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[web development]]></category>
		<category><![CDATA[webdev]]></category>

		<guid isPermaLink="false">http://www.stoimen.com/blog/?p=2390</guid>
		<description><![CDATA[Consider the following case. We have an array with identical keys. $arr = array&#40;1 =&#62; 10, 1 =&#62; 11&#41;; What happens when the interpreter reaches this line of code? This is not a syntax error and it is completely valid. &#8230; <a href="http://www.stoimen.com/blog/2011/10/19/thing-to-know-about-php-arrays/">Continue reading <span class="meta-nav">&#8594;</span></a>
Related posts:<ol>
<li><a href='http://www.stoimen.com/blog/2010/08/31/php-what-is-more-powerful-than-list-perhaps-extract/' rel='bookmark' title='PHP: What is More Powerful Than list() &#8211; Perhaps extract()'>PHP: What is More Powerful Than list() &#8211; Perhaps extract()</a></li>
<li><a href='http://www.stoimen.com/blog/2010/05/19/php-associative-arrays-coding-style/' rel='bookmark' title='PHP Associative Arrays Coding Style'>PHP Associative Arrays Coding Style</a></li>
<li><a href='http://www.stoimen.com/blog/2010/06/11/friday-algorithms-quicksort-difference-between-php-and-javascript/' rel='bookmark' title='Friday Algorithms: Quicksort &#8211; Difference Between PHP and JavaScript'>Friday Algorithms: Quicksort &#8211; Difference Between PHP and JavaScript</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>Consider the following case. We have an array with identical keys.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$arr</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">11</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>What happens when the interpreter reaches this line of code? This is not a syntax error and it is completely valid. Very similar, but more interesting case is when we have an array of identical keys, where those identical keys are represented once as an integer and then as a string.<br />
<div id="attachment_2404" class="wp-caption aligncenter" style="width: 650px"><a href="http://www.stoimen.com/blog/wp-content/uploads/2011/10/php.code_.jpg"><img src="http://www.stoimen.com/blog/wp-content/uploads/2011/10/php.code_.jpg" alt="Keys in PHP arrays are not type sensitive, so pay attention when using them!" title="PHP Code" width="640" height="480" class="size-full wp-image-2404" /></a><p class="wp-caption-text">Keys in PHP arrays are not type sensitive, so pay attention when using them!</p></div></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$arr</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;1&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">11</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Now several questions arise. First of all, how many elements have this array? Two or one. This can be easily verified by checking what count() will return.<span id="more-2390"></span></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">echo</span> <span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$arr</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The correct answer is 1. This simply means, that there&#8217;s no difference between string keys and integer keys. What would happen if we had a &#8220;normal&#8221; array with different keys?</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$arr</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;2&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">11</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$arr</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>As expected this returns 2. </p>
<p>Next thing to check is what&#8217;s in the array after this initialization line.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$arr</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;1&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">11</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Is there something in the first element $arr[0], or there&#8217;s something in the second element $arr[1]? What is the value of the single value?<br />
As it appears the second element replaces the first one. We&#8217;ve seen that the array has only one value, but where&#8217;s that value? The only way to check this is to dump both elements:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">var_dump</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$arr</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Here we can see that $arr[1] contains &#8220;11&#8243; and it is the only value, and $arr[0] is not set.</p>
<p>Related posts:<ol>
<li><a href='http://www.stoimen.com/blog/2010/08/31/php-what-is-more-powerful-than-list-perhaps-extract/' rel='bookmark' title='PHP: What is More Powerful Than list() &#8211; Perhaps extract()'>PHP: What is More Powerful Than list() &#8211; Perhaps extract()</a></li>
<li><a href='http://www.stoimen.com/blog/2010/05/19/php-associative-arrays-coding-style/' rel='bookmark' title='PHP Associative Arrays Coding Style'>PHP Associative Arrays Coding Style</a></li>
<li><a href='http://www.stoimen.com/blog/2010/06/11/friday-algorithms-quicksort-difference-between-php-and-javascript/' rel='bookmark' title='Friday Algorithms: Quicksort &#8211; Difference Between PHP and JavaScript'>Friday Algorithms: Quicksort &#8211; Difference Between PHP and JavaScript</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.stoimen.com/blog/2011/10/19/thing-to-know-about-php-arrays/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Powerful PHP: Less Known String Manipulation</title>
		<link>http://www.stoimen.com/blog/2011/08/18/powerful-php-less-known-string-manipulation/</link>
		<comments>http://www.stoimen.com/blog/2011/08/18/powerful-php-less-known-string-manipulation/#comments</comments>
		<pubDate>Thu, 18 Aug 2011 14:01:51 +0000</pubDate>
		<dc:creator>Stoimen</dc:creator>
				<category><![CDATA[micro tutorial]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[Computer programming]]></category>
		<category><![CDATA[Computing]]></category>
		<category><![CDATA[Data types]]></category>
		<category><![CDATA[Hello world program]]></category>
		<category><![CDATA[Mathematics]]></category>
		<category><![CDATA[Software engineering]]></category>
		<category><![CDATA[String]]></category>

		<guid isPermaLink="false">http://www.stoimen.com/blog/?p=2343</guid>
		<description><![CDATA[Yet another thing that&#8217;s great in PHP is the power you have when doing some string manipulation/operation. Here&#8217;s something that is really useful, but I think it remains a bit unknown. Let&#8217;s imagine you need to take the first (or &#8230; <a href="http://www.stoimen.com/blog/2011/08/18/powerful-php-less-known-string-manipulation/">Continue reading <span class="meta-nav">&#8594;</span></a>
Related posts:<ol>
<li><a href='http://www.stoimen.com/blog/2010/08/25/flexible-javascript-replace-in-a-string/' rel='bookmark' title='Flexible JavaScript &#8211; Replace in a String'>Flexible JavaScript &#8211; Replace in a String</a></li>
<li><a href='http://www.stoimen.com/blog/2010/09/17/5-php-string-functions-you-need-to-know/' rel='bookmark' title='5 PHP String Functions You Need to Know'>5 PHP String Functions You Need to Know</a></li>
<li><a href='http://www.stoimen.com/blog/2010/08/27/php-what-is-more-powerful-than-list/' rel='bookmark' title='PHP: What is More Powerful Than list()'>PHP: What is More Powerful Than list()</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>Yet another thing that&#8217;s great in PHP is the power you have when doing some string manipulation/operation. Here&#8217;s something that is really useful, but I think it remains a bit unknown. Let&#8217;s imagine you need to take the first (or whatever) character of a string. Most developers go to the obvious:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$str</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'hello world'</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$str</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// outputs &quot;h&quot;</span></pre></div></div>

<p>But here&#8217;s something better and cleaner.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">echo</span> <span style="color: #000088;">$str</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// outputs &quot;h&quot;</span></pre></div></div>

<p>This code chunk return the first character of $str, but it can be used with the same success for any other character of the string. In my opinion this is more cleaner and its really syntactically self documented.</p>
<p>This approach can be useful when trying to check whether the first symbol for instance is &#8220;?&#8221; or &#8220;/&#8221;.</p>
<p>Related posts:<ol>
<li><a href='http://www.stoimen.com/blog/2010/08/25/flexible-javascript-replace-in-a-string/' rel='bookmark' title='Flexible JavaScript &#8211; Replace in a String'>Flexible JavaScript &#8211; Replace in a String</a></li>
<li><a href='http://www.stoimen.com/blog/2010/09/17/5-php-string-functions-you-need-to-know/' rel='bookmark' title='5 PHP String Functions You Need to Know'>5 PHP String Functions You Need to Know</a></li>
<li><a href='http://www.stoimen.com/blog/2010/08/27/php-what-is-more-powerful-than-list/' rel='bookmark' title='PHP: What is More Powerful Than list()'>PHP: What is More Powerful Than list()</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.stoimen.com/blog/2011/08/18/powerful-php-less-known-string-manipulation/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>PHP: Fetch $_GET as String with http_build_query()</title>
		<link>http://www.stoimen.com/blog/2011/08/17/php-fetch-get-as-string-with-http_build_query/</link>
		<comments>http://www.stoimen.com/blog/2011/08/17/php-fetch-get-as-string-with-http_build_query/#comments</comments>
		<pubDate>Wed, 17 Aug 2011 07:42:24 +0000</pubDate>
		<dc:creator>Stoimen</dc:creator>
				<category><![CDATA[micro tutorial]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Computing]]></category>
		<category><![CDATA[D]]></category>
		<category><![CDATA[elegant solution]]></category>
		<category><![CDATA[Foreach]]></category>
		<category><![CDATA[http]]></category>
		<category><![CDATA[http_build_query]]></category>
		<category><![CDATA[php reference]]></category>
		<category><![CDATA[Query string]]></category>
		<category><![CDATA[Scripting languages]]></category>
		<category><![CDATA[String]]></category>
		<category><![CDATA[URL]]></category>
		<category><![CDATA[World Wide Web]]></category>

		<guid isPermaLink="false">http://www.stoimen.com/blog/?p=2358</guid>
		<description><![CDATA[PHP is really full of functions for everything! Most of the time when you try to do something with strings, there&#8217;s a function that can do it better and faster. The Route from $_GET to String The global arrays in &#8230; <a href="http://www.stoimen.com/blog/2011/08/17/php-fetch-get-as-string-with-http_build_query/">Continue reading <span class="meta-nav">&#8594;</span></a>
Related posts:<ol>
<li><a href='http://www.stoimen.com/blog/2011/08/18/powerful-php-less-known-string-manipulation/' rel='bookmark' title='Powerful PHP: Less Known String Manipulation'>Powerful PHP: Less Known String Manipulation</a></li>
<li><a href='http://www.stoimen.com/blog/2010/06/16/zend-examples-get-parameters-default-value/' rel='bookmark' title='Zend Examples: GET Parameters Default Value'>Zend Examples: GET Parameters Default Value</a></li>
<li><a href='http://www.stoimen.com/blog/2010/09/17/5-php-string-functions-you-need-to-know/' rel='bookmark' title='5 PHP String Functions You Need to Know'>5 PHP String Functions You Need to Know</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p><strong>PHP</strong> is really full of functions for everything! Most of the time when you try to do something with strings, there&#8217;s a function that can do it better and faster. </p>
<h2>
The Route from $_GET to String<br />
</h2>
<p>The global arrays in PHP contain request parameters. Either <a href="http://php.net/manual/en/reserved.variables.get.php" title="PHP: $_GET - Manual" target="_blank">GET</a> or <a href="http://www.php.net/manual/en/reserved.variables.post.php" title="PHP: $_POST - Manual" target="_blank">POST</a>. As you know if the page address is something like:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;">http<span style="color: #339933;">:</span><span style="color: #666666; font-style: italic;">//www.example.com/index.php?a=b&amp;key=value</span></pre></div></div>

<p>This means that you pass to the index.php file two parameters &#8211; &#8220;a&#8221; and &#8220;key&#8221; with their values: &#8220;b&#8221; and &#8220;value&#8221;. Now in this case you can dump the $_GET <strong>global array</strong> somewhere in index.php and you&#8217;ll receive something like this.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
	<span style="color: #0000ff;">&quot;a&quot;</span>   <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;b&quot;</span><span style="color: #339933;">,</span>
	<span style="color: #0000ff;">&quot;key&quot;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">&quot;value&quot;</span><span style="color: #339933;">,</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>This is however pseudocode, but in fact $_GET will be very similar to this sample array. <span id="more-2358"></span></p>
<h2>
$_GET to String<br />
</h2>
<p>Very often when a developer need to process the $_GET array to a string, which means generating again the query string from $_GET, he often comes to some operation like this one.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$queryString</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$_GET</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$key</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$queryString</span> <span style="color: #339933;">.=</span> <span style="color: #000088;">$key</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'='</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$value</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&amp;'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>However this will result in something quite ugly like <b>a=b&#038;key=value&#038;</b> which comes with a trailing &#038; at the end of the string.</p>
<p>There is however another approach &#8211; using an array.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$queryString</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$_GET</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$key</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$queryString</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$key</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'='</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$value</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000088;">$queryString</span> <span style="color: #339933;">=</span> <span style="color: #990000;">implode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'&amp;'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$queryString</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>But that invokes one function more and this is still not the most elegant solution. As I said at the beginning PHP is full of useful functions and here comes the <a href="http://php.net/manual/en/function.http-build-query.php" title="PHP: http_build_query - Manual" target="_blank">http_build_query</a>.</p>
<h2>
http_build_query<br />
</h2>
<p>This is exactly what you need. As it name describe you can build the query string even by using a different from &#038; separator.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$queryString</span> <span style="color: #339933;">=</span> <span style="color: #990000;">http_build_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$_GET</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">''</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'|'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Thus $queryString will contain <strong>a=b|key=value</strong> and at least the code will look pritier.</p>
<p>Related posts:<ol>
<li><a href='http://www.stoimen.com/blog/2011/08/18/powerful-php-less-known-string-manipulation/' rel='bookmark' title='Powerful PHP: Less Known String Manipulation'>Powerful PHP: Less Known String Manipulation</a></li>
<li><a href='http://www.stoimen.com/blog/2010/06/16/zend-examples-get-parameters-default-value/' rel='bookmark' title='Zend Examples: GET Parameters Default Value'>Zend Examples: GET Parameters Default Value</a></li>
<li><a href='http://www.stoimen.com/blog/2010/09/17/5-php-string-functions-you-need-to-know/' rel='bookmark' title='5 PHP String Functions You Need to Know'>5 PHP String Functions You Need to Know</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.stoimen.com/blog/2011/08/17/php-fetch-get-as-string-with-http_build_query/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>PHP Strings: How to Get the Extension of a File</title>
		<link>http://www.stoimen.com/blog/2011/07/29/php-strings-how-to-get-the-extension-of-a-file/</link>
		<comments>http://www.stoimen.com/blog/2011/07/29/php-strings-how-to-get-the-extension-of-a-file/#comments</comments>
		<pubDate>Fri, 29 Jul 2011 06:25:32 +0000</pubDate>
		<dc:creator>Stoimen</dc:creator>
				<category><![CDATA[micro tutorial]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[app]]></category>
		<category><![CDATA[Computer file formats]]></category>
		<category><![CDATA[Computing]]></category>
		<category><![CDATA[elegant solution]]></category>
		<category><![CDATA[EXE]]></category>
		<category><![CDATA[Filename]]></category>
		<category><![CDATA[gif]]></category>
		<category><![CDATA[jpeg]]></category>
		<category><![CDATA[path]]></category>

		<guid isPermaLink="false">http://www.stoimen.com/blog/?p=2335</guid>
		<description><![CDATA[EXE or GIF or DLL or &#8230; Most of the code chunks I&#8217;ve seen about getting a file extension from a string are based on some sort of string manipulation. $filename = '/my/path/image.jpeg'; echo substr&#40;$filename, strrpos&#40;$filename, '.'&#41; + 1&#41;; Howerver &#8230; <a href="http://www.stoimen.com/blog/2011/07/29/php-strings-how-to-get-the-extension-of-a-file/">Continue reading <span class="meta-nav">&#8594;</span></a>
Related posts:<ol>
<li><a href='http://www.stoimen.com/blog/2011/08/18/powerful-php-less-known-string-manipulation/' rel='bookmark' title='Powerful PHP: Less Known String Manipulation'>Powerful PHP: Less Known String Manipulation</a></li>
<li><a href='http://www.stoimen.com/blog/2010/05/25/download-files-with-zend-framework/' rel='bookmark' title='Download Files with Zend Framework'>Download Files with Zend Framework</a></li>
<li><a href='http://www.stoimen.com/blog/2011/04/07/use-fopen-to-check-file-availability/' rel='bookmark' title='Use fopen() to Check File Availability?'>Use fopen() to Check File Availability?</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<h2>EXE or GIF or DLL or &#8230;</h2>
<p>Most of the code chunks I&#8217;ve seen about getting a file extension from a string are based on some sort of string manipulation.<br />
<div id="attachment_2351" class="wp-caption aligncenter" style="width: 245px"><a href="http://www.stoimen.com/blog/wp-content/uploads/2011/07/filename_extension.jpg"><img class="size-full wp-image-2351" title="filename_extension" src="http://www.stoimen.com/blog/wp-content/uploads/2011/07/filename_extension.jpg" alt="Get the Filename Extension with PHP" width="235" height="235" /></a><p class="wp-caption-text">If you want to get the filename extension with PHP is better to use pathinfo() than string manipulations</p></div></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$filename</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'/my/path/image.jpeg'</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$filename</span><span style="color: #339933;">,</span> <span style="color: #990000;">strrpos</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$filename</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'.'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Howerver there is a more elegant solution.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$filename</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'/my/path/image.jpeg'</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #990000;">strtolower</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">pathinfo</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$filename</span><span style="color: #339933;">,</span> PATHINFO_EXTENSION<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Thus you rely on PHP built in functions and it&#8217;s harder to overlook the exact string manipulation approach.</p>
<p>Related posts:<ol>
<li><a href='http://www.stoimen.com/blog/2011/08/18/powerful-php-less-known-string-manipulation/' rel='bookmark' title='Powerful PHP: Less Known String Manipulation'>Powerful PHP: Less Known String Manipulation</a></li>
<li><a href='http://www.stoimen.com/blog/2010/05/25/download-files-with-zend-framework/' rel='bookmark' title='Download Files with Zend Framework'>Download Files with Zend Framework</a></li>
<li><a href='http://www.stoimen.com/blog/2011/04/07/use-fopen-to-check-file-availability/' rel='bookmark' title='Use fopen() to Check File Availability?'>Use fopen() to Check File Availability?</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.stoimen.com/blog/2011/07/29/php-strings-how-to-get-the-extension-of-a-file/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>OOP JavaScript: Accessing Public Methods in Private Methods</title>
		<link>http://www.stoimen.com/blog/2011/07/28/oop-javascript-accessing-public-methods-in-private-methods/</link>
		<comments>http://www.stoimen.com/blog/2011/07/28/oop-javascript-accessing-public-methods-in-private-methods/#comments</comments>
		<pubDate>Thu, 28 Jul 2011 12:05:42 +0000</pubDate>
		<dc:creator>Stoimen</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[micro tutorial]]></category>
		<category><![CDATA[web development]]></category>
		<category><![CDATA[Accessing Private]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[Class]]></category>
		<category><![CDATA[Computer programming]]></category>
		<category><![CDATA[Computing]]></category>
		<category><![CDATA[Curly bracket programming languages]]></category>
		<category><![CDATA[JavaScript programming language]]></category>
		<category><![CDATA[Object-oriented programming]]></category>
		<category><![CDATA[oop]]></category>
		<category><![CDATA[private]]></category>
		<category><![CDATA[public]]></category>
		<category><![CDATA[Scripting languages]]></category>
		<category><![CDATA[Software engineering]]></category>
		<category><![CDATA[Subroutine]]></category>

		<guid isPermaLink="false">http://www.stoimen.com/blog/?p=2339</guid>
		<description><![CDATA[As you know in JavaScript when you define a variable with the special word &#8220;var&#8221; the scope of this variable is within the function. So when you simply wite &#8220;var a = 5&#8243; the variable named &#8220;a&#8221; has a global &#8230; <a href="http://www.stoimen.com/blog/2011/07/28/oop-javascript-accessing-public-methods-in-private-methods/">Continue reading <span class="meta-nav">&#8594;</span></a>
Related posts:<ol>
<li><a href='http://www.stoimen.com/blog/2011/05/30/object-oriented-javascript-inheritance/' rel='bookmark' title='Object Oriented JavaScript: Inheritance'>Object Oriented JavaScript: Inheritance</a></li>
<li><a href='http://www.stoimen.com/blog/2011/10/20/some-notes-on-the-object-oriented-model-of-php/' rel='bookmark' title='Some Notes on the Object-oriented Model of PHP'>Some Notes on the Object-oriented Model of PHP</a></li>
<li><a href='http://www.stoimen.com/blog/2010/08/11/quick-look-at-javascript-objects/' rel='bookmark' title='Quick Look at JavaScript Objects'>Quick Look at JavaScript Objects</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>As you know in JavaScript when you define a variable with the special word &#8220;var&#8221; the scope of this variable is within the function. So when you simply wite &#8220;var a = 5&#8243; the variable named &#8220;a&#8221; has a global scope and can be accessed in any function in the global scope.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> a <span style="color: #339933;">=</span> <span style="color: #CC0000;">5</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> f<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000066; font-weight: bold;">return</span> a<span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span> <span style="color: #006600; font-style: italic;">// returns 5</span></pre></div></div>

<p>Thus f will return the value of &#8220;a&#8221; which equals to 5. You can also change the value of the global variable in the function body.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> a <span style="color: #339933;">=</span> <span style="color: #CC0000;">5</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">function</span> f<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> a <span style="color: #339933;">=</span> <span style="color: #CC0000;">10</span><span style="color: #339933;">;</span> <span style="color: #000066; font-weight: bold;">return</span> a<span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>a<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// equals to 10</span></pre></div></div>

<p>Now after we call the function f the value of &#8220;a&#8221; will equal to 10. This is because we reference the global variable &#8220;a&#8221; into the function body without using the keyword &#8220;var&#8221;. This means that if you put the &#8220;var&#8221; keyword the variable &#8220;a&#8221; inside the function body is no longer the same variable as the variable defined outside the body. It becames &#8220;local&#8221; and it&#8217;s visible only inside the function.<br />
<span id="more-2339"></span></p>
<h2>Objects &#038; JavaScript</h2>
<p>Actually in JavaScript functions can be also objects. Any variable defined with the &#8220;var&#8221; keyword becomes private, because it&#8217;s only visible inside the function body, while by using the &#8220;this&#8221; keyword we can define global variables visible outside the function body. Let&#8217;s see na example.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> f <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> 
<span style="color: #009900;">&#123;</span>
	<span style="color: #003366; font-weight: bold;">var</span> a <span style="color: #339933;">=</span> <span style="color: #CC0000;">10</span><span style="color: #339933;">;</span>
	<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">b</span> <span style="color: #339933;">=</span> <span style="color: #CC0000;">5</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> myfunc <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> f<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
myfunc.<span style="color: #660066;">a</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// is undefined because a is private</span>
myfunc.<span style="color: #660066;">b</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// equals to 5 because b is public</span></pre></div></div>

<h2>Public and Private Methods in JavaScript</h2>
<p>As you may know in JS you can define functions inside other functions. Actually this is how you can define classes in JavaScript.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> myClass <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> 
<span style="color: #009900;">&#123;</span>
	<span style="color: #003366; font-weight: bold;">var</span> f <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #CC0000;">10</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>But this in fact defines a local (private) function into myClass and we cannot access it from the outside world. Here&#8217;s a fully functional class with one public and one private method.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> myClass <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #006600; font-style: italic;">// public method</span>
	<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">a</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #CC0000;">10</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// private method</span>
	<span style="color: #003366; font-weight: bold;">var</span> b <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #CC0000;">5</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> c <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> myClass<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
c.<span style="color: #660066;">a</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// will return 10</span>
c.<span style="color: #660066;">b</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// is undefined, because is private</span></pre></div></div>

<h2>Accessing Private Methods from Public Methods</h2>
<p>Easily we can access private methods from public ones.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> myClass <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #006600; font-style: italic;">// public method</span>
	<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">a</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">return</span> b<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// private method</span>
	<span style="color: #003366; font-weight: bold;">var</span> b <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #CC0000;">5</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>However accessing public mtehods in private ones is more difficult. That&#8217;s because we cannot use &#8220;this&#8221; into the private methods, just because &#8220;this&#8221; refers to the private method<br />
itself and not to the global method, which is &#8220;myClass&#8221; in our case.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> myClass <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #006600; font-style: italic;">// public method</span>
	<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">a</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #CC0000;">10</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// private method</span>
	<span style="color: #003366; font-weight: bold;">var</span> b <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">a</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// this will result in an error</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<h2>Accessing Public Methods from Private Methods</h2>
<p>To access public methods in private methods you need to define a variable that points to the global &#8220;this&#8221; object.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> myClass <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #003366; font-weight: bold;">var</span> self <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #339933;">;</span> 
&nbsp;
	<span style="color: #006600; font-style: italic;">// public method</span>
	<span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">a</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">return</span> <span style="color: #CC0000;">10</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #006600; font-style: italic;">// private method</span>
	<span style="color: #003366; font-weight: bold;">var</span> b <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">return</span> self.<span style="color: #660066;">a</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #006600; font-style: italic;">// this will return 10</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This variable gives you the bridge between private methods and global &#8220;this&#8221; pointer.</p>
<p>Related posts:<ol>
<li><a href='http://www.stoimen.com/blog/2011/05/30/object-oriented-javascript-inheritance/' rel='bookmark' title='Object Oriented JavaScript: Inheritance'>Object Oriented JavaScript: Inheritance</a></li>
<li><a href='http://www.stoimen.com/blog/2011/10/20/some-notes-on-the-object-oriented-model-of-php/' rel='bookmark' title='Some Notes on the Object-oriented Model of PHP'>Some Notes on the Object-oriented Model of PHP</a></li>
<li><a href='http://www.stoimen.com/blog/2010/08/11/quick-look-at-javascript-objects/' rel='bookmark' title='Quick Look at JavaScript Objects'>Quick Look at JavaScript Objects</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.stoimen.com/blog/2011/07/28/oop-javascript-accessing-public-methods-in-private-methods/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>A JavaScript Trick You Should Know</title>
		<link>http://www.stoimen.com/blog/2011/07/12/a-javascript-trick-you-should-know/</link>
		<comments>http://www.stoimen.com/blog/2011/07/12/a-javascript-trick-you-should-know/#comments</comments>
		<pubDate>Tue, 12 Jul 2011 14:49:46 +0000</pubDate>
		<dc:creator>Stoimen</dc:creator>
				<category><![CDATA[javascript]]></category>
		<category><![CDATA[micro tutorial]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Brace notation]]></category>
		<category><![CDATA[Computer programming]]></category>
		<category><![CDATA[Computing]]></category>
		<category><![CDATA[Concatenation]]></category>
		<category><![CDATA[Data types]]></category>
		<category><![CDATA[Formal languages]]></category>
		<category><![CDATA[heredoc]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[JavaScript programming language]]></category>
		<category><![CDATA[JavaScript syntax]]></category>
		<category><![CDATA[js solution]]></category>
		<category><![CDATA[possible solution]]></category>
		<category><![CDATA[Software engineering]]></category>
		<category><![CDATA[String]]></category>

		<guid isPermaLink="false">http://www.stoimen.com/blog/?p=2333</guid>
		<description><![CDATA[JavaScript Strings You should know that in JavaScript you cannot simply write a multilined code chunk, i.e. something like this: var str = 'hello world'; This will result in an error, which can be a problem when you deal with &#8230; <a href="http://www.stoimen.com/blog/2011/07/12/a-javascript-trick-you-should-know/">Continue reading <span class="meta-nav">&#8594;</span></a>
Related posts:<ol>
<li><a href='http://www.stoimen.com/blog/2011/08/18/powerful-php-less-known-string-manipulation/' rel='bookmark' title='Powerful PHP: Less Known String Manipulation'>Powerful PHP: Less Known String Manipulation</a></li>
<li><a href='http://www.stoimen.com/blog/2010/08/26/javascript-flexibility-regex-match/' rel='bookmark' title='JavaScript Flexibility &#8211; Regex match()'>JavaScript Flexibility &#8211; Regex match()</a></li>
<li><a href='http://www.stoimen.com/blog/2010/08/24/flexible-javascript-splitting-strings/' rel='bookmark' title='Flexible JavaScript &#8211; Splitting Strings'>Flexible JavaScript &#8211; Splitting Strings</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<h2>JavaScript Strings</h2>
<p>You should know that in JavaScript you cannot simply write a multilined code chunk, i.e. something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> str <span style="color: #339933;">=</span> <span style="color: #3366CC;">'hello
world'</span><span style="color: #339933;">;</span></pre></div></div>

<p>This will result in an error, which can be a problem when you deal with large strings. Imagine a string containing some HTML that should be injected via innerHTML. Now one possible solution is to concatenate two or more strings, by splitting the initial string.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> str <span style="color: #339933;">=</span> <span style="color: #3366CC;">'hello'</span>
        <span style="color: #339933;">+</span> <span style="color: #3366CC;">'world'</span><span style="color: #339933;">;</span></pre></div></div>

<p>Howerver this solution result in some additional operations like concatenation. It can be very nice if there was something like the PHP multiline string format.</p>
<h2>The PHP Example</h2>
<p>In PHP you have the same use case with strings. You can have a string concatenated over multiple lines, but you cannot split lines like so:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// the following line is wrong</span>
<span style="color: #000088;">$str</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'hello
world'</span><span style="color: #339933;">;</span> 
&nbsp;
<span style="color: #666666; font-style: italic;">// a possible solution to the line above is</span>
<span style="color: #000088;">$str</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'hello'</span>
     <span style="color: #339933;">.</span> <span style="color: #0000ff;">'world'</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// which is very similar to the js solution in the second example</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// ... but in PHP there is the so called HEREDOC</span>
<span style="color: #000088;">$str</span> <span style="color: #339933;">=</span> <span style="color: #0000cc; font-style: italic;">&lt;&lt;&lt;EOD
hello
world
EOD</span><span style="color: #339933;">;</span></pre></div></div>

<p>The heredoc gives us the power to write multiline strings. </p>
<h2>The JavaScript Trick</h2>
<p>Actually in JavaScript there is something that&#8217;s exactly what Heredoc is meant to be for PHP. Take a look at the last example:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> text <span style="color: #339933;">=</span> <span style="color: #339933;">&lt;&gt;</span>
<span style="color: #000066; font-weight: bold;">this</span> <span style="color: #339933;">&lt;</span>br <span style="color: #339933;">/&gt;</span>
<span style="color: #000066; font-weight: bold;">is</span>
my
multi<span style="color: #339933;">-</span>line
text
<span style="color: #339933;">&lt;/&gt;</span>.<span style="color: #660066;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
document.<span style="color: #660066;">body</span>.<span style="color: #660066;">innerHTML</span> <span style="color: #339933;">=</span> text<span style="color: #339933;">;</span></pre></div></div>

<p>Thus you can write multiline strings in the JavaScript code.</p>
<p>Related posts:<ol>
<li><a href='http://www.stoimen.com/blog/2011/08/18/powerful-php-less-known-string-manipulation/' rel='bookmark' title='Powerful PHP: Less Known String Manipulation'>Powerful PHP: Less Known String Manipulation</a></li>
<li><a href='http://www.stoimen.com/blog/2010/08/26/javascript-flexibility-regex-match/' rel='bookmark' title='JavaScript Flexibility &#8211; Regex match()'>JavaScript Flexibility &#8211; Regex match()</a></li>
<li><a href='http://www.stoimen.com/blog/2010/08/24/flexible-javascript-splitting-strings/' rel='bookmark' title='Flexible JavaScript &#8211; Splitting Strings'>Flexible JavaScript &#8211; Splitting Strings</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.stoimen.com/blog/2011/07/12/a-javascript-trick-you-should-know/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
	</channel>
</rss>

