<?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>PHP Reference Book Blog &#187; php errors</title>
	<atom:link href="http://www.phpreferencebook.com/tag/php-errors/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.phpreferencebook.com</link>
	<description>PHP Reference: Beginner to Intermediate PHP5</description>
	<lastBuildDate>Wed, 25 Aug 2010 12:58:18 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=abc</generator>
		<item>
		<title>PHP Errors &#8211; 10 Common Mistakes</title>
		<link>http://www.phpreferencebook.com/misc/php-errors-common-mistakes/</link>
		<comments>http://www.phpreferencebook.com/misc/php-errors-common-mistakes/#comments</comments>
		<pubDate>Fri, 27 Mar 2009 05:30:41 +0000</pubDate>
		<dc:creator>Mario Lurig</dc:creator>
				<category><![CDATA[Miscellaneous]]></category>
		<category><![CDATA[php errors]]></category>

		<guid isPermaLink="false">http://www.phpreferencebook.com/?p=136</guid>
		<description><![CDATA[ol li {margin-top: 50px;} Everyone makes mistakes when writing PHP code no matter your experience level. There are many common mistakes that sometimes are not immediately clear when looking at the error that PHP displays. First and foremost, when debugging PHP code, make sure you include the proper code to Display PHP Errors. Each item [...]]]></description>
			<content:encoded><![CDATA[<style type="text/css">
ol li {margin-top: 50px;}
</style>
<div style="float:right;"><script type="text/javascript" src="http://www.reddit.com/button.js"></script></div>
<div class="alignright" style="clear:right;"><script src="http://digg.com/tools/diggthis.js" type="text/javascript"></script></div>
<p>Everyone makes mistakes when writing PHP code no matter your experience level. There are many common mistakes that sometimes are not immediately clear when looking at the error that PHP displays. First and foremost, when debugging PHP code, make sure you include the proper code to <a href="http://www.phpreferencebook.com/tips/display-all-php-errors-and-warnings/">Display PHP Errors</a>.<br />
Each item will be organized by the PHP error itself, followed by chunks of invalid code that will be used as the baseline for the common mistakes, and finally the corrected code and solution will be presented.</p>
<ol>
<li>
<h3>Parse error: syntax error, unexpected &#8216;}&#8217; &#8230; on line 62</h3>
</li>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$assoc_array</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: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">$key</span> contains <span style="color: #006699; font-weight: bold;">$value</span><span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\r</span>&quot;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #b1b100;">switch</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$key</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">case</span> <span style="color: #0000ff;">'name'</span><span style="color: #339933;">:</span>
			<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Name found!'</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">case</span> <span style="color: #0000ff;">'age'</span><span style="color: #339933;">:</span>			
			<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Age found!'</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">default</span><span style="color: #339933;">:</span>
			<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'No Case found!'</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>When you forget an opening curly brace &#8216;{&#8216;, or if you have an extra closing curly brace, this error is generated at the closing brace, referred to in the above example as line 62. Many frameworks and syntax highlighters such as <a href="http://notepad-plus.sourceforge.net/uk/site.htm">Notepad++</a> will assist you in finding the matching brace by simply placing your cursor next to a curly brace.</p>
<p><strong><em>Answer:</em></strong> Add the opening brace to the end of line 48:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>48
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$assoc_array</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></pre></td></tr></table></div>

<li>
<h3>Parse error: syntax error, unexpected $end &#8230; on line 63</h3>
</li>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$assoc_array</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: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">$key</span> contains <span style="color: #006699; font-weight: bold;">$value</span><span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\r</span>&quot;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #b1b100;">switch</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$key</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">case</span> <span style="color: #0000ff;">'name'</span><span style="color: #339933;">:</span>
			<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Name found!'</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">case</span> <span style="color: #0000ff;">'age'</span><span style="color: #339933;">:</span>			
			<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Age found!'</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">default</span><span style="color: #339933;">:</span>
			<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'No Case found!'</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>On the opposite end of the spectrum, forgetting a closing curly brace &#8216;}&#8217; will generate this obscure error. Obscure because the line number refers to the bottom of the page. It is unable to identify the closing of the control structure.</p>
<p><strong><em>Answer:</em></strong> Add the closing brace for the switch that begins on line 51. In the sample code, the brace is missing from line 61:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>58
59
60
61
62
</pre></td><td class="code"><pre class="php" style="font-family:monospace;">		<span style="color: #b1b100;">default</span><span style="color: #339933;">:</span>
			<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'No Case found!'</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<li>
<h3>Parse error: syntax error, unexpected &#8216;{&#8216; &#8230; on line 42</h3>
</li>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>42
43
44
45
46
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$numeric</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">1</span> <span style="color: #009900;">&#123;</span> 
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Equal to 1'</span><span style="color: #339933;">;</span> 
  <span style="color: #009900;">&#125;</span><span style="color: #b1b100;">else</span><span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Not equal to 1'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>While this may seem obvious with the simplified example above, this comes up very often when you have functions nested inside of the evaluated expression of a control structure. For a momentary detour, here is an example of the exact situation that could easily lead to this error. <em>Note: This sample code would also generate the same error.</em></p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$numeric</span> <span style="color: #339933;">==</span> <span style="color: #990000;">stripslashes</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">intval</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> 
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Equal to 1'</span><span style="color: #339933;">;</span> 
  <span style="color: #009900;">&#125;</span><span style="color: #b1b100;">else</span><span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Not equal to 1'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The error is generated because of a missing closing parenthesis &#8216;)&#8217;. In the above cases, the missing character is the matched pair to the opening parenthesis &#8216;(&#8216; that encompasses the evaluated expression. In the very first line, you can count 3 &#8216;(&#8216; and only 2 &#8216;)&#8217;, causing PHP to reach an unexpected curly brace which begins the behaviour when the expression is evaluated as TRUE. </p>
<p><strong><em>Answer:</em></strong> Add the closing parenthesis to line 42.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>42
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$numeric</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span></pre></td></tr></table></div>

<li>
<h3>Parse error: syntax error, unexpected T_VARIABLE &#8230; on line 38</h3>
</li>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>37
38
39
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$num_array</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">5</span>
<span style="color: #000088;">$get_test</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'test'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$escaped</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'A great movie is \'Shawshank Redemption\', in my humble opinion.'</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>The error has identified the variable <strong>$get_test</strong> on line 38 and is surprised by its presence suddenly! Why? Because the most important character in all PHP programming is missing, and as soon as it reaches the next item, it throws out the error, the specific error because the next item is a variable. It is reading the line as if it was written:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$num_array</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">5</span><span style="color: #000088;">$get_test</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'test'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span></pre></div></div>

<p><strong><em>Answer: </em></strong>There is a semicolon missing after the 5 on line 37, closing the behaviour of assigning the integer 5 to the next numeric index in the <strong>$num_array</strong> array variable.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>37
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$num_array</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">5</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<li>
<h3>Parse error: syntax error, unexpected &#8216;}&#8217;, expecting &#8216;,&#8217; or &#8216;;&#8217; &#8230; on line 44</h3>
</li>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>42
43
44
45
46
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$numeric</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> 
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Equal to 1'</span>
  <span style="color: #009900;">&#125;</span><span style="color: #b1b100;">else</span><span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Not equal to 1'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Ok, this is the same problem as the previous item in the list, but the error is completely different because of the next element reached by PHP after the missing character. In the above example, PHP identifies the opening curly brace of the else statement.</p>
<p><strong><em>Answer: </em></strong>There is a semicolon missing after the echo statement on line 43, leading PHP to unexpectedly reach the closing brace of the if statement.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>42
43
44
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$numeric</span> <span style="color: #339933;">==</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> 
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Equal to 1'</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #b1b100;">else</span><span style="color: #009900;">&#123;</span></pre></td></tr></table></div>

<li>
<h3>Parse error: syntax error, unexpected &#8216;,&#8217; &#8230; on line 38</h3>
</li>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>38
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$escaped</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'A great movie is \'Shawshank Redemption'</span><span style="color: #339933;">,</span> in my humble opinion<span style="color: #339933;">.</span><span style="color: #0000ff;">';</span></pre></td></tr></table></div>

<p>Using a syntax highlighter should make this error very evident, as you notice that the string that is being assigned to the variable <strong>$escaped</strong> changes its coloring midway through. The entire string is surrounded by single quotes, however single quotes are also used inside, and while the first one has been escaped by a backslash so it is evaluated as a single quote character rather than the encapsulating single quotes, it seems that one is missing&#8230;</p>
<p><strong><em>Answer: </em></strong>Escape the 3rd single quote, after Redemption, which in the above example is acting as the closing single quote to identify the string.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>38
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$escaped</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'A great movie is \'Shawshank Redemption\', in my humble opinion.'</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<li>
<h3>Parse error: syntax error, unexpected &#8216;=&#8217;, expecting &#8216;)&#8217; &#8230; on line 32</h3>
</li>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>32
33
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$assoc_array</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'name'</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">'John Smith'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'age'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">35</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'gender'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'male'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$num_array</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;">,</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'three'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'four'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>When defining an array, you can refer to simply the values (and let the index be assigned numerically and automatically) or you can define the key and value pairs. Any other syntax can trigger a parsing error, since PHP is expecting that before placing an equals sign you would close the array.</p>
<p><strong><em>Answer: </em></strong>Line 32 is missing the greater than symbol after the equals sign to define the value for the key &#8216;name&#8217;.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>32
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$assoc_array</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'name'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'John Smith'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'age'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">35</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'gender'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'male'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<li>
<h3>Parse error: syntax error, unexpected T_STRING, expecting &#8216;)&#8217; &#8230; on line 32</h3>
</li>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>32
33
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$assoc_array</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'name'</span> <span style="color: #339933;">=</span> John Smith<span style="color: #339933;">,</span> <span style="color: #0000ff;">'age'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">35</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'gender'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'male'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$num_array</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;">,</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'three'</span><span style="color: #339933;">,</span><span style="color: #0000ff;">'four'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Variable types have specific syntax when assigning the values. Arrays use <em>array()</em>, integers and floats are written directly sans extraneous syntax (<em>e.g.</em> $variable = 12; ), and strings are surrounded by single or double quotes. Neglecting to surround a string with quotations causes PHP to find an unexpected T_STRING and look for a missing character that is part of a control structure or language construct.</p>
<p><strong><em>Answer: </em></strong>John Smith is a string, the value of the key &#8216;name&#8217; (also a string), but was missing quotation marks.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>32
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$assoc_array</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'name'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'John Smith'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'age'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">35</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'gender'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'male'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<li>
<h3>Warning: Missing argument 1 for preprint(), called &#8230; on line 27 and defined &#8230; on line 21</h3>
</li>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>21
22
23
24
25
26
27
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> preprint<span style="color: #009900;">&#40;</span><span style="color: #000088;">$parray</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$nl</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\r</span>&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;pre&gt;'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$nl</span><span style="color: #339933;">;</span>
	<span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$parray</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000088;">$assoc_array</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'name'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'John Smith'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'age'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">35</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'gender'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'male'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
preprint<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>The error states that a function, called on line 27, is missing the first argument (<em>e.g.</em> the elements within the parenthesis), as defined by the function that is created on line 21. When the function is written to expect an argument, and none is provided nor the argument within the function has a default value assigned, a PHP warning is presented. If the function was written as follows, with a default value, the warning would not appear and the default value would be used:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> preprint<span style="color: #009900;">&#40;</span><span style="color: #000088;">$parray</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'name'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'Test'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000088;">$nl</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span><span style="color: #000099; font-weight: bold;">\r</span>&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;pre&gt;'</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$nl</span><span style="color: #339933;">;</span>
	<span style="color: #990000;">print_r</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$parray</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p><strong><em>Answer: </em></strong>Provide the argument in the function call on line 27.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>26
27
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$assoc_array</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'name'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'John Smith'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'age'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #cc66cc;">35</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'gender'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'male'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
preprint<span style="color: #009900;">&#40;</span><span style="color: #000088;">$assoc_array</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<li>
<h3>Make sure you understand the difference between == and ===</h3>
</li>
<p>Ok, so this last one isn&#8217;t a PHP error, but it bears repeating. Failing to understand the difference and the appropriate usage of equality and identical comparison operators will not generate an error by PHP, but will likely find you debugging your code for a very long time, wondering what is wrong with your data. Make sure you understand the <a href="http://www.phpreferencebook.com/samples/equal-sign-operators/">equals sign in PHP</a>! Remember, the following If statement will always be TRUE because you are assigning the variable rather than evaluating it:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$numeric</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span> 
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Equal to 1'</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #b1b100;">else</span><span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'Not equal to 1'</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

</ol>
<p>PHP can be a cruel mistress, but if you decode the PHP errors and warnings, you&#8217;ll find the total time building your application, website, or school project will be dramatically reduced!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.phpreferencebook.com/misc/php-errors-common-mistakes/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Display all PHP Errors and Warnings</title>
		<link>http://www.phpreferencebook.com/tips/display-all-php-errors-and-warnings/</link>
		<comments>http://www.phpreferencebook.com/tips/display-all-php-errors-and-warnings/#comments</comments>
		<pubDate>Tue, 16 Sep 2008 05:10:08 +0000</pubDate>
		<dc:creator>Mario Lurig</dc:creator>
				<category><![CDATA[Tips]]></category>
		<category><![CDATA[debugging]]></category>
		<category><![CDATA[display errors]]></category>
		<category><![CDATA[display_errors]]></category>
		<category><![CDATA[error reporting]]></category>
		<category><![CDATA[E_ALL]]></category>
		<category><![CDATA[php errors]]></category>
		<category><![CDATA[php warnings]]></category>

		<guid isPermaLink="false">http://www.phpreferencebook.com/?p=18</guid>
		<description><![CDATA[If you wish to see all the PHP errors and warnings in your script, include the following bit of code:
error_reporting(E_ALL);
ini_set('display_errors', '1');]]></description>
			<content:encoded><![CDATA[<p>Every time I was debugging my pages I found myself searching around for this little chunk of code to display PHP errors. So, I put in the book so it was always nearby. Since people still search google endlessly, I thought I would provide it here as well. If you wish to see all the PHP errors and warnings in your script, include the following bit of code:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">error_reporting</span><span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">E_ALL</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #990000;">ini_set</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'display_errors'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'1'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Now, continue to beat your head against your keyboard while you continue to hunt down your missing semicolon or closing parenthesis.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.phpreferencebook.com/tips/display-all-php-errors-and-warnings/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>
