TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Advanced PHP Programming (http://www.talkphp.com/advanced-php-programming/)
-   -   Order of Execution (http://www.talkphp.com/advanced-php-programming/3128-order-execution.html)

buggabill 07-15-2008 06:59 PM

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!

drewbee 07-15-2008 07:02 PM

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.

delayedinsanity 07-15-2008 07:48 PM

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

buggabill 07-16-2008 02:24 PM

Quote:

Originally Posted by drewbee (Post 17095)
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 (Post 17096)
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! 8-)

drewbee 07-16-2008 02:49 PM

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

buggabill 07-16-2008 03:04 PM

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! :-D

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!

xenon 07-16-2008 08:36 PM

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.

buggabill 07-16-2008 09:06 PM

@xenon:

What you say seems to contradict what delayedinsanity says.

Quote:

Originally Posted by delayedinsanity (Post 17096)
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...

delayedinsanity 07-16-2008 09:26 PM

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

xenon 07-16-2008 10:41 PM

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.

buggabill 07-17-2008 02:51 AM

Quote:

Originally Posted by delayedinsanity (Post 17135)
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.


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

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