ricky spires Posted March 7, 2012 Share Posted March 7, 2012 hello i keep seeing Ternary Operators like this $action = (empty($_POST['action'])) ? 'default' : $_POST['action']; but how would i turn this if statement in to a Ternary Operator ? if($a == $b){ echo 'hello'; }else{ //do nothing } thanks Quote Link to comment https://forums.phpfreaks.com/topic/258488-how-would-i-turn-this-if-statement-in-to-a-ternary-operator/ Share on other sites More sharing options...
Pikachu2000 Posted March 7, 2012 Share Posted March 7, 2012 echo $a == $b ? 'hello' : ''; Quote Link to comment https://forums.phpfreaks.com/topic/258488-how-would-i-turn-this-if-statement-in-to-a-ternary-operator/#findComment-1324997 Share on other sites More sharing options...
ricky spires Posted March 7, 2012 Author Share Posted March 7, 2012 thanks Quote Link to comment https://forums.phpfreaks.com/topic/258488-how-would-i-turn-this-if-statement-in-to-a-ternary-operator/#findComment-1324998 Share on other sites More sharing options...
ricky spires Posted March 13, 2012 Author Share Posted March 13, 2012 hello i have another question. how would i put php in to it? say i have this $headText = PageContent::find_text($headText); foreach ($headText as $headTexts){ $header = $headTexts->title; echo $navPnt == 0 ? '<h3 class="menuheader expandable">'.$header.'</h3>' : ''; } now, what would i do if i want to move this line inside it $header = $headTexts->title; this is what i tried but i get an error $headText = PageContent::find_text($headText); foreach ($headText as $headTexts){ echo $navPnt == 0 ? $header = $headTexts->title; '<h3 class="menuheader expandable">'.$header.'</h3>' : ''; } Quote Link to comment https://forums.phpfreaks.com/topic/258488-how-would-i-turn-this-if-statement-in-to-a-ternary-operator/#findComment-1327053 Share on other sites More sharing options...
Mahngiel Posted March 14, 2012 Share Posted March 14, 2012 Ternary statements work like this: ( condition ) ? if true, do this : else do this; $headText = PageContent::find_text($headText); foreach ($headText as $headTexts){ echo $navPnt == 0 ? $header = $headTexts->title; '<h3 class="menuheader expandable">'.$header.'</h3>' : ''; } Why not just do echo ( $navPnt == 0 ) ? '<h3 class="menuheader expandable">'.$headTexts->title.'</h3> : ''; There's no reason to assign a variable there Quote Link to comment https://forums.phpfreaks.com/topic/258488-how-would-i-turn-this-if-statement-in-to-a-ternary-operator/#findComment-1327067 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.