Which php method is better?

Ye' old general discussion board. Basically, for everything that isn't covered elsewhere. Come here to shoot the breeze, shoot your mouth off, or whatever suits your fancy.
This forum is not for asking programming related questions.

Moderator: General Moderators

Post Reply
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Which php method is better?

Post by jaoudestudios »

Which method is better?

Code: Select all

if ($x == 1)
{
 return true;
}
else 
{
 return false;
}
or

Code: Select all

return $x == 1;
thanks
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Which php method is better?

Post by Jonah Bron »

Um, I think that on the hardware level, "return $x==1" is slightly better, plus there is slightly less interpreting. Though, it's not so good for readability. Maybe "return ($x==1)"?

Why?
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: Which php method is better?

Post by jaoudestudios »

We're having a debate at work.

I agree with what you said, may be less interpreting but also less readable.

Thanks! :)
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Which php method is better?

Post by VladSun »

jaoudestudios wrote:We're having a debate at work.

I agree with what you said, may be less interpreting but also less readable.

Thanks! :)
I think that the "if" version is less readable ;)
I'd always use the "single-line-return" version.
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Which php method is better?

Post by pickle »

In your position, I'd have used:

Code: Select all

return ($x == 1) ? TRUE : FALSE;
Still one line, and still readable
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply