Last active
December 9, 2020 19:27
-
-
Save LGitHub-sprout/414e048c0d96cacdfc6f2bc65ce3762d to your computer and use it in GitHub Desktop.
Non-looping conditionals: ternary ?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* O'Reilly p.82 Expressions and Control Flow | |
* evaluates expression and | |
* executes two stmts: one when expression is T and one when F | |
* | |
* 1st stmt: when expression is true | |
* 2nd stmt: when expression if false | |
*/ | |
$fuel = 1.00000000000001; | |
echo $fuel <= 1 ? "<p>Better stop for gas</p>" : "<p>Keep driving</p>"; | |
/** | |
* Concise way to keep track of the largest value as a program progresses. | |
* Or testing whether var is set before passing to function. | |
* Preferable syntax for short comparisons. | |
*/ | |
$set = null; | |
$not_set = '<p>Nope... not set :( </p>'; | |
echo $set = isset( $set ) ? $set : $not_set; ?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment