Common errors

Place for questions and answers for all newcomers and new coders. This is a free for all forum, no question is too stupid and to noob.
Post Reply
Xaleph
Posts: 897
Joined: Mon Feb 07, 2011 2:55 am

Common errors

Post 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.
User avatar
SpiritWebb
Posts: 3107
Joined: Sun Jul 12, 2009 11:25 pm

Re: Common errors

Post by SpiritWebb »

Good post...very informational...thanks for sharing this
Image

Image
User avatar
Nexus
Posts: 293
Joined: Sat Jun 18, 2011 10:42 pm

Re: Common errors

Post 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! :)
alexrules01
Posts: 175
Joined: Sun Oct 11, 2009 9:33 am

Re: Common errors

Post 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']
Xaleph
Posts: 897
Joined: Mon Feb 07, 2011 2:55 am

Re: Common errors

Post 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];
User avatar
hallsofvallhalla
Site Admin
Posts: 12031
Joined: Wed Apr 22, 2009 11:29 pm

Re: Common errors

Post by hallsofvallhalla »

nice post!
Hattrick
Posts: 38
Joined: Tue Jan 17, 2012 1:11 am

Re: Common errors

Post by Hattrick »

Thank you Xaleph! Very helpful for me on my game!
Image
Post Reply

Return to “Beginner Help and Support”