|
|
|
|
Thread title: do NOT use {} for single if-else statements.. |
|
|
|
|
|
Thread tools
Search this thread
Display Modes
|
|
02-18-2007, 10:53 PM
|
#1
|
Status: Junior Member
Join date: Feb 2005
Location:
Expertise:
Software:
Posts: 45
|
do NOT use {} for single if-else statements..
hey mates, just browsed a couple of threads in this section and I got impression that most of the people are using wrong code/technique while using if or else statements..
for example if you are using "if" statement
if($x == 10) {
echo "it is 10";
} else {
echo"it is not 10";
}
now thats not a good approach since you have only one function being used in "if" and also in "else", so there is no need of using those brackets, you can use it someting like this..
if($x == 10)
echo "it is 10";
else
echo"it is not 10";
OR use ternary operator, e.g.
$myVar = condition ? if_condition_true : if_condition_false
thats just a note for beginners, since I found a couple of threads and saw their code, I though since they read these forums, it would be a good advice for beginners...
cheers
|
|
02-18-2007, 10:55 PM
|
#2
|
Status: Request a custom title
Join date: Dec 2005
Location: Colchester, UK
Expertise:
Software:
Posts: 4,625
|
I'm not much of a PHP programmer, so this is probably a stupid question.
But, how would php know when to stop processing the 'else' part of the statement? |:
|
|
02-18-2007, 10:57 PM
|
#3
|
Status: Junior Member
Join date: May 2005
Location:
Expertise:
Software:
Posts: 44
|
Originally Posted by Indy
I'm not much of a PHP programmer, so this is probably a stupid question.
But, how would php know when to stop processing the 'else' part of the statement? |:
|
when theres a ";"...
thanks for the tip, I'm gonna do it like this in the futute, didn't know it also worked like this
|
|
02-18-2007, 11:27 PM
|
#4
|
Status: Request a custom title
Join date: Dec 2005
Location: Colchester, UK
Expertise:
Software:
Posts: 4,625
|
Originally Posted by noodles
when theres a ";"...
thanks for the tip, I'm gonna do it like this in the futute, didn't know it also worked like this
|
Ah yes. I misread the first post. x]
|
|
02-18-2007, 11:24 PM
|
#5
|
Status: Junior Member
Join date: Feb 2005
Location:
Expertise:
Software:
Posts: 45
|
Originally Posted by Indy
I'm not much of a PHP programmer, so this is probably a stupid question.
But, how would php know when to stop processing the 'else' part of the statement? |:
|
if there are no {} curly brackets after else, that means only the next first statement will be considered as a part of else.
cheers
|
|
02-18-2007, 11:28 PM
|
#6
|
Status: design rockstar
Join date: Jan 2005
Location: guelph, ontario
Expertise:
Software:
Posts: 2,246
|
for single statements, you're better off using the ? operator anyways.
PHP Code:
<?= $==10 ? 'it's 10' : 'it's not 10'; ?>
|
|
02-18-2007, 11:56 PM
|
#7
|
Status: We're all mad here
Join date: Aug 2005
Location: Missouri
Expertise: programming
Software: Notepad
Posts: 1,606
|
Originally Posted by derek lapp
for single statements, you're better off using the ? operator anyways.
PHP Code:
<?= $==10 ? 'it's 10' : 'it's not 10'; ?>
|
Thanks for that method, I like it without the if/else and brackets
|
|
02-21-2007, 01:36 PM
|
#8
|
Status: I'm new around here
Join date: Feb 2007
Location:
Expertise:
Software:
Posts: 1
|
I was just browsing and had to comment.
It doesn't really matter.
It may speed the script up by a few mili seconds, but that's it.
It's better to use { }, it's clearer, and better for nested statements.
Using the alternative Control Structure, ? : would be better than no { }
Adrian
|
|
02-18-2007, 11:40 PM
|
#9
|
Status: Member
Join date: Sep 2005
Location: Tucson, Arizona
Expertise:
Software:
Posts: 241
|
i dont know php... but c++ works in the same way...
but personally i always use {}, you never know when you will have to add extra lines of code and in general it just looks better...
|
|
02-28-2007, 08:59 PM
|
#10
|
Status: Member
Join date: Feb 2007
Location:
Expertise:
Software:
Posts: 137
|
Originally Posted by Dizzee
i dont know php... but c++ works in the same way...
but personally i always use {}, you never know when you will have to add extra lines of code and in general it just looks better...
|
I do the same, im always adding "print 'did you get here yet?';" to my if's for debugging
|
|
|
|
|
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
|