views:

3718

answers:

41

What are the worst practices found in PHP code?

Some examples:

  • Use of $array[reference] without single quotes
  • Instance "hidden" variables into inclusion files, which are needed later
  • Lots of recursive inclusion not using "_once" functions

Note: maybe subjective or "fun" if you like.

+71  A: 

Using and relying on register_globals. Yuck.

Oli
Very True ( how could i forgot? )
Joshi Spawnbrood
That will come to an abrupt and ignominious with the release of 6.0.
Lucas Oman
Only if extract is removed from the core library.
jmucchiello
All the examples in one of the books I have need register_globals turned on -- as a PHP newbie, it wasn't exactly obvious. (It was a Linux box with StartCom Enterprise Linux 5 on it as the server... good times!)
Lucas Jones
Ahh, register_globals, possibly the biggest reason people will not upgrade to PHP 6. Getting people to upgrade from PHP 4 to PHP 5 was a huge effort already, but getting them onto PHP 6... That will take ages. Anyway, if there was ever justification for creating a time machine, this is it.
Michael Stum
+43  A: 

How about Magic Quotes? So evil they've been removed from PHP6.

Greg
Second that. Thank GOD magic quotes are gone.
Nicholas Flynt
Yes, it was like a seatbelt...fastened around the neck.
micahwittman
Actually, I stopped using php because of rubbish like magic_quotes, "safe" mode, etc. No second chances.
Longpoke
@Longpoke Safe mode will be gone too as of PHP6. Finally no more extremely tied down Shared Hosting.. ugh. (I got a VPS I was so tired of Safe Mode limitations)
Earlz
+3  A: 

Overuse of globally scoped variables in general. And, erm... using PHP ;-)

Dominic Rodger
Hey, I wanted to write that. :-)
Tomalak
Agree with overuse of scoped variables. Disagree STRONGLY with the bash of PHP, I don't think I would use anything else if given a choice as the backend for my websites.
Nicholas Flynt
PHP is a procedural language (really) so global variables are less of an evil. Without encapsulation, sometimes you just have to use a global.
jmucchiello
+34  A: 

HTML and PHP and SQL interspersed without any discipline, resulting in an unmaintainable mess.

Tom Haigh
that's not really specific for PHP.. But is is a really bad thing
Jacco
Maybe not, but it is the classic example that you find causing php projects that are impossible to maintain
Tom Haigh
I tend to "mix" a lot, but I do all my logic and calculation before lines of HTML. That HTML may include lots of PHP to control formatting based on pre-calculated logic, but I'm generally good about keeping it separated.
Nicholas Flynt
You can mix php with HTML if you do it with care. Only ifs, foreach's and echo's. There is no need for yet another language like smarty when PHP itself was designed as a templating language. Just make sure not to do anything unrelated to ouput (like mysql querys or calculations) in the HTML.
Pim Jager
Yes and that's fine, but it is still bad practise to do it badly, which is what I meant
Tom Haigh
Yea, my last project I did I split evenly into two different parts. One is the "templating" where I write out all the HTML code. The other is the "engine" which calls the templating functions to write out HTML and all that. My index.php and postpacks.php files work as the controller. So I made my own simiplistic MVC..ish framework
Earlz
+48  A: 
  • Using the dot operator to stuff variables into queries, without any form of escaping or variable binding. This still happens a lot.
  • Using extract() liberally to have easy access to variables. We got rid of register_globals only to see it replaced with extract($_POST).
Joeri Sebrechts
oh boy, extract is a nightmare.
bbxbby
Extract is almost as dangerous as eval()
Lucas Oman
I'm not entirely certain that I understand the dot operator thing, unless you mean a lack of database sanitization, but its not clear from the statement.
Nicholas Flynt
I meant concatenating variables into strings to build queries. Most people writing PHP scripts don't know the meaning of the term "database sanitization".
Joeri Sebrechts
+1 to extract, but it can be useful in combination with EXTR_IF_EXISTS
Ken
wtf? extract is awesome when used properly. Using it for $_POST is just damn sick, but you can't blame extract as such.
dr Hannibal Lecter
I used extract once in a function that took an associative array. The function used output buffering and included a template file, after calling `extract` on the array passed to it. That way, the template was loaded into a context containing simple variables rather than having to reference them from the array. Isn't this fine usage?
Carson Myers
Extract is PHP's equivalent of pointer arithmetic in C. It's quite handy if you know what you're doing, it's a disaster if you don't.
Joeri Sebrechts
+27  A: 

HTML + PHP + SQL all nested and messed up together.

Add in a bunch of global functions relying on global variables, and you've got a right mess.

And this was just in the first 30 lines of the index.php file of a major open source project that shall remain nameless...

madlep
How about HTML+PHP+SQL+JS+CSS, I've seen that...
Dean
Let me guess... WordPress?
Maciej
sounds like pretty much 70% of the php sites out there.
jcoby
Why remain nameless? Bad coders should be shamed into becoming good coders. WORDPRESS SUCKS.
Lucas Oman
Um... I tend to *use* HTML+PHP+SQL+JS+CSS. The difference is that (even when its all nested together) its not messy. The trick to a project like that is learning how to keep the different languages separated, even when PHP is generating parts of the other languages on the fly.
Nicholas Flynt
I'm all for proper coding practices and whatnot, but from a user perspective, Wordpress is probably the best blogging software out there. At the end of the day, who cares what the code looks like.
nickf
@nickf: I do! Cause I have to read it, fix it, tweak it, use it, etc., etc...
Svish
(That was an In General comment btw, not Wordpress spesific)
Svish
At one point there was an example on php.net of the mysql_get_assoc which did this. I swear I worked on at least 3 sites where every query display was copy pasted from that example.
Nick
+40  A: 

using @ everywhere because they're too lazy to use isset()

Lewis
Or "empty();"!
Ty
Or just ignoring all the warnings caused by undefined array indexes. This makes being the only guy who actually has all E_ALL on and looks at the log file painful. Though this will be mitigated when we switch to 5.3
therefromhere
A: 

Setting the error reporting to 0 - error_reporting(0);

Good in production mode - very bad in development mode.

Binny V A
Bad in production mode, too. You should LOG errors, but don't display them to the user.
Bob Somers
Agree with Bob Somers.
thomasrutter
I also agree with tomasrutter
alexy13
He's saying bad in development, good in production.
WishCow
+10  A: 

Setting up a system that uses an id field from $_GET to access a particular data record, then not checking group-permissions when the user just starts incrementing the id in the url. Very bad when the page displays information like SS#'s, addresses, etc.

ex: user_record.php?user_id=1

Either obfuscate the user_id in the url with some kind of salted hash, or check that the user has permission to be viewing the page. Better yet, do both.

it gets worse, i've seen not only values, but column names taken directly from the url like that.
Kris
There's been stuff on dailywtf about *entire queries* in the URL.
chaos
It also sucks when the way to delete a user or whatever from the database is via GET variable :(
AntonioCS
Or: drop the obfuscation, and do the proper way twice!
Longpoke
haha @chaos: I've seen sites that base64-encode SQL queries into a GET var and execute it.
Longpoke
+29  A: 

Programmers thinking that they are 'doing object orientation', when they are actually creating large collections of functions and using hardly any useful OOP practices.

Kieran Hall
But I use classes!!class Utility {public static function doX();public static function doY();}
WishCow
+17  A: 

Web designers thinking that because it is easy to learn PHP syntax that it must be easy to learn to code. PHP is just... a more powerful CSS, right?

They're fans of using Dreamweaver to manage their PHP code, which often means having the same code pasted at the top of every single page on the site. Includes, abstraction, DRY, generalization are all beyond their programming "knowledge."

Lucas Oman
Dreamweaver is the source of most PHP evils. It screws up the code formatting, encourages bad css names, and encourages duplicating the layout everywhere thanks to templates and by making it easy to change multiple files. Makes it hell for the rest of us.
jcoby
I agree. I love PHP, don't get me wrong, but any coding project needs to be handled properly. That said, I actually use Dreamweaver, with a couple of rules: It can't type my code for me, and it doesn't get to touch my CSS. So basically, its a CSS highlighting FTP window thing, but that's all I need.
Nicholas Flynt
oooh time to upgrade Nicholas. There's much better software available for you out there. I started learning PHP from the automatically generated code it gave me many moons ago, but thankfully realised that it wasn't a great system for development not long afterwards.
nickf
...so which do you use? :D
Beau Martínez
I'm an avid vim fan. Can't imagine using anything else.
Lucas Oman
A: 

Planning on using the WTF crap php guys call "closure"

gizmo
`use` huh? nice.
jcoby
A: 

register_globals sibling in evil extract($_REQUEST)

magic_quotes_*

DIRECTORY_SEPARATOR when opening files ('/' is usable on all systems).

require_once(SF_ROOT_DIR.DIRECTORY_SEPARATOR.'apps'.DIRECTORY_SEPARATOR.SF_APP.DIRECTORY_SEPARATOR.'config'.DIRECTORY_SEPARATOR.'config.php');

Yeah.. that's readable.

jcoby
DIRECTORY_SEPARATOR is good practice, as it is making your code more robust to future platform support. I agree it is a long constant, and in my applications I usually do a define('DS', DIRECTORY_SEPARATOR) in my bootstrap or wherever necessary.
Kieran Hall
also, symfony's front controller almost never needs to be edited, so using it as an example is unfair.
Nathan Strong
it was only an example. not bashing on sf at all.
jcoby
+2  A: 

Bad use of the magic method __call(); resulting is some ugly things like the automagically creation of the setter and getter methods via reflection.

Davide Gualano
+6  A: 

By far the worst practice I have ever seen is suggesting that eval() should ever be used under any circumstance what-so-ever. eval() is evil. Never ever use eval(). If you feel like you have no other choice, there's a 99.99% chance you've done something horribly, horribly wrong.

There are also particularly egregious examples of system function misuse (backtick operators, system(), passthru())

@jcoby, I have to disagree with you on DIRECTORY_SEPARATOR. It's a matter of personal preference. I personally prefer the constant as it is more precise. If paths are exported to logs or other displays, I know they are valid without the need for paranoid transformations and can be used by out-of-band scripts. As for readability, the example you gave could easily have been rewritten as:

// Load configuration settings.
require_once(implode(DIRECTORY_SEPARATOR, array(
    SF_ROOT_DIR,
    'apps',
    SF_APP,
    'config',
    'config.php'
));

Which is readable and arguably easier to modify than scanning through a string. I'm not suggesting that using the constant is a better practice, I'm just suggesting that it's a matter of personal preference and consistency should be followed whatever the choice may be.

eval is not evil. It is evil when you use it with data from the user
AntonioCS
+10  A: 

"Enterprise error suppression" ;-)

if (some_condition) {
   return false;
   error_log('some error message');
}
Endlessdeath
Wow, just wow!!!
AntonioCS
That'll be a pretty empty log file yeh?
alex
Behind every one of these there'll be a phone call from the boss asking if you could "fix this error that keeps coming up in the logs"
thomasrutter
+15  A: 

Take a look at the design of CakePHP. It uses classes without being object-oriented. It duplicates classes within an app layout. It defines methods that take input that doesn't match the method prototype at all. It dumps things in to $this->params, which is a more complicated version of $_REQUEST.

It takes everything that is convenient, wonderful, and fast about PHP and makes it complicated, disturbing, and slow.

Jim Puls
hehe well put. It seems these days that frameworks that over-complicate things are all the rage. It seems there's a natural tendency for people to crave something which is too complicated to understand.
thomasrutter
+12  A: 

Trying to emulate the strenghts of other languages while forgetting about the elegant simplicity that makes PHP powerful. And overengineering frameworks.

I wouldn't use word 'elegant' and PHP in the same sentence. I believe the right word is 'pragmatic'.
porneL
The elegance comes from what you do with it. Well that's what she said.
thomasrutter
+6  A: 

I've come across

if ($condition) {}
else {
    // something
}

and

if ($condition) {
    $var = 'foo';
} else {
    $var = 'foo';
}

And the following is actually code written by someone who took Hungarian Notation to the extreme. These are just snippets from a class for manipulating a DOMXML object.

function getAttributes(&$a_oNode)
{
    $l_aAttributes = $a_oNode->attributes();
    if ($l_aAttributes === NULL)
        return array();

    $l_iCount = count($l_aAttributes);
    $l_i = 0;

    for ($l_i = 0; $l_i < $l_iCount; $l_i++)
    {
        $l_aReturn[$l_aAttributes[$l_i]->name()] = $l_aAttributes[$l_i]->value();
    }

    return $l_aReturn;
}

function getText(&$a_oNode)
{
    $l_aChildren = $a_oNode->child_nodes();
    if ($l_aChildren === NULL)
        return NULL;

    $l_szReturn = "";

    $l_iCount = count($l_aChildren);
    for ($l_i = 0; $l_i < $l_iCount; $l_i++)
    {
        if ($l_aChildren[$l_i]->node_type() == XML_TEXT_NODE)
            $l_szReturn .= $l_aChildren[$l_i]->node_value();
    }

    if (strlen($l_szReturn) > 0)
        return $l_szReturn;

    return NULL;
}

Another really bad issue that I have to deal with is the complete lack of consistent indentation, and also inconsistent use of braces, such that you get code like this:

if ($condition)
    foo();

Which lends itself open to a bug because when it's written without indentation, and foo() is inline with the 'if', another developer could miss the condition and add a bar() before foo(), not realising that they've completely changed the flow of logic (it has happened...).

JamShady
This is bad code, and has nothing whatsoever to do with PHP, besides the fact that the bad code happened to be written IN PHP.
mabwi
+9  A: 

Not validating input.

mysql_query('SELECT * FROM users WHERE user = '.$_POST['user']);

Or:

$_REQUEST['anything']

How often are you not going to know what kind of request you're getting? If anyone has a legitimate use case, please share.

VirtuosiMedia
I built a REST API and at time the requests were in POST and at other times they were GET so I used $_REQUEST
Unkwntech
Try two different PHP files.
jmucchiello
This answer was practically bait for someone to jump in and volunteer a time they had done it, and then be told why they were wrong. Can someone start PHP over from scratch? If I wish hard enough can it happen?
marr75
I use $_REQUEST to get error messages and notifications and display them in my templates. That way if you need to notify the user of something you can just pass it whichever way is the easiest. I also check in $_SESSION for the same thing too, which makes it even more transparent.
tj111
No problems with that. Just wrap your $_REQUEST into objects, where input can only ever be accessed via filter functions. So mysql_query("SELECT * WHERE user = $_GET->int[id]"); Only way to enforce validation.
mario
For trivial things like sort=namedesc, sometimes it's handy to allow this either via the query string or a post variable as there might be a few actions you can take on that page. Ok so if @marr75 is right someone's going to come in now and tell me doing that was wrong. ;)
thomasrutter
+1  A: 

Yeah, found a new one! The future "\ as namespace separator". The irc discussion show how stupid the PHP core developper are. They don't just have a clue about what context sensitive operators are.

Damn, that's a shame such an horrible language gains some popularity!

gizmo
seriously can you explain what is so horrible about the the backslash namespace operator? What would have you preferred?
nickf
Well, the :: was a more decent choice (or any other commonly used separator). The main reason they did go for "\" is their incapacity to creator a correct context dependent operator. Now, you can have a string with "\n" in there that can be used as namespace, but when you try to debug using logging, you end up with a new line in you debug information. Great...
gizmo
I don't see anything horrible in it. Not perfect, but not horrible either. PHP is not that kind of academically pure language for that to be important.
RommeDeSerieux
+26  A: 

One company I worked for had written their own shopping cart; on the checkout page the purchase total was calculated and saved to a hidden form field... When you submitted your order it conducted the transaction with the price from that hidden field...

Kevin
Isn't this a fiddler example? Tell me you're kidding about this being in the wild...
marr75
OMFG!!!!! No!!!
Sepehr Lajevardi
:) That's so scary, it makes me laugh...
Irfy
can you post the url of the store?>:)
nimcap
So what if you set it to a negative number.... >;)
CrazyJugglerDrummer
+8  A: 

renaming plain .html files to .php just to look "cool"

daniels
Or the opposite, adding a php handler for .html files just to hide the fact they use php ;p
Kevin
@Kevin, what's wrong with that? Of course, the mime type has no real place in a good URI but it still beats exposing implementation details by a laaarge margin. So yes, if your server doesn't allow/support something like mod_rewrite, *do* use PHP handlers for html files instead (+ DirectoryIndex!).
Konrad Rudolph
@Konrad -- good fucking point!
Horace Loeb
@Konrad, there's nothing wrong with it besides the fact that PHP has to parse every HTML file called from the server which increases server load. Also, PHP headers usually include "X-Powered-By PHP" which defeats the whole purpose of giving HTML a PHP mimetype handler.
Kevin
+3  A: 

I recently rewrote some code written by an employee who obviously didn't know what he was doing.. I ran into a few gems like this..

$sql = "SELECT * FROM attendance_exceptions";
$found = false;

...

$appendSql = " AND YEAR(event_time) = '$_POST['year']' AND MONTH(event_time) = '$_POST['month']' AND DAY(event_time) = '$_POST['day']'";
$temp = array($sql, $appendSql);
$sql = join($temp); 
$found=true;

Creating an array, then join()ing it simply to concatenate a string? I won't get started on the query itself, nor the unsanitized $_POST data.

function verifyQuery($sql, $con) {
    if (!mysql_query($sql, $con)) {
        echo "Error occured in verifyQuery() in sqlfunctions.php <br>";
     echo "SQL sent : ";
     echo $sql;
     echo "<br>Database report: <br>";
        die('Error: ' . mysql_error());
    }
    return mysql_query($sql);
}

Executing mysql_query twice?

Sometimes I wonder if the guy was just trying to make his code look more difficult.

actually, joining arrays is faster than string concatenation in most languages, not sure about PHP, though.
I.devries
in PHP, strings are mutable, so it's not really worth the WTFery of using arrays.
nickf
judging from that code sample, i don't imagine the guy was using join() for purposes of speed.
ithcy
+8  A: 

The use of short tags <? ?> rather than <?php ?> because other languages also use the <? syntax.

Edit: As of PHP 5.3, short tags will throw an error.

VirtuosiMedia
I don't really agree with that
Joshi Spawnbrood
What don't you agree with? It can conflict with XML and not all hosts support the use of short tags. It's a deprecated practice.
VirtuosiMedia
I agree20% of the time I see code that someone is having problems with they are using <? how much work is just to type the extra 3 characters
Unkwntech
It's kind of handy when you're not using something like smarty as a template engine.<?=$var?> is a lot easier than <?php echo $var; ?>
I.devries
Agreed with Unkwntech. Those 3 bytes prevent a number of unnecessary headaches. Short tags solve a problem that doesn't exist.
Legion
@Vatos: Indeed the <?=$var?> is a lot simpler than <?php echo $var?> but I always use the <?php.. ?> because I had problems with short tags in the past and just using <?php instead of <? fixed everything
AntonioCS
Is that edit true? Short tags will throw an error in 5.3? Is this on `E_STRICT` only?
alex
Just use short tags for development, and a simple oneliner for code distribution with long tags: perl -pie 's/<?=/<?php echo /; s/<?\s/<?php /' $(find templates)
mario
+16  A: 

Reimplementing the native functions because one did not bother to check the documentation first.

Krzysztof Sikorski
This has happened to me before... why did I not know basename() existed?
alex
This is also because the naming strategy in PHP is virtually non-existant. There's no way to safely 'guess' a method, let alone their parameters. And the PHP devs are also not really interested in fixing this, I gathered.
Erik van Brakel
I'm forever checking the docs just to make sure I have the parameters in the right order. There's no standard. For search functions, sometimes haystack comes first, sometimes needle. Gah!
seanmonstar
PHP's hideous library doesn't help, but this is a problem common in all languages. Half the stories from dailywtf are snippets of someone poorly re-implementing library functions. I was working on a project once my senior year when one of my developers looked at me and asked if I could review his function to convert hex user input to ascii in C, I said nothing and we instantiated code reviews from then on.
marr75
Hey, at least php.net has the most rockin API documentation of any API out there practically. (I've yet to see a better source of documentation for other languages/frameworks)
Earlz
+2  A: 

my favourite so far is in some code I was given when they had skipped the idea of Mod_rewrite and instead used the 404 page to return "200 OK" and then process all 404 urls (which were being used as real pages) into parts of a query and build the page from that

$url_parts = explode("/",ereg_replace("(.php|.html|.htm)", "", $_SERVER['PHP_SELF']));

This is just wrong on so many levels.

Tristan Bailey
I was seriously looking at that idea on an older IIS/PHP setup a few years ago. It was a hosted system with no mod_rewrite and installing a module for it was no option. Luckily, there was PATHINFO.
Pekka
+8  A: 

I know this is an old question, but what I perceive as the worst practice hasn't been posted yet.

The worst practice in PHP is having the language's behavior change based on a settings file. Particularly the settings you can't alter at runtime. It severely affects portability.

PHP6 will be great if not just because it removes the magic quotes settings.

As a side note, PHP5 has two ini files. From memory, here are some of the differences between them:

php.ini-dist

  • magic_quotes_gpc = On
  • display_errors = On
  • arg_separator.input = "&"

php.ini-recommended

  • magic_quotes_gpc = Off
  • display_errors = Off
  • arg_separator.input = "&;"
R. Bemrose
Off the top of my head I can't remember the exact version numbers (4.1.3/4.1.4/4.1.5 something), but there exists at least one instance in which the default setting for magic quotes was changed from on to off, and then back to on again, *all in a sequential set of minor version updates*.
David
It doesn't help that PHP comes with two different inis: magic quotes gpc is off in dist and on in recommended. There are other minor differences between the two as well. I should probably add these to my answer.
R. Bemrose
Default error settings fall foul of that, too. I actually filed a bug because the default .ini file couldn't decide if it was intended for a dev or a prod environment (e.g. Notices were suppressed).
staticsan
A: 

There is a lot of things which can be done badly in PHP. However, I find it very bad when people do not use functions, classes, mix php and html, use echo for creating pages and still try to support pre php5 versions.

Niran
+5  A: 

There are a few. And they usually come together :)

  • No functions or few functions 300+ lines long. Classes? Yeah, right...
  • When code is mixed with HTML the indentation is just random.
  • "select count(*) from users where login = '{$_GET['login']}'...
  • Mixing English with other languages within code
  • Copy & Paste methodology - why bother using functions when you can copy the blob of code, tweak it a bit and it all works?

They are not necessarily php-specific, however those practices tend to show up more frequently in this language.

ya23
+3  A: 

These two are not PHP specifics, but please please please please:

  • If you are not english speaker, please do not use your language not english characters on the variable names. Variables named $apáñalo are not funny (and I've even seen wrong behaviours using them)
  • Also, do not use stupid names for variables $what_a_long_variable is not descriptive, and people's mood won't get high
dexem
Using overly short variables are bad also, such as $x, $b, $c, $dcx.
alexy13
+2  A: 

Where to begin, the use of variables that are not defined unless the if(something) is triggered, thus producing lots of errors in the error log.

The use of an array value that does not exists unless it was created prior in if(something), thus spawning more errors.

Not closing a persistent connection to MySQL for a single query in the page...

Including files that have no purpose in the script other than to say give more errors with variables that are not defined.

Running multiple queries to insert data into a table that could have been done in 1 line.

Aduljr
A: 

Templating engines. Why implement templating languages in a templating language?

This article says it better than I can.

ykaganovich
Unfortunately this debate is more like a religious war, kinda like the vi vs emacs one.
WishCow
Because a template engine limits the options, it leaves less options for 'junior programmers' to create a complete mess. Then again, I never fail to be amazed by the new and creative ways people come up with to create an even bigger mess when you limit their options.
Jacco
A: 

Hmm, what to say, most PHP projects I've ever seen are just plain horrible. I can't remember much, here goes:

// WARNING: Don't try this at home!

mysql_query("SELECT * FROM tblSomething WHERE id = " . $_GET['x'] . " AND tblSomethingElse = " . $someval ) or die(mysql_error());

(note, there is no return in this, SO wrapped it)

  • Breaking 80 columns isn't cool anymore, let's break 120! Who knew that Moore's law applies to source line length?
  • die(mysql_error()) yay! let's spam the user with something that is completely useless to him instead of a standard not found page
  • "foo" . $bar . "baz" ugly as...
  • $_GET['x'] Unsanitized, however I blame PHP for not forcing you to use parameterized queries.
  • or die() wtf, maybe for a 10-line project it's okay to die in the middle of nowhere

If you're going to do something insecure, at least make the code look nice..

mysql_query("SELECT * FROM tblSomething WHERE id = {$_GET['x']}".
            "AND tblSomethingElse = $someval") or die(mysql_error());

What seems to be an idiom in php: include($_GET['x']); This is just plain bad security-wise, and bad programming style anyways.

Writing 500 function calls with the @ error suppressor.

Global variables galore. Setting $_GET['x'] = 123, then calling a function with no parameters that uses $_GET['x'] which also calls another function with no parameters that uses $_GET['x'].

People calling sprintf where they could have just used string interpolation.. uggh.

This:

<?php 
if ($_GET['ad'] == 'ud') {
include('includes/ud.inc.php');
} else if ($_GET['ad'] == 'dd') {
include('includes/dd.inc.php');
} else if ($_GET['ad'] == 'wd') {
include('includes/wd.inc.php');
} else if ($_GET['ad'] == 'fd') {
include('includes/fd.inc.php');
} else if ($_GET['ad'] == 'cd') {
include('includes/cd.inc.php');
} else if ($_GET['ad'] == 'od') {
include('includes/od.inc.php');
} else if ($_GET['ad'] == 'ld') {
include('includes/ld.inc.php');
} else if ($_GET['ad'] == 'ldu') {
include('includes/ldu.inc.php');
} else if ($_GET['ad'] == 'ldc') {
include('includes/ldc.inc.php');
} else if ($_GET['ad'] == 'lde') {
include('includes/lde.inc.php');
} else if ($_GET['ad'] == 'pd') {
include('includes/pd.inc.php');
} else if ($_GET['ad'] == 'nd') {
include('includes/nd.inc.php');
} else if ($_GET['ad'] == 'cu') {
include('includes/cu.inc.php');
} else if ($_GET['ad'] == 'kg') {
include('includes/kg.inc.php');
} else if ($_GET['ad'] == 'st') {
include('includes/st.inc.php');
} else if ($_GET['ad'] == 'cm') {
include('includes/cm.inc.php');
} else {
include('includes/home.inc.php');
}
?>

And the winner is...

<table cellpadding="4" cellspacing="0" border="0">
<?php do { ?>
<?php if ($userDetails['name'] == 'adLinkTitle') { ?>
<tr>
<td>
<label for="wTitle">Your Website Title:</label>
</td>
<td>
<input name="wd_title" type="text" id="wTitle" class="adminInput" value="<?php echo $userDetails['value'];?>" />
</td>
</tr>
<?php } ?>
<?php if ($userDetails['name'] == 'adLinkURL') { ?>
<tr>
<td>
<label for="wAddress">Your Backlink URL:</label>
</td>
<td>
<input name="wd_url" type="text" id="wAddress" class="adminInput" value="<?php echo $userDetails['value'];?>" />
</td>
</tr>
<?php } ?>
<?php if ($userDetails['name'] == 'adLinkDescription') { ?>
<tr>
<td>
<label for="wAddress">Your Website Description:</label>
</td>
<td>
<textarea name="wd_description" id="wDescription" class="adminInput"  rows="6"><?php echo $userDetails['value'];?></textarea>
</td>
</tr>
<?php } ?>
<?php } while ($userDetails = mysql_fetch_assoc($getUserDetails)); ?>
</table>
<input type="hidden" name="wd_updated" value="yes" />
<input value="Update Your Link Details" type="submit" class="padSubmit" />
</form>
<br />
Longpoke
+1  A: 

Okay, the answers here seem pretty tame compared to my experience. I once worked on a project where the author had avoided using arrays by using eval instead. Example:

$qty = eval("\$qty_" . $product)
$cost = eval("\$cost_" . $product)

This was to access variables that should have been accessed through $POST or whatever.

Dietrich Epp
A: 

How about this (snipped from http://thedailywtf.com/ and modified a bit) :

<?php

  function get_signature($signature) {
  return $signature;
  }

  function get_date($date) {
  return $date;
  }
<?

And,

if (!false) {...
alexy13
A: 

The over usage of duck-tape, er I mean arrays. I'm horribly guilty of this though because PHPs arrays are so powerful!

things like this

myobject=array("field1" => 10, "field2" => "abc");

and yes. I do this because I still think PHPs OOP implementation is half-baked. I haven't tried using it in so long though that I'm probably wrong now though

Earlz
A: 

What about using the e flag in preg_replace_all and the like?

alex
+3  A: 

I know this might even sound too wicked to be real, but the worst piece of "software" I've ever had the chance (or disgrace) to put my hands on was a single nK line php file which, I remember, was handling the whole logic for an e-commerce site and was structured like this (including tons of echoed HTML, obviously not properly indented):

<?php

// Obviosly relying on register globals!
switch($page) {

case cart:
echo '<html><head>...</head><body>'. 
               some random application logic, including SQL, of course! .
      '</body></html>';
break;

case checkout:
echo '<html><head>...</head><body>'.
       even more random application logic, including SQL, of course! .
     '<form><input type="hidden" name="total" value="X"></body></html>';
break;

case complete:
echo '<html><head>...</head><body>'.
       and again, some random application logic, including SQL, of course! .
       '</body></html>';
break;

case error:
echo '<html><head>...</head><body>'.
      some random application logic, including SQL, of course! .
     '</body></html>';
break;

// Show the home page
default:
echo '<html><head>...</head><body>'.
     show the home page, if you really don't know what else to do.....
    '</body></html>';
break;

?>

A nightmare, believe me!!! ...and thus the worst coding practice I could think of... If I think about it now, of course it makes me smile though :-)

maraspin
A: 

One programmer in our office did this to add space:

<?php echo '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'?>

I was dumbstruck!

Thorpe Obazee
+1  A: 

Walking past one of the 'developers' desks and seeing this code.

for ($i=1; $i <= 5; $i++) { 
    echo '<br>';
}

shudders

Stoosh