Page 1 of 2
Which one is the best?
Posted: Sat Feb 28, 2004 3:19 pm
by dethron
Hi everyone(GURUS);
Code: Select all
<?php
if(mysql_query($resultSQL))
return true;
return false;
?>
Code: Select all
<?php
if(mysql_query($resultSQL))
return true;
else
return false;
?>
These are same considering the job, but different considering the concept(i mean machine language).
Which one is the best? There should be a difference between them.
What is your opinion?
Thnx, Good Luck.
P.S : I always prefer the first (with an inborn instict...)
P.S : (I edited the post to add a poll)
Re: Which one is the best?
Posted: Sat Feb 28, 2004 3:22 pm
by Straterra
dethron wrote:Hi everyone(GURUS);
Code: Select all
<?php
if(mysql_query($resultSQL))
return true;
return false;
?>
Code: Select all
<?php
if(mysql_query($resultSQL))
return true;
else
return false;
?>
These are same considering the job, but different considering the concept(i mean machine language).
Which one is the best? There should be a difference between them.
What is your opinion?
Thnx, Good Luck.
P.S : I always prefer the first (with an inborn instict...)
I would use
Code: Select all
<?php
if(mysql_query($resultSQL)){
return true;
} else {
return false;
}
?>
Posted: Sat Feb 28, 2004 3:25 pm
by dethron
Is there any point?
Why do you prefer the second?
But I want to add that there is no logical reason for why i am using the first.
I just wonder why I'm using the first?
Thnx for reply..(Straterra)
Posted: Sat Feb 28, 2004 3:32 pm
by Meteo
you're asking opinions, so i'll be glad to share mine.
i would prefer using the else keyword; it makes it enough straightforward. the first choice might be a little hard to be read by others because it's not really that direct, I think anyway.
for a simple if-statement like that, there's actually a way to make it shorter, even another way after that, as well. actually there's another way, and other programmers may think of others.
my personal favorite for certain things...
Code: Select all
(mysql_query($query)) ? $return = true : $return = false;
if you were planning on ending the script later by testing the value, which should be set to a variable...
Code: Select all
mysql_query($query) or die(mysql_error());
is always popular.
and in setting it to a variable, this would be shorter...
Code: Select all
$return = mysql_query($query);
// i've never done it the above way, but i've seen it in examples
// it would be the same output as...
if (mysql_query($query)) $return = true;
else $return = false;
enough of my babbling
Posted: Sat Feb 28, 2004 3:38 pm
by dethron
Good point and thanx for your opinion
I got some unwanted results with (sta1 ? res1 : res2) stuff.
And personelly, I don't want to use them. But it seems nice, too.
I think (considering efficiency) the second requires a little bit more buffer, but i m not sure of course
Anyway, thnx for replying.
Posted: Sat Feb 28, 2004 3:40 pm
by Straterra
The reason I prefer the second is because that is how I thought my self to write PHP, in that format..so, for me, it's easier.
Posted: Sat Feb 28, 2004 7:23 pm
by Weirdan
Re: Which one is the best?
Posted: Sat Feb 28, 2004 7:39 pm
by tim
Straterra wrote:dethron wrote:Hi everyone(GURUS);
Code: Select all
<?php
if(mysql_query($resultSQL))
return true;
return false;
?>
Code: Select all
<?php
if(mysql_query($resultSQL))
return true;
else
return false;
?>
These are same considering the job, but different considering the concept(i mean machine language).
Which one is the best? There should be a difference between them.
What is your opinion?
Thnx, Good Luck.
P.S : I always prefer the first (with an inborn instict...)
I would use
Code: Select all
<?php
if(mysql_query($resultSQL)){
return true;
} else {
return false;
}
?>
haha funny, this is exactly what my head coded when I seen it... same way I would do it.
Posted: Sat Feb 28, 2004 8:54 pm
by m3mn0n
It's a tie, they both lose.
Personally though, I would use:
Code: Select all
<?php
if ( mysql_query ( $resultSQL ) )
{
return true;
} else {
return false;
}
?>

Posted: Sat Feb 28, 2004 9:03 pm
by ilovetoast
I also dislike both.
I'd use this if I had to write this block as is:
Code: Select all
if (mysql_query($resultSQL)) {
return TRUE;
} else {
return FALSE;
}
Same thing as Sami, tim and Straterra, just adjusted to my personla coding style guides as it were.
Why am I answering this? There is no speed difference in any of the options AFAIK. Is this a coding style question or are you thinking there is a speed difference?
peace
Posted: Sat Feb 28, 2004 9:05 pm
by m3mn0n
Yup, it's all about style.
I like my code nice and spaced so things are much easier to comprehend at first glance.
Posted: Sat Feb 28, 2004 10:25 pm
by d3ad1ysp0rk
i find toast's easier to understand than yours
I think you overspace

Posted: Sun Feb 29, 2004 5:34 pm
by m3mn0n
Bah, it's all about what you're use to. And I use to do the structure that way but lately I've been into the excessive spaces for clarity. It helps especially when I do late night coding.
At 4 AM I would take...
Code: Select all
<?php
// 15 lines
if ( mysql_query ( $resultSQL ) )
{
return true;
} else {
return false;
}
if ( mysql_query ( $resultSQL ) )
{
return true;
?>
...over...
Code: Select all
<?php
// 15 lines
if (mysql_query($resultSQL)) {
return TRUE;
} else {
return FALSE;
}
if (mysql_query($resultSQL)) {
return TRUE;
} else {
return FALSE;
}
if (mysql_query($resultSQL)) {
return TRUE;
} else {
return FALSE;
?>
Posted: Sun Feb 29, 2004 5:48 pm
by d3ad1ysp0rk
mine would be
Code: Select all
<?php
//8 lines
if (mysql_query($resultSQL)) { return TRUE; }
else { return FALSE; }
if (mysql_query($resultSQL)) { return TRUE; }
else { return FALSE; }
if (mysql_query($resultSQL)) { return TRUE; }
else { return FALSE; }
?>
Posted: Sun Feb 29, 2004 7:19 pm
by redhair
How about:
Code: Select all
<?php
if ($confused)
{
$move_on
}
else
{
$stay
}
?>