PHP - Understanding Errors
From Global Programming Syntax
T_STRING + T_PRINT + T_IF + T_VARIABLE + T_ECHO errors
All T_ errors are normally due to a missing return symbol. This is a very common error for many forgetful programmers since only one commonly used character needs to be missing. Below are examples of an error when using the echo function:
echo 'test' //no return symbol ( ; ) causes error
print_r(array('a','b','c')); //T_STRING
echo 'test' //no return symbol ( ; ) causes error
print '<br>second line'; //T_PRINT
echo 'test' //no return symbol ( ; ) causes error
$var=preg_replace('/[^A-Z]/','','word123'); //T_VARIABLE
echo 'test' //no return symbol ( ; ) causes error
if (isset($_GET)) {} //T_IF
echo 'test' //no return symbol ( ; ) causes error
echo '<br>second line'; //T_ECHO
The correction to all of the above errors is by replacing the first line with the following:
echo 'test';
As you can see in the correction, the only change is an added return symbol at the end of the line. That is normally the solution to the error however, the line reported in the error isn't always the line that needs correcting. This is always the case the the T_ errors as the error is made by a missing return sign after a quote however, the effect of the missing return sign is on the the next (non-blank) line down from where the return should be. So when comparing your T_ error with your code, allways check a few lines before the reported line.
Parse error
A parse error is a lot like a T_ error except it is not being limited to a missing return after a quote. Instead a parse error means a missing return symbol or bracket after a function, method, if statement or loop. So below are some examples of the error:
$var=str_replace('from','to',"I'm going from the zoo") //no return symbol ( ; ) causes error
echo $var;
if (isset($_GET)) {
}} //too many closing brackets
if (isset($_GET) { } //missing a bracket
while(isset($_GET) //missing a bracket
{ break; }
for ($i=0;$i<10) { //for statement invalid
echo $i.'<br>';
$i++; }
<!-- Some versions of php will comment the php closing tag -->
<?//include("menu.php");?>
<b>test</b>
Now you will notice with the above examples a wide variety of parse errors each with simple solutions. However, not all parse errors are the same. Sometimes additional information is provided such as:
- parse error, expecting `';''
- parse error, expecting `','' or `';''
parse error, expecting `';''
Depending on your version of php, this will usually refer to something like a for loop missing one of its statement ; dividers. An example of that situation is as follows:
for ($i=0;$i<10) { //for statement invalid
echo $i.'<br>';
$i++; }
parse error, expecting `','' or `';''
This can be caused by an unescaped quotation mark. Below is a sample problem and three solutions:
//Example:
echo 'hopefully it's displayed';
//Solution 1 - changing the quote type:
echo "hopefully it's displayed";
//Solution 2 - escaping the quotes:
echo 'hopefully it\'s displayed';
//Solution 3 - change the quotes in an advanced way (not recommended):
echo 'hopefully it'."'".'s displayed';
As you can see in the example, the quotation mark inside the same type of quotation mark sends php crazy. So best way is to just escape the quotation mark by adding the backslash before the quotation mark.
Fatal error: Can't use function return value in write context
This is generally reported when one functions output type does not match the type for the usage input. An example includes:
if (isset(str_replace('1','2','11223344'))) { }
With the above example, the input must be a variable and not the value of a function. So to correct the error in the above example the following script would be used even though the if statement will always pass as true.
$var=str_replace('1','2','11223344');
if (isset($var)) { }
Warning: session_start() [function.session-start]: ................................ - headers already sent by
This error with the blank space covers 2 errors saying basically the same thing. It means that you submitted html/text output before you used session_start(). Below are two example scripts.
<html>The solution to the above script is as follows:
<?php
session_start();
<?phpBelow is the second example of a script giving the same error.
session_start();
?><html>
<?phpThe solution to the above script is as follows:
$var=array('a','b','c','d','e','f');
print_r($var);
session_start();
<?php
session_start();
$var=array('a','b','c','d','e','f');
print_r($var);
So as you can see, with the above example problems, there is always some output before session_start(). However there is another twist to this error. Some text editors don't follow the standards and will put some invisible garbage at the beginning of the file which will submit to the browser causing the error. If you have already tried the above and think this could be your problem then first open your file in the old fashion windows notepad. Then delete any black squares or anything before the <? symbol. Then save the file, preview under wamp or upload. Then see if that works. If you uploaded the file and it didn't work, be sure that you uploaded in text mode and not binary as that will make a great difference on how your script will perform.
Warning: mysql_fetch_......(): supplied argument is not a valid MySQL result resource
This is another common error for those using mysql a lot. Basically it is saying that the variable placed into the function does not have the correct results. First lets consider the following example:
<?
mysql_connect('localhost','root','');
mysql_select_db('test_database');
$result=mysql_query('SELECT * FROM `table`');
while ($row=mysql_fetch_array($result)) {
echo $row[0].'<br>';
}
In the above example, while it looks perfectly valid, there is one fatal floor. That is if the table is empty then it will spit out an error complaining mysql_fetch_array hasn't got a result with data. To solve that error simply replace the above example with the following:
<?So if the above example now being perfect, what else can cause this error. Well sometimes if you mis-spell the variable somewhere, alter it's value or even unset the $result variable that will trigger an error. So below is en example of another problem and is more common in huge complicated scripts.
mysql_connect('localhost','root','');
mysql_select_db('test_database');
$result=mysql_query('SELECT * FROM `table`');
if (mysql_num_rows($result)>0) {
while ($row=mysql_fetch_array($result)) {
echo $row[0].'<br>';
}
}
<?
mysql_connect('localhost','root','');
mysql_select_db('test_database');
$result=mysql_query('SELECT * FROM `table`');
//300 lines of code here
$result='Hello world';
echo $result;
//200 lines of code here
if (mysql_num_rows($result)>0) {
while ($row=mysql_fetch_assoc($result)) {
echo $row[0].'<br>';
}
}
So as you can see in the above example, the variable has been overridden and now is unusable. So that is worth checking if that is happening if your still having problems.
Fatal error: Only variables can be passed by reference
This error is caused by placing a string or integer into a function instead of a variable. Below is an example of the error:
echo str_replace('1','4','113344',2);
And so how to solve it, well the number 2 in the script should actually be an empty variable containing no data like the following:
echo str_replace('1','4','113344',$variable);
After that script is executed, the variable named $variable will then contain a second stream of data which the function outputs. For those who didn't get what that previous sentence means, it means that the variable $variable will contain new data after the line of code has ran and it is not possible to assign a value to a value. It is only possible to assign a value to a variable or array. And that is what the error is complaining about.
Another example of the error which may look more familiar is preg_match_all. If you forget to make the third input a variable then again this fatal error will occur. So below are more examples and their solutions:
//Example:
preg_match_all('/\<([^\/][^\<]+)\>/','<html><body>html text</body></html>','some value');
//Solution:
preg_match_all('/\<([^\/][^\<]+)\>/','<html><body>html text</body></html>',$matches);
//Example:
asort(array('a','e','c'));
//Solution:
$darray=array('a','e','c');
asort($darray);
