The giant PHP List of Common Problems

  • (2 Pages)
  • +
  • 1
  • 2

28 Replies - 19084 Views - Last Post: 11 August 2010 - 04:40 AM Rate Topic: ****- 2 Votes

#4 JBrace1990  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 110
  • View blog
  • Posts: 760
  • Joined: 09-March 08

Re: The giant PHP List of Common Problems

Posted 23 August 2008 - 01:36 PM

I've decided to make this giant list over most of the problems that usually occur, and people have the most questions about.

Headers

The most problems that come from headers is the following error:

Quote

Warning: Cannot modify header information - headers already sent by (output started at ****) in *** on line ****


This error occurs when some type of data (usually an echo'd statement) is sent before you try to redirect someone. To fix it, you just need to move the redirection header up to the top of your page (around where session_start() should be if you're using it).

Other errors for headers occur when you are trying to force download things, or open images. you need to make sure you're using the correct type of header (IE: location, image/jpeg).

Sessions


certain errors include sessions not holding information (the fix is usually that session_start() does not exist at the top of the page), or errors popping up (meaning session_start is not at the top of the page. it needs to be set before anything is echo'd to the page).

MySQL


MySQL errors are usually very common. Following are the most common errors:

Quote

Mixing of GROUP columns
(MIN(),MAX(),COUNT(),...) with no GROUP columns is illegal if there is no GROUP BY clause


this error occurs when you're using certain functions (such as COUNT), and not having a group by at the end. Basically it is telling you that MySQL doesn't know WHAT to count.

Quote

MySQL client ran out of memory


This message usually indicates too many connections, and can be fixed by either increasing the amount of concurrent connections your DB can handle, but can usually be fixed by using mysql_close.

Quote

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in (****) on line ****


The above error is usually indicative of you either messing up the query that mysql_fetch_array points to, or not pointing to a query, or even that the variable you used (possibly $sql or $result) does not hold a query.

If MySQL returns 1 row less then it should (and you're positive your query is perfect), it's possible that you called something such as mysql_fetch_array or mysql_fetch_assoc once (or more) before you called it in the while loop. Remember that each call moves the pointer ahead by 1 result.

echo errors


echo errors usually occur when you do not escape certain characters, such as in the following quote:

Quote

echo "<table border="0">";


it will probably end either a white page, or a message such as :

Quote

unexpected T_WHITESPACE ON LINE *.


or:

Quote

Parse error: syntax error, unexpected T_LNUMBER, expecting ',' or ';' in **** on line ****


If/Elseif/Else Statements


the most common error for these is flawed logic. in the following statement, the if will never be activated, always leading to the else:

<?php
if($i > 1){
echo "Activated";
}
else
{
echo "Not Activated";
}
?>


The error is that $i is never set to anything. Another error is caused by being too strict:

<?php
$i = 3;
if($i > 2 && $i < 3)
{
echo "Activated";
}
?>


Since you are too strict (IE: the variable $i needs to be between 2.000~1 to 2.99999~, it will never be activated.

Elseif are the EXACT same as if statements. however in an if/elseif/else statement, only 1 of them are activated. this means that if the IF statement is True, even if the elseif statement is true, it will NOT be activated. If you want to activate 2 of them, use 2 if statements.

Common Errors


Other common errors include missing Semicolons (;­) at the end of a statement as below:
echo "Hello World"
echo "There's a php error in here."


Depending on the server information, you'll either end up with a white page or an error message.

Suggestions
  • Buy or Install a free trial of an IDE (Integrated Development Environment). It highlights any PHP syntax errors, and tells you what the error is. I would reccomend NuSphere PhpED
  • Read the DIC Tutorials. They're very helpful and usually very informative.
  • If you're going to ask DIC for help, Please don't be impatient, and make sure to tell us what is wrong. We're not going to go through your entire script just to help you unless you'retrying to help yourself as well.
  • although many of us here are good at PHP or other coding languages, this does not mean we know what you're talking about. I, for one, do not know things such as Smarty or XamPP, or Apache or any of them.
  • Have Fun. Help other people. If someone has helped you, chances are the time will come when you can help them back. All of us usually overlook some simple errors (I for one have been known to mess up some simple things, such as += and .= in PHP (which probably results from me learning like 5 languages at a time))

Feel free to post any revisions or elements this could use. I'm hoping to be able to make a thread where someone can look up a common problem and fi it by themselves.
Was This Post Helpful? 4

#5 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1641
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: The giant PHP List of Common Problems

Posted 23 August 2008 - 01:40 PM

Nice post JBrace1990, think I'll pin this one so others can easily find it
Was This Post Helpful? 1

#6 JBrace1990  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 110
  • View blog
  • Posts: 760
  • Joined: 09-March 08

Re: The giant PHP List of Common Problems

Posted 23 August 2008 - 01:45 PM

Thank you PsyoCoder =D I was hoping someone would... Anything else I should add to it? I ran out of ideas lol
Was This Post Helpful? 0
  • +
  • -

#7 Moonbat  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 36
  • View blog
  • Posts: 424
  • Joined: 30-June 08

Re: The giant PHP List of Common Problems

Posted 23 August 2008 - 02:55 PM

Here's one that bugged me a little bit one time.
The Code
while ($result = mysql_fetch_array($query)) { 
			echo "$result['data']";
		}

The Error

Quote

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING

The Solution
You cannot use single quotes inside the double quotes like this. An easy solution, instead of escaping the quotes, is to get rid of the single quotes:
while ($result = mysql_fetch_array($query)) { 
			echo "$result[data]";
		}

This post has been edited by Moonbat: 23 August 2008 - 02:56 PM

Was This Post Helpful? 0
  • +
  • -

#8 kummu4help  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 4
  • View blog
  • Posts: 245
  • Joined: 05-August 08

Re: The giant PHP List of Common Problems

Posted 25 August 2008 - 10:51 PM

nice post Jbrace1990

i would like to add following
while comparing for returntypes of function values with true or false it's better to do with === rather sticking to ==.

because sometimes == maynot treat 0(say the return value) as false
i.e
if(false==somefunction(returnsfalse))
{
   do something;
}



will not compare if the returnvalue is *0*
so always better to do the following

if(false===somefunction(returnsfalse))
{
   do something;
}


Was This Post Helpful? 0
  • +
  • -

#9 akozlik  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 90
  • View blog
  • Posts: 797
  • Joined: 25-February 08

Re: The giant PHP List of Common Problems

Posted 26 August 2008 - 07:40 AM

Logic Errors

A lot of people have trouble with their logic when determining whether or not to execute a section of code. I'm talking about the use of ANDs and ORs, specifically with the use of NOT.

The biggest problem I've seen is when somebody is saying something like this in their mind.

"If x is not equal to three or y is not equal to five, do this"

This would result in something like the following code:

if ($x != 3 || $y != 5)
{
     // code to execute
}



However, this would be incorrect. In your head you are saying OR, but what you really mean is AND. The above code would always return true. What you would be looking for is this:

if ($x != 3 && $y != 5)
{
     // code to execute
}



Hopefully that will clear up some logic errors. To see the post that this came from, check out:

Simple OR Statement
Was This Post Helpful? 0
  • +
  • -

#10 Hary  Icon User is offline

  • D.I.C Regular

Reputation: 44
  • View blog
  • Posts: 427
  • Joined: 23-September 08

Re: The giant PHP List of Common Problems

Posted 26 September 2008 - 01:38 AM

Unexpected behaviour of an if-statement:
Incorrect usage of an assignment when a comparison is is meant
if ($a = $b)



This piece of code will assign the value of $b to $a and php will return the value of $b. It will only evaluate to false iff $b == false

Correct usage:
if ($a == $b)

Was This Post Helpful? 0
  • +
  • -

#11 spearfish  Icon User is offline

  • Monkey in Training
  • member icon

Reputation: 10
  • View blog
  • Posts: 746
  • Joined: 10-March 08

Re: The giant PHP List of Common Problems

Posted 30 September 2008 - 07:28 PM

2 things:
First, to add on to this:
while ($result = mysql_fetch_array($query)) {
            echo "$result[data]";
        }


I would suggest making that:
while ($result = mysql_fetch_array($query)) {
            echo "{$result['data']}";
        }


The curly braces allow you to interpolate the data while retaining the single quotes. Without them, the parser FIRST looks for a CONSTANT named data, which can mess with your results.

Second,
One of the biggest things that tripped me up was that when I was first starting, I didn't know all of the built-in functions. You'll have a tough time finding the error in this code:
function link($target, $txt) {
$target = htmlentities($target);
$txt = htmlentities($txt);
return "<a href='$target'>$txt</a>";
}


if you don't already know that php has a built in function called link(). So, if php isn't letting you "redeclare" a function you know you haven't decalred yet, check out php.net/{function name} and see if something comes up.

This post has been edited by spearfish: 30 September 2008 - 07:29 PM

Was This Post Helpful? 0
  • +
  • -

#12 mazeroth  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 29
  • Joined: 11-December 08

Re: The giant PHP List of Common Problems

Posted 07 January 2009 - 09:21 AM

Careful when doing a redirect. Make sure to exit after a redirect if that's what the intention is. For example if in your code you have something like this:

header('Location:http://www.google.com');
header('Location:http://www.yahoo.com');

You will be taken you to yahoo since both headers get executed while:

header('Location:http://www.google.com');
exit;
header('Location:http://www.yahoo.com');

Takes you to google since you stop execution after the first header.
Was This Post Helpful? 0
  • +
  • -

#13 akozlik  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 90
  • View blog
  • Posts: 797
  • Joined: 25-February 08

Re: The giant PHP List of Common Problems

Posted 07 January 2009 - 10:51 AM

That is an excellent addition. That's especially important if you're doing conditional statements. You want to make sure you don't inadvertently call the wrong header.
Was This Post Helpful? 0
  • +
  • -

#14 basic_level  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 16-April 09

Re: The giant PHP List of Common Problems

Posted 16 April 2009 - 04:52 PM

nice post............
Was This Post Helpful? 0
  • +
  • -

#15 SpaceMan  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 10
  • View blog
  • Posts: 507
  • Joined: 20-February 03

Re: The giant PHP List of Common Problems

Posted 18 April 2009 - 03:07 PM

this is slower, the server has to assume things because you did not tell it...

code:$result[data] assumes $result["data"]
2 ways to fix
echo "{$result['data']}"; slower, has to evaluate the "" when it already knows the contents if the value.
fast,
echo 'text = '.$result['data'].'<br>';
single quotes get pushed to browser faster cause it does not have to evaluate it.
i see it everyday here in forums people using double quotes.

only time i use double quotes is when i am lazy for debug to echo out a bunch values.

View PostMoonbat, on 23 Aug, 2008 - 01:55 PM, said:

Here's one that bugged me a little bit one time.
The Code
while ($result = mysql_fetch_array($query)) { 
			echo "$result['data']";
		}

The Error

Quote

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING

The Solution
You cannot use single quotes inside the double quotes like this. An easy solution, instead of escaping the quotes, is to get rid of the single quotes:
while ($result = mysql_fetch_array($query)) { 
			echo "$result[data]";
		}

This post has been edited by SpaceMan: 18 April 2009 - 03:11 PM

Was This Post Helpful? 0
  • +
  • -

#16 DingleNutZ  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 121
  • Joined: 02-May 09

Re: The giant PHP List of Common Problems

Posted 04 May 2009 - 06:14 PM

if i have noticed a common human error in this forum
it is when using a variable with echo

i see people do it this way

<?php
$age = "15";
echo "my age is $age";
?>



the more correct way to do this is put qoutes around the variable

<?php
$age = "1";
echo "my age is";
echo "$age";

//or

echo "my age is \"$age\" ";
?>



with that last echo, i am pretty sure that is the correct way to use qoutes within qoutes

This post has been edited by DingleNutZ: 04 May 2009 - 06:15 PM

Was This Post Helpful? -1
  • +
  • -

#17 noorahmad  Icon User is offline

  • Untitled
  • member icon

Reputation: 209
  • View blog
  • Posts: 2,290
  • Joined: 12-March 09

Re: The giant PHP List of Common Problems

Posted 04 May 2009 - 10:53 PM

header
The Big problem that i was facing but i solved my Problem.
and the problem was that at the begging of the line i was missing a space and location problem which the Correct Spelling is "Location:"
 header("Location: index.php");


This post has been edited by noorahmad: 04 May 2009 - 11:00 PM

Was This Post Helpful? 0
  • +
  • -

#18 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 543
  • View blog
  • Posts: 1,713
  • Joined: 08-November 08

Re: The giant PHP List of Common Problems

Posted 25 June 2009 - 12:41 AM

View PostDingleNutZ, on 4 May, 2009 - 08:14 PM, said:

if i have noticed a common human error in this forum
it is when using a variable with echo

i see people do it this way

<?php
$age = "15";
echo "my age is $age";
?>



the more correct way to do this is put qoutes around the variable

<?php
$age = "1";
echo "my age is";
echo "$age";

//or

echo "my age is \"$age\" ";
?>



with that last echo, i am pretty sure that is the correct way to use qoutes within qoutes


Actually, your "more correct" method of echoing "my age is [$age's value]" is both more demanding on the server, and missing a space in the output. You'd end up with "my age is1", and execution time would drag down a tad because echo is very slow to execute.

The first method is commonly accepted practice. It can also be done with echo "my age is {$age}", though in that particular instance, curly braces are actually unnecessary.

While we're discussing this, it is important to remember that while single and double quotes are often interchangeable in PHP code, there is a critical distinction. If you were to execute this:

<?php

$age = 15;
echo "My age is $age.";
?>


Your output would be My age is 15.

However, if you were to execute this:

<?php

$age = 15;
echo 'My age is $age.';
?>


Your output would be My age is $age.. This is because single quotes tell PHP to handle everything contained within as plain text. Double quotes tells PHP to check for variable calls. You can get variable values to output with single quotes using concatenation, though, like this:

<?php

$age = 15;
echo 'My age is '.$age.'.';
?>


Where the output would be the same as the first example.

And yes, your example of using double quotes within a double quoted echo is correct. You would escape them.

This post has been edited by Valek: 25 June 2009 - 12:41 AM

Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2