Re: [PHP] Control Structure Syntax Question
$x ? xxx : xxx But it makes your code less readable IMHO and offers no tangible benefit whatsoever. It offers the tangible benefit of evaluating to something: return (isset($foo) ? $foo : $bar); $query = select * from foo .(isset($bar) ? where bar = ${bar} : ''); $foo = ((count($bar) 1) ? 'bars' : 'bar); - michal migurski- contact info and pgp key: sf/cahttp://mike.teczno.com/contact.html -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] Control Structure Syntax Question
Someone had posted a tip for using an abbreviated form of an if.. else structure... It looked something like: $x : action1 : action2; I'm trying to shorten having to do the following: if ($x) { action1; } else { action 2; } can someone please post the syntax if they know it? I am reading the fine documentation but can't find it... many thanks Ahbaid -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Control Structure Syntax Question
$x ? action1 : action2; structure... It looked something like: $x : action1 : action2; I'm trying to shorten having to do the following: if ($x) { action1; } else { action 2; } can someone please post the syntax if they know it? I am reading the fine documentation but can't find it... many thanks Ahbaid -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Control Structure Syntax Question
Hello Ahbaid, Tuesday, March 9, 2004, 11:42:21 PM, you wrote: AG Someone had posted a tip for using an abbreviated form of an if.. else AG structure... AG It looked something like: AG $x : action1 : action2; $x ? xxx : xxx But it makes your code less readable IMHO and offers no tangible benefit whatsoever. -- Best regards, Richard Davey http://www.phpcommunity.org/wiki/296.html -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Control Structure Syntax Question
Yeah, but if my code is less readable, my job security goes up ;) (Kidding) Thanks for all your help folks, that's what I needed. regards Ahbaid Richard Davey wrote: Hello Ahbaid, Tuesday, March 9, 2004, 11:42:21 PM, you wrote: AG Someone had posted a tip for using an abbreviated form of an if.. else AG structure... AG It looked something like: AG $x : action1 : action2; $x ? xxx : xxx But it makes your code less readable IMHO and offers no tangible benefit whatsoever. -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Control Structure Syntax Question
Ahbaid Gaffoor wrote: Yeah, but if my code is less readable, my job security goes up ;) (Kidding) Thanks for all your help folks, that's what I needed. regards Ahbaid Richard Davey wrote: Hello Ahbaid, Tuesday, March 9, 2004, 11:42:21 PM, you wrote: AG Someone had posted a tip for using an abbreviated form of an if.. else AG structure... AG It looked something like: AG $x : action1 : action2; $x ? xxx : xxx But it makes your code less readable IMHO and offers no tangible benefit whatsoever. Before this thread dies, I'd like to clarify something. This syntax is mostly for inline ifs as what it does is return a value. For example: $apples = 2; echo 'There are '.$apples.' apple'.($apples == 1 ? '' : 's').'.'; In this case, it returns a string which is then echoed. This *could* be used in place of a normal if, but there is probably an added overhead as the value of the last statement is returned. Also, you *must* have an else when you use this syntax (even if it does nothing), it cannot be used for a simple if. -- paperCrane Justin Patrin -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Control Structure Syntax Question
On 9 Mar 2004 Richard Davey wrote: $x ? xxx : xxx But it makes your code less readable IMHO and offers no tangible benefit whatsoever. Ah, to each his/her own I guess ... I actually find: $value = ($condition ? $val1 : $val2); easier to read than: if ($condition) $value = $val1; else $value = $val2; On the other hand for this: $value = ($var1 ($var2 == 4) (($var3 == $var7) || ($var9 == hello))) ? htmlspecialchars(strstr($string1, $string2) . test) : NULL; I would prefer to use: if ($var1 ($var2 == 4) (($var3 == $var7) || ($var9 == hello))) $value = htmlspecialchars(strstr($string1, $string2) . test); else $value = NULL; In other words ... IMO you can use the ?: operator to be concise (which increases readability) or to be cryptic (which reduces it). -- Tom -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php