Re: Comparison style

From: Date: Tue, 15 May 2001 10:46:20 +0000
Subject: Re: Comparison style
References: 1  Groups: php.pear.dev 
Request: Send a blank email to pear-dev+get-384@lists.php.net to get a copy of this message
yes, putting the constant on the left side of the expression keeps one's butt safe. Sterling's remark is valid for both PHP and C (and perhaps all the C-like lingos out there). Besides, I consider comparisons w/ constants on the left side more readable: if (zend_hash_init(&BG(putenv_ht), 1, NULL, (void (*)(void *)) php_putenv_destructor, 0) == FAILURE) { vs. if (FAILURE == zend_hash_init(&BG(putenv_ht), 1, NULL, (void (*)(void *)) php_putenv_destructor, 0)) { And, as always, it pays off to be consistent w/ your style. NB, take this for example: $x = 0 ; if( $x == 0 ) {
    # some code to perform if $x is zero
} else {
    # if $x is NOT zero
} now take out one of the equal signs. what happens is that $x is apparently 0, but the "else" branch is executed. debugging hell unleashed! and you can prevent this (plus other problems) if you switch the order of the operands. At 23:00 14.5. 2001 -0400, Sterling Hughes wrote:
Jon Parise wrote:
I was just perusing some of the PEAR code, and a certain condition caught my eye:
    if ("" == $content)
My habit has always been to write that sort of condition using the style:
    if ($content == "")
(leaving the unknown value on the left side of the comparison operator) Is one of these styles preferable to the other, either always or in certain instances?
It depends on your personal style. The reason for testing like: if ("" == $content) Is because, if you're careless enough to have only one '=' sign: if ("" = $content) The code will not fail silently, but rather emit and error, whereas: if ($content = "") Will fail silently, and might therefore cause debugging headaches later on. -Sterling -- PHP Development Mailing List <http://www.php.net/> To unsubscribe, e-mail: php-dev-unsubscribe@lists.php.net For additional commands, e-mail: php-dev-help@lists.php.net To contact the list administrators, e-mail: php-list-admin@lists.php.net


« previous php.pear.dev (#384) next »