A community in which webmasters can ask for help with topics such as PHP coding , MySQL , IT jobs, web design, IT security.
Current location:homephp forumphp talk in 2008 yearPHP conditionals, brackets needed? - page 1
User InfoPosts
PHP conditionals, brackets needed?#1
I was just browsing a forum and someone asked about a PHP file they had found on the web. It has several spots like this in the code:

if ($REMOTE_ADDR == "") $ip = "no ip";
else $ip = getHostByAddr($REMOTE_ADDR);


I have always thought brackets are needed to enclose what you want to do if the condition is true. Is there some other alternative, such as if it is on the same line you don't?

There is also another line like this:
if ($action != ""):
mail("$adminaddress","Visitor Comment from YOUR SITE",


My instinct is to say this wouldn't work, but I also don't know if it is an outdated PHP file and it used to work?

posted date: 2008-12-19 07:22:00


Re: PHP conditionals, brackets needed?#2
I had made out the solution of this problem. click to view my topic...

hope that hepls.

posted date: 2008-12-19 07:22:01


Re: PHP conditionals, brackets needed?#3
you can do if else statements like this:<?phpif ($something) { echo 'one conditional line of code'; echo 'another conditional line of code';}if ($something) echo 'one conditional line of code';if ($something)echo 'one conditional line of code';echo 'a NON-conditional line of code'; // this line gets executed regardless of the value of $something?>and then you can also write if - else in an alternate syntax:<?phpif ($something): echo 'one conditional line of code'; echo 'another conditional line of code';elseif: echo 'one conditional line of code'; echo 'another conditional line of code';else: echo 'one conditional line of code'; echo 'another conditional line of code';endif;?>with the alternate syntax you can also fall out of parsing mode like this:<?phpif ($something):?>one conditional line of code<br />another conditional line of code<?phpelse: echo "it's value was: $value<br />\n";?>another conditional line of code<?phpendif;?>But this gets really messy really fast and I won't recommend it's use (except maybe for template-logic).and to make it complete:<?php$result = $something ? 'something was true' : 'something was false';echo $result;?>equals<?phpif ($something) { $result = 'something was true';} else { $result = 'something was false';}echo $result;?>

posted date: 2008-12-19 07:27:00


Re: PHP conditionals, brackets needed?#4
Braces (not brackets) are optional in PHP, as in most C-like syntax. Maybe you're thinking of Perl; they're required there, for that form of if syntax.The colon thing is an alternate control structure form that PHP supports. I hate it, but some people (particularly template system designers, apparently) love it.

posted date: 2008-12-19 07:28:00


Re: PHP conditionals, brackets needed?#5
To go into a little more detail, the reason that the braces are optional is that the syntax looks like:if(CONDITION) BLOCK[elseif(CONDITION) BLOCK][else BLOCK]BLOCK can be a single statement:foo();or it can be a brace-enclosed group of statements:{ foo(); bar();}

posted date: 2008-12-19 07:31:00


Re: PHP conditionals, brackets needed?#6
In my opinionif ($REMOTE_ADDR == "") $ip = "no ip"; else $ip = getHostByAddr($REMOTE_ADDR);is valid, but much harder to read than:if ($REMOTE_ADDR == "") { $ip = "no ip"; } else { $ip = getHostByAddr($REMOTE_ADDR);}

posted date: 2008-12-19 07:32:00


Re: PHP conditionals, brackets needed?#7
brackets are needed to enclose what you want to do if the condition is trueI can't think of any language that requires this

posted date: 2008-12-19 07:35:00


Re: PHP conditionals, brackets needed?#8
Thanks, it must have been that I learned Perl first and it must have been the stricter side of me thinking it was wrong.

posted date: 2008-12-19 07:36:00


Re: PHP conditionals, brackets needed?#9
Yes, excluding the braces is allowed, although many times I have heard 2 reasons for not using that syntax:It's harder to read. Less obvious to another programmer.If you ever wany to add something inside the if, then you need to add the braces which is harder after then when you're first coding since most editors will add the closing brace for you.Also, yes, the colon syntax is valid. The alternatives can be found here: http://php.net/manual/en/control-structures.alternative-syntax.php

posted date: 2008-12-19 07:37:00


Re: PHP conditionals, brackets needed?#10
If you last example, you don't need the extra variable: echo $something ? 'something was true' : 'something was false';

posted date: 2008-12-19 07:40:00


Re: PHP conditionals, brackets needed?#11
I know it works, but I think I read somewhere that it was a non-documented result (and thus could change in future version). Not sure though.

posted date: 2008-12-19 07:46:00


Re: PHP conditionals, brackets needed?#12
--- 1. It's harder to read. Less obvious to another programmer. That's controversial. Many programmers like to "absorb" more code with less vertical scrolling (I'm not saying that you have to write several ;;; instructions on single line) This way you can see better the "bigger picture". But I agree, it's always better for debug mode.

posted date: 2012-12-22 04:27:00


select page: « 1 2 »
Copyright ©2008-2017 www.momige.com, all rights reserved.