Which one is the best?

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

The first or the second?

The first
2
25%
The second
6
75%
 
Total votes: 8

User avatar
dethron
Forum Contributor
Posts: 370
Joined: Sat Apr 27, 2002 11:39 am
Location: Istanbul

Which one is the best?

Post 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)
Last edited by dethron on Sat Feb 28, 2004 3:45 pm, edited 1 time in total.
Straterra
Forum Regular
Posts: 527
Joined: Mon Nov 24, 2003 8:46 am
Location: Indianapolis, Indiana
Contact:

Re: Which one is the best?

Post 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;
}
?>
User avatar
dethron
Forum Contributor
Posts: 370
Joined: Sat Apr 27, 2002 11:39 am
Location: Istanbul

Post 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)
User avatar
Meteo
Forum Newbie
Posts: 24
Joined: Sun Jan 18, 2004 10:19 am
Contact:

Post 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
User avatar
dethron
Forum Contributor
Posts: 370
Joined: Sat Apr 27, 2002 11:39 am
Location: Istanbul

Post 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.
Straterra
Forum Regular
Posts: 527
Joined: Mon Nov 24, 2003 8:46 am
Location: Indianapolis, Indiana
Contact:

Post 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.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

why not just:

Code: Select all

return (bool)mysql_query($query);
?
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Re: Which one is the best?

Post 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.
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post 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; 

} 
?>

:)
ilovetoast
Forum Contributor
Posts: 142
Joined: Thu Jan 15, 2004 7:34 pm

Post 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
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post 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.
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

i find toast's easier to understand than yours :P

I think you overspace

:)
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post 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; 
?>
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post 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; }
?>
User avatar
redhair
Forum Contributor
Posts: 300
Joined: Fri May 30, 2003 4:36 pm
Location: 53.23N-6.57E
Contact:

Post by redhair »

How about:

Code: Select all

<?php
if ($confused)
{
      $move_on
}
else
{
      $stay
}
?>
Post Reply