website stat

PHP Obnoxiousnesses

What happens if you do

PHP:
  1. $var = "hey";
  2.  
  3. die($var);

It prints "hey" and terminates execution.

What if you do

PHP:
  1. $var = 1;
  2.  
  3. die($var);

It shows nothing. Since most of the times the $var actually points to a more complex data structure like $_GET or $_POST you wonder if the variable is set. Or is null. You do thousands of checks. And then somehow you discover that for some weirdo reason die does not print int values. Why? I don't know. It's not even documented. The least it could do would be converting to String and print "1". Instead, you need to do

PHP:
  1. die(var_dump($var));

Compared to Ruby and Python, PHP is as painful as sticking two forks at each eye ball. I do hope PHP use starts decreasing dramatically (or PHP improving to the same extent).

End of rant.


9 Responses to “PHP Obnoxiousnesses”

  1. falso
    Published at May 14th, 2007 at 3:05 am

    Se fosses ler a documentacao vias que o die() é um alias pro exit()
    Que pela documentacao define que se o argumento for uma string ele mostra-a antes de sair, e se for um integer esse int é o exit code do script.

    Em vez de dizeres que nem está documentado, podias olhar pras coisas.

  2. mlopes
    Published at May 14th, 2007 at 3:23 am

    On PHP website:

    “die — Equivalent to exit()”

    It does not say it is an alias.

    “PHP >= 4.2.0 does NOT print the status if it is an integer.”

    So, another thing that gets changed from one minor revision to another. Oh, well…

  3. Carlos Jorge Andrade
    Published at May 14th, 2007 at 11:29 am

    Pera aí…

    Tu tocas em PHP de longe a longe.
    Tu *não* lês o manual.
    Tu *não* lês o ChangeLog.

    E a culpa é do PHP e o mundo tem de mudar para Ruby ?

  4. Bruno Pedro
    Published at May 14th, 2007 at 12:21 pm

    You should take a look at the manual *before* you stick two forks at each eye ball, not after.

    Quoting the manual: “die — Equivalent to exit()” (http://www.php.net/manual/en/function.die.php).

    Let’s now take a look at the exit() help page (http://www.php.net/manual/en/function.exit.php). It seems that exit() has two behaviors:

    1- If status is a string, this function prints the status, if set as a string just before exiting.

    2- If status is an integer, that value will also be used as the exit status. Exit statuses should be in the range 0 to 254, the exit status 255 is reserved by PHP and shall not be used. The status 0 is used to terminate the program successfully.

    Also, there is a note stating that “PHP >= 4.2.0 does NOT print the status if it is an integer.”

    Anyway, this is the *correct* behavior, according to what everybody is used to. Do a “man exit(2)” and see what I mean.

    So, the next time you start making stupid comparisons, RTFM and get your act together.

  5. mlopes
    Published at May 14th, 2007 at 5:03 pm

    You’re kind of right, I must confess.

    I’m used to a world where the language guesses what I’m doing (duck typing). When I fallback to PHP is like if I returned to the stone age. I’m not expecting little things to be changed from PHP 4.1.9 to 4.2.0.

    I mean, I used die() thousands of times and then, suddenly, is reporting a different behavior. You see, when you’re used to smell roses, everything else smells like sh**.

    I guess I should definitely lower my standards each time I use PHP again and realize I should read everything from ChangeLogs to duplicate methods (die/exit) as Carlos suggested.

    So, afterall, it’s Ruby fault! It raises the bar too high!

  6. Bruno Pedro
    Published at May 14th, 2007 at 5:42 pm

    You say:
    “I’m used to a world where the language guesses what I’m doing (duck typing).”

    I say:
    Duck typing has nothing to do with this specific example. We’re talking about aborting a script.

    You say:
    “I’m not expecting little things to be changed from PHP 4.1.9 to 4.2.0.”

    I say:
    PHP 4.1.9 was never released. It jumped from 4.1.2 directly to 4.2.0. Again, RTFM before expressing your opinion.

    You say:
    “(…) I used die() thousands of times and then, suddenly, is reporting a different behavior.”

    I say:
    PHP 4.2.0 was released in April 2002, more than 5 years ago! PHP 4.0.0 was released in July 1999, 3 years before the change to die() was made. die() only appeared in PHP after version 4.0.0.
    “suddenly” is not a good way to represent the timespans we are talking about.

    You say:
    “I guess I should definitely lower my standards each time I use PHP again and realize I should read everything from ChangeLogs to duplicate methods (die/exit) as Carlos suggested.”

    I say:
    You shouldn’t lower your standards, on the contrary. You should be well informed before making a decision and don’t jump into false conclusions without reading every piece of available information.

    You say:
    “So, afterall, it’s Ruby fault! It raises the bar too high!”

    I say:
    Speaking of Ruby, take a look at this little gem (sic) I found: http://www.ruby-forum.com/topic/106384

    Quoting the original:
    “exit from within block returns wrong value”

    “1.8.5
    ruby -e ‘at_exit { exit(1) }; echo $? #=> 1

    1.8.6
    ruby -e ‘at_exit { exit(1) }; echo $? #=> 0″

    What? A bug at Ruby’s *core*?
    What? The return value was changed from one *patch* (not a minor revision) to the next?

  7. mlopes
    Published at May 14th, 2007 at 5:54 pm

    Hey, Bruno Pedro, using a well known Portuguese expression, “hold down the horses”.

    I like doing rants. And rants should always be taken with a grain of salt. If I were to follow strict execution patterns it would be a scientific experiment and not a rant. A rant has always a little bit of personal opinion. Mine is clearly that PHP is inferior to Ruby or Python. It’s obviously an arguable point and it’s not always true — nor I believe it to be true for that matter :-). Each language has its perks. It’s just a way of ranting a little bit.

    Now, I did this rant without checking all the sources. True. But it even wasn’t my intention to do so. The point I was trying to make is that since I started coding in Ruby and Python something like this never happened to me. Perhaps Ruby/Python fits my coding behavior, perhaps PHP does not.

    To end this foolishness, I’ll refrain: take this with a grain of salt. Don’t get upset, I’m just fooling (aka ranting) around.

    Gosh, I’ll start putting a disclaimer at the end of each post so people don’t get all upset.

    P.S. - You are obviously right (and everyone else) about what you pointed out. I should have checked the documentation or the changelogs. But that would take the fun out of the rant which I did enjoy writing ;-)

  8. Bruno Pedro
    Published at May 14th, 2007 at 5:59 pm

    I’m not upset.

    I also enjoy a nice argument ;) That’s all.

    Happy ranting.

  9. mlopes
    Published at May 14th, 2007 at 6:06 pm

    Yeah sure, of course, but don’t expect an argument out of this rant. It’s too biased ;-)