fluidsharp Posted January 9, 2010 Share Posted January 9, 2010 Hello! Please tell what does it mean, and how it works: $device = is_null($coffeeMaker) ? "hands" : $coffeeMaker; What do mean "?" and ":" there? It is regular expression? <?php function makecoffee($types = array("cappuccino"), $coffeeMaker = NULL) { $device = is_null($coffeeMaker) ? "hands" : $coffeeMaker; return "Making a cup of ".join(", ", $types)." with $device.\n"; } echo makecoffee(); echo makecoffee(array("cappuccino", "lavazza"), "teapot"); ?> Link to comment https://forums.phpfreaks.com/topic/187785-code-help-from-man-example-17-8/ Share on other sites More sharing options...
p2grace Posted January 9, 2010 Share Posted January 9, 2010 That syntax is called shorthand or ternary if/else statements. Explanation: http://snippets.dzone.com/posts/show/76 Basically it's saying set $device equal to "hands" if $coffeeMaker is null, otherwise set it to the value of $coffeeMaker. Link to comment https://forums.phpfreaks.com/topic/187785-code-help-from-man-example-17-8/#findComment-991431 Share on other sites More sharing options...
trq Posted January 9, 2010 Share Posted January 9, 2010 Take a look at the ternary operator. Its basically a small if statement that is equivalent to.... if (is_null($coffeeMaker)) { $device = "hands"; } else { $device = $coffeeMaker; } Link to comment https://forums.phpfreaks.com/topic/187785-code-help-from-man-example-17-8/#findComment-991432 Share on other sites More sharing options...
fluidsharp Posted January 11, 2010 Author Share Posted January 11, 2010 Thanks! Link to comment https://forums.phpfreaks.com/topic/187785-code-help-from-man-example-17-8/#findComment-992794 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.