At our weekly usergroup meeting, which is taking place just right now, we again discussed our beloved topic, the Ternary Operator. I like that thing really much and I believe it makes code much more readable in a lot of cases. Anyway, I tend to stack ternary operators and I didn't even know, that the PHP manual recommends to not do that. I think this recommendation is only valid of you don't add appropriate braces, to indicate the precendences. Anyway, while having a beer, we thought about some more "funny" things you could do with it... For example, executing mutliple statements, within one section of the operator... here is the result:
echo ( ( 1 == 1 ) ? call_user_func( create_function( '$a, $b', 'return ($a * 2) + $b;' ), 10, 15 ) : 0 );
Update:
Some more funny constructs after some more beer. I simply love lambda calculus. :)
call_user_func(($func = create_function('$func, $x', 'echo $x; return ($x < 10 ? $func($func, $x+1) : $x);')), $func, 1);
If you liked this blog post or learned something, please consider using flattr to contribute back: .
Fields with bold names are mandatory.
Derick
Don't you ever dare to commit this sort of code though! :D
Link to commentRichard Thomas
Hate them with a passion, yes they are easy to understand if you don't go stack crazy but they make scanning through code harder.
Link to commentIvo Jansch
When used properly, it can give you pretty clean code. Sometimes I encounter them in the most complex situations, in which they only cause confusion.
Link to commentThomas Koch
Somebody on #php.de showed me another nice application. I'm only not sure, if it saves you any time.
Link to commentIn Python you can give a default value when assigning a the value to a variable. That's usefull for example, when a you want to give a default if a function returns null or false.
In PHP write:
$var = ($var = functionCall()) ? $var : 'no return from function!';
Toby
You should read internals and search for ifsetor operator.
Link to commentIvo Jansch
It would be cool if this worked:
Link to comment$var = $_REQUEST["somevar"] || "default";
instead of 'ifsetor'. But this is syntactically impossible.
Armand
"...I believe it makes code much more readable..."
Link to commentAre you joking? It's horrible!
Toby
Please read exactly, what I wrote. The code shown here is absolutly unreadably, that is true. But there are cases, where the ternary operator makes code more reabale that nested ifs, I think.
Link to commentArmand
I cant remember any situation in my programming practice where such a style was helpful. I consequently reject such a style in my projects. All what i can say is: If you dont use it you havent to break with such a habit, which is painful.
Link to commentIvo Jansch
I remember that in the past, there were some Code Poetry contests in the perl community for writing the most beautifully constructed code. Maybe we should do something similar sometime for PHP :)
Link to commentTobias Struckmeier
Imageine code like this:
Link to comment$myvar = $http->hasVariable( 'foobar' ) ? $http->variable( 'foobar' ) : 'defaultValue';
This is more readable as for example:
$myvar = 'defaultValue';
if( $http->hasVariable( 'foobar' ) )
{
$myvar = $http->variable( 'foobar' );
}
Because in one line you see there is an assignment only when a value exists. Otherwise you have to read the whole if statement.
But that is only one example.
Armand
what about encapsulation? some thing like this:
Link to commentclass test{
...
...
function hasVariable( $var_name ){
if( $this->http->hasVariable( $var_name ) ){
return $this->http->variable( $var_name );
}
else{
return 'defaultValue';
}
}
}
I think it's more maintainable and readable. (ok it depends on what you understand under readable)
Armand
from my assembler time. Most of the related web pages still exists with beautiful masterpieces.
Link to comment..., 65-byte Life Simulator,...
http://www.df.lth.se/~john_e/gems.html
---
code poetry .
Jan
What makes your if-statements unreadable are the insane bracketing rules you seem to force upon yourself.
Link to commentTry bracketing and indenting pragmatically instead of dogmatically.
Compare
<pre>
if ($cond)
{
$a = 1;
}
else
{
$a = 2;
};
</pre>
to
<pre>
if ($cond) $a = 1;
else $a = 2;
</pre>
Unfortunately my point is kinda hard to recognize here because your comments don't support the pre- HTML Tag