views:

178

answers:

8

Hello There,

What can be the common PHP coding pitfalls which probably many of us are not avoiding?

+6  A: 

Short and sweet, reliance on register_globals being on

http://php.net/manual/en/security.globals.php

Scuzzy
yeah that's the least every developer should know.
Sarfraz
+4  A: 

Insufficient use of: mysqli_prepare/mysqli::prepare

Tegeril
Or PDO's equivalents.
Justin Johnson
..or completely disuse of them -.-
DaNieL
+2  A: 

Although this applies to most languages with any kind of library worth mentioning, one of the most common pitfalls is reinventing the wheel, or rewriting it, in the case of programming. Being deeply familiar with the available standard library, aware of the popular, compilable modules is one of the greatest assets an programmer can have. Being ignorant of or just plain ignoring these pre-made and time tested solutions is a huge pitfall.

Justin Johnson
+2  A: 

One thing you should be warned is that PHP will allow you to code how ever you want, this by itself doesn't sound so bad, but it is, when the programmer doesn't have any programming basis.

So a little advice. When creating your first site, try to keep the code organized. Don't put a lot of php in html or a lot of html in php.

When you feel up to it, try looking at some php frameworks (a quick search in google will provide many links)

One last piece of advice. If you ever move away from PHP don't look back and say PHP was a bad language, it was you, it was all your fault! You were the bad programmer! :)

AntonioCS
+2  A: 

I'd say using PHP is a coding pitfall, but then again I've been there and back. You weren't there, man! You weren't there...

Tor Valamo
+1  A: 

Easy.... Echoing HTML!

It pisses me off, why do people echo HTML? For instance:

echo("<ul>")
foreach($arr as $key => $value) {
   echo("    <li>".$value."</li>");
}
echo("</ul>");

Hey, do you remember that PHP is an embedded language? Probably not as your echoing out hundreds of lines of HTML just to make sure your code looks pretty when you view source.

ILMV
Agree with you, but sometimes is faster and easyest echo **small** pieces of html instead of close and reopen the php tags
DaNieL
I agree as well, but many opening and closings will slow down the parsing, wont it? also, if there's too many - too close it becomes very hard to read IMO.
Tommy
I doubt it, it is an embedded language, your meant to embed it into your HTML, not the other way round. I also think echoing will put more load on the server than opening and closing HTMl tags. Finally HTML within a php tags will not be highlighted by your code editor, making that harder to read.
ILMV
"HTML within a php tags will not be highlighted by your code editor, making that harder to read".. i've heard this so many times, but i cant figured how could an experienced programmer fail just becose 2 `<li></li>` are not highlighted... remember, i said that echoing html could be goo for small pieces, and only in particular cases ;)
DaNieL
Yes and I agree, I guess I provided a basic example so not to infuriate myself, but when you get someone who echos 10 lines os static HTML (no dynamic logic). I guess this is all a moot point as any complicated project will usually separate presentation and logic, perhaps even using a template engine such as smarty. But for those starting out and looking for pitfalls, echoing HTML might just be one you want to avoid.
ILMV
A: 

Not comment functions / classes / whatever.

DaNieL
+1  A: 
mysql_query("SELECT * FROM foo WHERE bar = '" . $_GET["baz"] . "'");
Charlie Somerville