Page 1 of 1

Common errors

Posted: Sat Jul 23, 2011 1:25 am
by Xaleph
First of, let me begin by saying that I think everyone here is doing a wonderfull job of helping each other out. However, it seems that most of the coding problems (PHP really..) here are common mistakes. I`d like to dedicate this post to some of the most common errors.

To begin: and this really should be mandatory; add the following PHP to every .php page you have:

$debug = true; // switch to false once you finished your game :)
define("DEBUG", $debug); // define a global usable DEBUG constant ( you can use it on all your pages after it is defined )
error_reporting(E_ALL); // tells PHP to report ALL errors possible ( notices, warnings, fatal errors et cetera )
ini_set('display_errors', $debug); // this command actually forces PHP to display them, what good is reporting if they`re not going to be displayed?

Now on to the next thing:
UNEXPECTED ? whatever

This almost always means you forgot to close something on the PREVIOUS line. So if you have an error that says:
T_UNEXPECTED something on line: 10

Look for a problem first on line 9. If you are damn sure that`s correct, start looking at line 10.

Now, sometimes, you get errors saying:
T_UNEXPECTED $var ( where $var is any variable name you want to give it.. )

This error means you failed to declare a variable somewhere. Something like:
if($var == "hello"){}

You check whether $var equals something, right? However, if you never said what $var is, there`s bound to be an error, hence an UNEXPECTED $var

Also, what most people tend to forget are the following:
1. ending a normal line with the semi-colon ( ; )
2. Forgotting brackets { and }
3. Encapsulating statements if(1==1){ if(1==2)else{{ if(1==1){} else{}}} // will throw an error, since a bracket is missing
4. Using $vars in strings. $var = "Hello $name"; // will work
\t $var = 'hello $name'; // however, will not :)

Anyway, everyone can share experiences here or even better, provide more common errors + solutions. What really would be great is that people look here first, ask questions later. That way we won`t have to repeat the same awnsers over and over again.

Re: Common errors

Posted: Sat Jul 23, 2011 2:28 am
by SpiritWebb
Good post...very informational...thanks for sharing this

Re: Common errors

Posted: Sat Jul 23, 2011 2:40 am
by Nexus
Yes I agree thanks Xaleph! I have also been using that bit of code you tell everyone to put in. It works wonders on my code! :)

Re: Common errors

Posted: Sat Jul 23, 2011 9:43 am
by alexrules01
you can add...
I think the error is on the lines of "Undefined variable: name, assumed 'name'
instead of:

$playerinfo3[name] you need $playerinfo3['name']

Re: Common errors

Posted: Sun Jul 24, 2011 9:31 pm
by Xaleph
Alex, good one,

Don`t let PHP assume anything, it will always guess wrong.

All array`s you are trying to access should be strings. This can be like $array["some_name"]; or $var = "some_name"; $array[$var] but never use constants like $array[some_name];

Re: Common errors

Posted: Mon Jul 25, 2011 6:32 pm
by hallsofvallhalla
nice post!

Re: Common errors

Posted: Thu Jan 19, 2012 7:25 pm
by Hattrick
Thank you Xaleph! Very helpful for me on my game!