Today's Posts Follow Us On Twitter! TFL Members on Twitter  
Forum search: Advanced Search  
Navigation
Marketplace
  Members Login:
Lost password?
  Forum Statistics:
Forum Members: 24,254
Total Threads: 80,792
Total Posts: 566,471
There are 1202 users currently browsing (tf).
 
  Our Partners:
 
  TalkFreelance     Design and Development     Programming     PHP and MySQL :

do NOT use {} for single if-else statements..

Thread title: do NOT use {} for single if-else statements..
Closed Thread  
Page 1 of 3 1 2 3 >
    Thread tools Search this thread Display Modes  
02-18-2007, 10:53 PM
#1
Ali is offline Ali
Status: Junior Member
Join date: Feb 2005
Location:
Expertise:
Software:
 
Posts: 45
iTrader: 0 / 0%
 

Ali is on a distinguished road

  Old  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
Garyrae is offline Garyrae
Status: Request a custom title
Join date: Dec 2005
Location: Colchester, UK
Expertise:
Software:
 
Posts: 4,625
iTrader: 16 / 100%
 

Garyrae is on a distinguished road

Send a message via MSN to Garyrae Send a message via Skype™ to Garyrae

  Old

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
noodles is offline noodles
Status: Junior Member
Join date: May 2005
Location:
Expertise:
Software:
 
Posts: 44
iTrader: 0 / 0%
 

noodles is on a distinguished road

  Old

Originally Posted by Indy View Post
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
Ali is offline Ali
Status: Junior Member
Join date: Feb 2005
Location:
Expertise:
Software:
 
Posts: 45
iTrader: 0 / 0%
 

Ali is on a distinguished road

  Old

Originally Posted by Indy View Post
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
Garyrae is offline Garyrae
Status: Request a custom title
Join date: Dec 2005
Location: Colchester, UK
Expertise:
Software:
 
Posts: 4,625
iTrader: 16 / 100%
 

Garyrae is on a distinguished road

Send a message via MSN to Garyrae Send a message via Skype™ to Garyrae

  Old

Originally Posted by noodles View Post
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
derek lapp is offline derek lapp
Status: design rockstar
Join date: Jan 2005
Location: guelph, ontario
Expertise:
Software:
 
Posts: 2,246
iTrader: 0 / 0%
 

derek lapp is on a distinguished road

  Old

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
Dizzee is offline Dizzee
Status: Member
Join date: Sep 2005
Location: Tucson, Arizona
Expertise:
Software:
 
Posts: 241
iTrader: 0 / 0%
 

Dizzee is on a distinguished road

Send a message via AIM to Dizzee

  Old

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
CreativeLogic is offline CreativeLogic
CreativeLogic's Avatar
Status: Request a custom title
Join date: Feb 2005
Location:
Expertise:
Software:
 
Posts: 1,078
iTrader: 6 / 100%
 

CreativeLogic is on a distinguished road

Send a message via MSN to CreativeLogic

  Old

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
MaxS is offline MaxS
Status: Senior Member
Join date: Aug 2005
Location: New York
Expertise:
Software:
 
Posts: 825
iTrader: 1 / 100%
 

MaxS is on a distinguished road

  Old

Using brackets isn't a 'wrong' way of doing it. It's arbitrary...

02-18-2007, 11:56 PM
#10
Impluo is offline Impluo
Status: We're all mad here
Join date: Aug 2005
Location: Missouri
Expertise: programming
Software: Notepad
 
Posts: 1,606
iTrader: 0 / 0%
 

Impluo is on a distinguished road

  Old

Originally Posted by derek lapp View Post
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

Closed Thread  
Page 1 of 3 1 2 3 >


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 

  Posting Rules  
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump:
 
  Contains New Posts Forum Contains New Posts   Contains No New Posts Forum Contains No New Posts   A Closed Forum Forum is Closed