TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
Advertisement
Associates
Associates
techtuts Darkmindz
CSS Tutorials Tutorialsphere.com - Free Online Tutorials
Boston PHP SurfnLearn
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 07-15-2008, 07:59 PM   #1 (permalink)
The Contributor
 
buggabill's Avatar
 
Join Date: Jan 2008
Location: Maine, USA
Posts: 92
Thanks: 2
buggabill is on a distinguished road
Default Order of Execution

I have a question about the order of operations in a condition of an if statement.

If I have a line like this:

php Code:
if ($objftp->set_wd() && $objftp->ftp_del()) {
    .
    .
    .
}

Is it reasonably safe to assume that the two methods will be called in the order that they are listed in the condition?

--- OR ---

Would it be safer to do something like this?

php Code:
if ($objftp->set_wd()){
    if ($objftp->ftp_del()) {
        .
        .
        .
    }
}

Thanks for any help!
__________________
-- Bill
"Why is it drug addicts and computer aficionados are both called users?" -Clifford Stoll
buggabill is offline  
Reply With Quote
Old 07-15-2008, 08:02 PM   #2 (permalink)
The Acquainted
 
drewbee's Avatar
 
Join Date: May 2008
Posts: 175
Thanks: 9
drewbee is on a distinguished road
Default

I have never tried this before, but you can do a quick test to see.
PHP Code:
function funcOne()
{
echo 
"Function 1";
return 
true;
}
 
function 
funcTwo()
{
echo 
"Function 2";
return 
true;
}
 
if (
funcOne() && funcTwo())
{
 

See what that does for you.
__________________
There are No Stupid Questions. But there a LOT of Inquisitive Idiots.
Send a message via AIM to drewbee
drewbee is offline  
Reply With Quote
Old 07-15-2008, 08:48 PM   #3 (permalink)
The Gregarious
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Cana'derr
Posts: 653
Thanks: 24
delayedinsanity is on a distinguished road
Default

When it comes to conditional statements, you should always use order of precedence - if the first criteria in an AND or && operation is not met, it will not procede to the second.
-m
delayedinsanity is offline  
Reply With Quote
Old 07-16-2008, 03:24 PM   #4 (permalink)
The Contributor
 
buggabill's Avatar
 
Join Date: Jan 2008
Location: Maine, USA
Posts: 92
Thanks: 2
buggabill is on a distinguished road
Default

Quote:
Originally Posted by drewbee View Post
I have never tried this before, but you can do a quick test to see.
PHP Code:
function funcOne()
{
echo 
"Function 1";
return 
true;
}
 
function 
funcTwo()
{
echo 
"Function 2";
return 
true;
}
 
if (
funcOne() && funcTwo())
{
 

See what that does for you.
This runs funcOne then funcTwo correctly.

Quote:
Originally Posted by delayedinsanity View Post
When it comes to conditional statements, you should always use order of precedence - if the first criteria in an AND or && operation is not met, it will not procede to the second.
-m
So it does follow an order of precedence... Good. Thanks folks!

The code just looks a little better to me as I do not have to nest layers and layers of if's. The thing with this section is that it needs to go in this order as the code calling the ftp wrapper class I wrote changes to the correct directory and then deletes a file. I wanted to make sure that the directory change always happens first as the delete will fail if it does not.

Thanks again!
__________________
-- Bill
"Why is it drug addicts and computer aficionados are both called users?" -Clifford Stoll
buggabill is offline  
Reply With Quote
Old 07-16-2008, 03:49 PM   #5 (permalink)
The Acquainted
 
drewbee's Avatar
 
Join Date: May 2008
Posts: 175
Thanks: 9
drewbee is on a distinguished road
Default

Hmm, in the case that you describe, it may actually be better to do a nested situation for proper error handling.

ie

if (changeDirectory())
{
deleteFile();
}
else
{
// an error occured changing to the correct directory.
}

I am not sure what the error rate is on changing directories (if there is one at all), but this is just good coding practices :D
__________________
There are No Stupid Questions. But there a LOT of Inquisitive Idiots.
Send a message via AIM to drewbee
drewbee is offline  
Reply With Quote
Old 07-16-2008, 04:04 PM   #6 (permalink)
The Contributor
 
buggabill's Avatar
 
Join Date: Jan 2008
Location: Maine, USA
Posts: 92
Thanks: 2
buggabill is on a distinguished road
Default

The error handling is one thing that I had considered, but due to the application's user base, there is really not an issue with them needing to know what the error actually was.

It is a group of mostly computer-illiterate salesmen who, despite the presence of any kind error message, will continue to try and delete the file/report they are looking at. They will never call regarding the error. I know them well!

What I may do is nest them anyway. It is good practice, and I may get one user to call. It will be easier to find a problem if it occurs.

Thanks!
__________________
-- Bill
"Why is it drug addicts and computer aficionados are both called users?" -Clifford Stoll
buggabill is offline  
Reply With Quote
Old 07-16-2008, 09:36 PM   #7 (permalink)
The Frequenter
 
xenon's Avatar
 
Join Date: Dec 2007
Location: Bucharest, Romania
Posts: 359
Thanks: 3
xenon is on a distinguished road
Default

Don't worry about the order in which the conditions are evaluated. But if you asked, I'll answer: the order in which the methods return types is checked is the same for both cases. The difference is that in the first case, both conditions are evaluated, while in the second case (nested), if the first condition fails, the parser will never hit the second condition. How you check the conditions is entirely up to you, depending on what you need to accomplish.
__________________
I have optimistic thoughts, even though sometimes (if not always) life's a bitch.
xenon is offline  
Reply With Quote
Old 07-16-2008, 10:06 PM   #8 (permalink)
The Contributor
 
buggabill's Avatar
 
Join Date: Jan 2008
Location: Maine, USA
Posts: 92
Thanks: 2
buggabill is on a distinguished road
Default

@xenon:

What you say seems to contradict what delayedinsanity says.

Quote:
Originally Posted by delayedinsanity View Post
When it comes to conditional statements, you should always use order of precedence - if the first criteria in an AND or && operation is not met, it will not procede to the second.
-m
The thing is - I do care what order the methods are called. The script needs to change to the proper directory THEN try to delete the file. If it does not, then the delete will fail. Regardless, I nested them to provide better error handling/reporting and render the possible precedence problem irrelevant.

I guess the real question is: does PHP do short-circuit evaluation? I did a little Googling and found that it does. My original statement will work, but the error handling left a bit to be desired...
__________________
-- Bill
"Why is it drug addicts and computer aficionados are both called users?" -Clifford Stoll
buggabill is offline  
Reply With Quote
Old 07-16-2008, 10:26 PM   #9 (permalink)
The Gregarious
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Cana'derr
Posts: 653
Thanks: 24
delayedinsanity is on a distinguished road
Default

I think what he's trying to say is that;

PHP Code:

if ($condition == this && $condition == that) ....

// is the exact same thing as doing

if ($condition == this)
{
    if (
$condition == that) ..... 
The only time it would make a real difference is if you need to run an operation after the first condition is met and before the second is checked.
-m
delayedinsanity is offline  
Reply With Quote
Old 07-16-2008, 11:41 PM   #10 (permalink)
The Frequenter
 
xenon's Avatar
 
Join Date: Dec 2007
Location: Bucharest, Romania
Posts: 359
Thanks: 3
xenon is on a distinguished road
Default

Yeah, that seems pretty logical. I was using the principle 'false && false === true', but it seems that the above statement evals to false, so...sorry.
__________________
I have optimistic thoughts, even though sometimes (if not always) life's a bitch.
xenon is offline  
Reply With Quote
Old 07-17-2008, 03:51 AM   #11 (permalink)
The Contributor
 
buggabill's Avatar
 
Join Date: Jan 2008
Location: Maine, USA
Posts: 92
Thanks: 2
buggabill is on a distinguished road
Default

Quote:
Originally Posted by delayedinsanity View Post
I think what he's trying to say is that;
The only time it would make a real difference is if you need to run an operation after the first condition is met and before the second is checked.
-m
Exactly. I really do not need to run that operation in the middle, but I am going to. It does make for better error handling.

There is no need to apologize xenon... I wish I could have conveyed it better.
__________________
-- Bill
"Why is it drug addicts and computer aficionados are both called users?" -Clifford Stoll
buggabill is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 09:37 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0