PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
WaldoMonster
Forum Contributor
Posts: 225 Joined: Mon Apr 19, 2004 6:19 pm
Contact:
Post
by WaldoMonster » Tue Jul 03, 2007 4:09 am
Is there any difference in comparing between these three examples?
Or is it just a matter of own preference?
I ask this because I see often !== true or !== false witch can also be written as === false or === true.
Code: Select all
if (file_exists($file) === true)
if (file_exists($file) === false)
Code: Select all
if (file_exists($file) === true)
if (file_exists($file) !== true)
Code: Select all
if (file_exists($file) !== false)
if (file_exists($file) === false)
volka
DevNet Evangelist
Posts: 8391 Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger
Post
by volka » Tue Jul 03, 2007 5:04 am
matter of own preference
superdezign
DevNet Master
Posts: 4135 Joined: Sat Jan 20, 2007 11:06 pm
Post
by superdezign » Tue Jul 03, 2007 6:37 am
I prefer to check against anything that would cause my functions to exit early first, in order to avoid nested if statements.
WaldoMonster
Forum Contributor
Posts: 225 Joined: Mon Apr 19, 2004 6:19 pm
Contact:
Post
by WaldoMonster » Tue Jul 03, 2007 8:43 am
superdezign wrote: I prefer to check against anything that would cause my functions to exit early first, in order to avoid nested if statements.
Hello superdezign,
Can you say it in other words or give an example.
Thanks
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Tue Jul 03, 2007 8:48 am
In this particular case the checks against false (not the keyword, but the intent in of the keyword in combination with the operator) would be used to short-circuit the execution of the function/method to avoid complexity of nesting if's.
Oren
DevNet Resident
Posts: 1640 Joined: Fri Apr 07, 2006 5:13 am
Location: Israel
Post
by Oren » Tue Jul 03, 2007 8:53 am
What
superdezign prefers (and me too) is doing this:
Code: Select all
my_func()
{
if (!foo($bar)) return;
if (!bar($foo)) return;
// Everything is ok, perform main tasks...
}
...over this:
Code: Select all
my_func()
{
if (foo($bar) && bar($foo))
{
// Everything is ok, perform main tasks...
}
}
WaldoMonster
Forum Contributor
Posts: 225 Joined: Mon Apr 19, 2004 6:19 pm
Contact:
Post
by WaldoMonster » Tue Jul 03, 2007 9:20 am
All thanks foo the help.