ricky spires Posted February 1, 2012 Share Posted February 1, 2012 i keep seeing people use this kind of code but i cant find any document or help web pages on it. im sure there is but i done know what to call it. i would really like to learn more about it for example $lang = isset($_GET['lang']) ? (int)$_GET['lang'] : 1; or return !empty($result_array) ? array_shift($result_array) : false; Quote Link to comment https://forums.phpfreaks.com/topic/256216-where-can-i-learn-about-using-in-my-true-or-false-code/ Share on other sites More sharing options...
Maq Posted February 1, 2012 Share Posted February 1, 2012 It's called a ternary operator. Search for it here: http://php.net/manual/en/language.operators.comparison.php Quote Link to comment https://forums.phpfreaks.com/topic/256216-where-can-i-learn-about-using-in-my-true-or-false-code/#findComment-1313467 Share on other sites More sharing options...
ricky spires Posted February 1, 2012 Author Share Posted February 1, 2012 thanks this is a perfect example <?php // Example usage for: Ternary Operator $action = (empty($_POST['action'])) ? 'default' : $_POST['action']; // The above is identical to this if/else statement if (empty($_POST['action'])) { $action = 'default'; } else { $action = $_POST['action']; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/256216-where-can-i-learn-about-using-in-my-true-or-false-code/#findComment-1313472 Share on other sites More sharing options...
scootstah Posted February 1, 2012 Share Posted February 1, 2012 You don't actually need the surrounding ( )'s. This will work fine: $action = empty($_POST['action']) ? 'default' : $_POST['action']; Quote Link to comment https://forums.phpfreaks.com/topic/256216-where-can-i-learn-about-using-in-my-true-or-false-code/#findComment-1313479 Share on other sites More sharing options...
Andy-H Posted February 1, 2012 Share Posted February 1, 2012 Although you do need to wrap the whole statement in parentheses if you want to concatenate the returned value - $string = 'Testing ternary: '. (true ? 'true' : 'false'); Quote Link to comment https://forums.phpfreaks.com/topic/256216-where-can-i-learn-about-using-in-my-true-or-false-code/#findComment-1313485 Share on other sites More sharing options...
jcbones Posted February 2, 2012 Share Posted February 2, 2012 I always wrap the conditional in ()'s for readability. But, then again, most people think I am strange... Quote Link to comment https://forums.phpfreaks.com/topic/256216-where-can-i-learn-about-using-in-my-true-or-false-code/#findComment-1313555 Share on other sites More sharing options...
Pikachu2000 Posted February 2, 2012 Share Posted February 2, 2012 In most cases, I assign the value to a variable anyhow, then use the variable in the string. More readable (to me, anyhow) that way. echo "<select name=\"box\">\n"; foreach( $array as $k => $v ) { $selected = $k == $_POST['box'] ? 'selected="selected"' : ''; echo "<option value=\"$k\" $selected>$v</option>\n"; } echo "</select>\n" ; Quote Link to comment https://forums.phpfreaks.com/topic/256216-where-can-i-learn-about-using-in-my-true-or-false-code/#findComment-1313575 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.