Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

Question about control structures

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

Question about control structures

Postby storm » Sat May 10, 2003 4:52 pm

I've seen alot of code where someone use an if statement like:

Code: Select all
 $var = '';
if ($var) {
   // do something
}


Does it cause any performance degradation to use control structures in a manner that forces them to convert a variable's value to a boolean?
storm
New php-forum User
New php-forum User
 
Posts: 6
Joined: Thu Apr 24, 2003 5:18 pm

Postby Skeletor » Mon May 12, 2003 2:47 pm

It is actually faster because it does not have to test the return expression.
Skeletor
New php-forum User
New php-forum User
 
Posts: 147
Joined: Tue Nov 19, 2002 7:47 am
Location: US, Ohio

Postby storm » Mon May 19, 2003 7:28 pm

Ah, then so ideally, the following is the only way to code:

Code: Select all
$var = 1;

if ($var == 1) {
   // do something
}


Instead of this:

Code: Select all
$var = 1;

if ($var) {
   // do something
}



I knew this. I guess the answer should have been obvious.

But how about this...

Code: Select all
function iif($condition,$truereturn,$falsereturn) {
   if ($condition == false) {
      return $falsereturn;
   } else {
      return $truereturn;
   }
}


...instead of alternate syntax for control structures?
storm
New php-forum User
New php-forum User
 
Posts: 6
Joined: Thu Apr 24, 2003 5:18 pm


Return to PHP coding => General

Who is online

Users browsing this forum: Bing [Bot], TurnitinBot [Bot] and 2 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.