|
|
|
|
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:24 PM
|
#4
|
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:27 PM
|
#5
|
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: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:40 PM
|
#7
|
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-18-2007, 11:42 PM
|
#8
|
Status: Request a custom title
Join date: Feb 2005
Location:
Expertise:
Software:
Posts: 1,078
|
Derek, your code won't even work, it will return a p**** error.
Using the braces make no difference and actually make the code more readable in my eyes. I think that getting rid of them is actually a bad idea and will result in more issues. So really, it's not beginners that are doing this.
|
|
02-18-2007, 11:43 PM
|
#9
|
Status: Senior Member
Join date: Aug 2005
Location: New York
Expertise:
Software:
Posts: 825
|
Using brackets isn't a 'wrong' way of doing it. It's arbitrary...
|
|
02-18-2007, 11:56 PM
|
#10
|
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
|
|
|
|
|
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
Thread Tools |
|
Display Modes |
Linear Mode
|
|
|