Jump to content


Photo

conditional IF versus statement AND statement


  • Please log in to reply
7 replies to this topic

#1 infratl

infratl
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 14 April 2006 - 09:01 PM

hi, i came across this question recently. I often use conditional statements like:

<?php
if (opendir($newdir)) {die('Directory already exists');}
?>

could i write that condition in this form:

<?php
opendir($newdir)) and die('Directory already exists');
?>

what would be better performance-wise or what would be more appropriate coding? i've always used the if construct, but i'm curious about the and construct since the manual uses or die in several cases.

what's the difference between using the
"statement1 and statement2"
instead of
"if(statemente1) {statement2}"?

i know the above example is bad because i should really use file_exists() to test for the existence of the directory. however, if i were to do it with opendir, which one should i use, the if or the and, and why?

#2 ToonMariner

ToonMariner
  • Members
  • PipPipPip
  • Advanced Member
  • 3,342 posts
  • LocationNewcastle upon Tyne, UK

Posted 14 April 2006 - 11:20 PM

Where on earth have you seen this used in this way before???

I think you have something mixed up, the example you have given has no apparent use what so ever!!!!

I don't even think you can use and in this context either (saying that without trying it of course!!)
follow me on twitter @PHPsycho

#3 infratl

infratl
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 15 April 2006 - 02:05 AM

ok, let me change up the example a bit like this:

say i have some code that reads:

<?php
if(file_exists($filename)) die('File already exists');
?>

couldn't i simply write it as:

<?php
file_exists($filename) and die('File already exists');
?>

wouldn't these two produce the same results? is there an advantage or disadvantage of using one over the other? i know the if condition is more readable than the and. but as far as performance and correctness of code how do they compare?

#4 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 15 April 2006 - 02:20 AM

Why don't you try each of those pieces of 'code' and see what happens? You'll learn more that way.
Legend has it that reading the manual never killed anyone.
My site

#5 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 15 April 2006 - 08:09 AM

Ha, I've just learnt somthing new today. infratl I can confirm you can do the following:
file_exists($filename) and die('File already exists');

Never knew you could do that.

#6 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 15 April 2006 - 09:16 AM

1 ) Both parts of an AND expression are supposed to be boolean expressions evaluating to true or false. die() returns void.

2 ) Both parts should be evaluated to see if the whole expression gives true or false, therefore one would expect the die() to be executed in all cases.

I can only assume that this works only because of current deficiency in the PHP processor and shouldn't be relied upon, apart from being virtually unreadable to anyone having to maintain your code.

That's my $0.02 worth.
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#7 infratl

infratl
  • New Members
  • Pip
  • Newbie
  • 4 posts

Posted 15 April 2006 - 08:15 PM

[!--quoteo(post=365022:date=Apr 15 2006, 05:16 AM:name=Barand)--][div class=\'quotetop\']QUOTE(Barand @ Apr 15 2006, 05:16 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]1 ) Both parts of an AND expression are supposed to be boolean expressions evaluating to true or false. die() returns void.
[/quote]Well, wouldn't the same be true about an OR expression? Both parts are supposed to be boolean expressions. However, in the php manual, especially with the mysql functions, there are lots of examples like this one taken from [a href=\"http://us3.php.net/manual/en/function.mysql-fetch-array.php\" target=\"_blank\"]http://us3.php.net/manual/en/function.mysql-fetch-array.php[/a] :
<?php
mysql_connect("localhost", "mysql_user", "mysql_password") or
die("Could not connect: " . mysql_error());
......

[!--quoteo(post=365022:date=Apr 15 2006, 05:16 AM:name=Barand)--][div class=\'quotetop\']QUOTE(Barand @ Apr 15 2006, 05:16 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]2 ) Both parts should be evaluated to see if the whole expression gives true or false, therefore one would expect the die() to be executed in all cases.
[/quote]I don't think this is true because according to the manual AND and OR epressions are short-circuited. Meaning that, in an AND expression, if the first part is false, php won't evaluate the second part because, even if it did, the whole expression is already false. The same is true with the OR expression. If the first part is true, php won't evaluate the second part, because the whole expression is already true.

[!--quoteo(post=365022:date=Apr 15 2006, 05:16 AM:name=Barand)--][div class=\'quotetop\']QUOTE(Barand @ Apr 15 2006, 05:16 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]I can only assume that this works only because of current deficiency in the PHP processor and shouldn't be relied upon, apart from being virtually unreadable to anyone having to maintain your code.
[/quote]I don't think it's a deficiency as php was, again according to info in the manual, designed to have the short-circuit or lazy evaluation feature.

I do agree that it is pretty unreadable though. A programmer that's not used to seeing code written that way would probably take long to figure out what's going on.

#8 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 15 April 2006 - 08:39 PM

OK, point taken. I'll settle for agreeing on the readability issue :-)
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users