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.

Common errors

Postby Xaleph » Fri Jul 22, 2011 8:25 pm

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.
Xaleph
 
Posts: 897
Joined: Sun Feb 06, 2011 9:55 pm
Location: Groningen, Netherlands
Has thanked: 0 time
Been thanked: 2 times
Blog: View Blog (6)

Re: Common errors

Postby SpiritWebb » Fri Jul 22, 2011 9:28 pm

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

Image
User avatar
SpiritWebb
Rockywood Studios
 
Posts: 3107
Joined: Sun Jul 12, 2009 6:25 pm
Location: Little Rock, AR
Has thanked: 6 times
Been thanked: 11 times
Blog: View Blog (2)

Re: Common errors

Postby Nexus » Fri Jul 22, 2011 9:40 pm

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! :)
User avatar
Nexus
 
Posts: 293
Joined: Sat Jun 18, 2011 5:42 pm
Location: Colorado
Has thanked: 0 time
Been thanked: 0 time
Blog: View Blog (1)

Re: Common errors

Postby alexrules01 » Sat Jul 23, 2011 4:43 am

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']
alexrules01
 
Posts: 175
Joined: Sun Oct 11, 2009 4:33 am
Has thanked: 0 time
Been thanked: 0 time

Re: Common errors

Postby Xaleph » Sun Jul 24, 2011 4:31 pm

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];
Xaleph
 
Posts: 897
Joined: Sun Feb 06, 2011 9:55 pm
Location: Groningen, Netherlands
Has thanked: 0 time
Been thanked: 2 times
Blog: View Blog (6)

Re: Common errors

Postby hallsofvallhalla » Mon Jul 25, 2011 1:32 pm

nice post!
User avatar
hallsofvallhalla
Site Admin
 
Posts: 11998
Images: 13
Joined: Wed Apr 22, 2009 6:29 pm
Location: mobile, al
Has thanked: 11 times
Been thanked: 164 times
Blog: View Blog (3)

Re: Common errors

Postby Hattrick » Thu Jan 19, 2012 2:25 pm

Thank you Xaleph! Very helpful for me on my game!
Image
Hattrick
 
Posts: 38
Joined: Mon Jan 16, 2012 8:11 pm
Has thanked: 0 time
Been thanked: 0 time


Return to Beginner Help and Support

Who is online

Users browsing this forum: No registered users and 1 guest

cron

x