Question about control structures

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

storm
New php-forum User
New php-forum User
Posts: 6
Joined: Thu Apr 24, 2003 5:18 pm

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?

User avatar
Skeletor
New php-forum User
New php-forum User
Posts: 147
Joined: Tue Nov 19, 2002 7:47 am
Location: US, Ohio
Contact:

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

It is actually faster because it does not have to test the return expression.

storm
New php-forum User
New php-forum User
Posts: 6
Joined: Thu Apr 24, 2003 5:18 pm

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?


Return to “PHP coding => General”

Who is online

Users browsing this forum: Google [Bot] and 1 guest