![]() |
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:
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:
Thanks for any help! |
I have never tried this before, but you can do a quick test to see.
PHP Code:
|
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 |
Quote:
Quote:
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-) |
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 |
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! |
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.
|
@xenon:
What you say seems to contradict what delayedinsanity says. Quote:
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... |
I think what he's trying to say is that;
PHP Code:
-m |
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.
|
Quote:
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