CyberShot Posted October 1, 2009 Share Posted October 1, 2009 I am learning php. I just read about another way to write an if statement $agestr = ($age < 16) ? 'child' : 'adult'; So I understand what is being done here. The thing that threw me before was the : What I am trying to figure out is what the : means by itself. I have seen code in wordpress that says things like this <?php if (have_posts()) : ?> <?php while (have_posts()) : the_post(); ?> what purpose would the : serve in this situation. Quote Link to comment https://forums.phpfreaks.com/topic/176144-coding-question/ Share on other sites More sharing options...
genericnumber1 Posted October 1, 2009 Share Posted October 1, 2009 It's called the ternary operator. (as opposed to binary operators like =, +, ==, etc). It's shorthand for <?php if ($age < 16) { $agestr = 'child'; } else { $agestr = 'child'; } ?> Think of ? as "then" and : as "else". $someVariable = (BooleanCondition) ? 'ValueIfBooleanConditionIsTrue' : 'ValueIfBooleanConditionIsFalse'; could be read as $someVariable = if SomethingIsTrue then "Set someVariable to this value" else "Set someVariable to this value" Do note this can be used for any datatypes, including strings, as we did in the above examples. It has nothing to do with the alternate if, while, for, etc syntax that are often used in templates for readability like wordpress. Quote Link to comment https://forums.phpfreaks.com/topic/176144-coding-question/#findComment-928202 Share on other sites More sharing options...
CyberShot Posted October 1, 2009 Author Share Posted October 1, 2009 I get that. But how does the ternary operator operate on it's own as in the wordpress sample code above? It doesn't follow the format of $someVariable = (BooleanCondition) ? 'ValueIfBooleanConditionIsTrue' : 'ValueIfBooleanConditionIsFalse'; Quote Link to comment https://forums.phpfreaks.com/topic/176144-coding-question/#findComment-928206 Share on other sites More sharing options...
genericnumber1 Posted October 1, 2009 Share Posted October 1, 2009 In that case, it's not the ternary operator. In fact, those if while statements you showed aren't even specific to wordpress, you can use them any where as alternative syntax. For example a php page consisting of only: <?php $pie = true; if ($pie) : while ($pie) : echo 'Pie\'s true!'; $pie = false; endwhile; endif; ?> is perfectly valid syntax even without wordpress anywhere in site (sorry, terrible pun). It's a good observation that they both use a colon, but they have completely different meanings and are treated completely differently as far as php is concerned. Quote Link to comment https://forums.phpfreaks.com/topic/176144-coding-question/#findComment-928208 Share on other sites More sharing options...
redarrow Posted October 1, 2009 Share Posted October 1, 2009 ? == if : == else <?php $redarrow=36; $age=36; $example=($redarrow >= $age) ? "yes your old":"your young"; echo $example; ?> Quote Link to comment https://forums.phpfreaks.com/topic/176144-coding-question/#findComment-928210 Share on other sites More sharing options...
CyberShot Posted October 1, 2009 Author Share Posted October 1, 2009 I just got the code from wordpress. that is where I have seen it used. I know it's not just for wordrpess. What I am trying to do is understand what it is doing there. What purpose is it serving? what does it mean to be all alone like that? Quote Link to comment https://forums.phpfreaks.com/topic/176144-coding-question/#findComment-928212 Share on other sites More sharing options...
redarrow Posted October 1, 2009 Share Posted October 1, 2009 And the truth of the matter, if you dont like the ternary, then use the if, and else, there no difference, only programmers preference like good old oop(object orientated programming)) It only a short cut for if and else that it... Quote Link to comment https://forums.phpfreaks.com/topic/176144-coding-question/#findComment-928213 Share on other sites More sharing options...
CyberShot Posted October 1, 2009 Author Share Posted October 1, 2009 I like it. But you haven't yet explained what it does. LOL. What does the colon : do all by itself as posted above in the wordpress code? Why is it there all by itself? What purpose does it serve. Quote Link to comment https://forums.phpfreaks.com/topic/176144-coding-question/#findComment-928214 Share on other sites More sharing options...
genericnumber1 Posted October 1, 2009 Share Posted October 1, 2009 I just got the code from wordpress. that is where I have seen it used. I know it's not just for wordrpess. What I am trying to do is understand what it is doing there. What purpose is it serving? what does it mean to be all alone like that? It's the exact same meaning as if it used braces, some people just like to use it because they think it looks prettier in their web page templates, and can make things easier to read For example take these two (functionally) identical pages and consider which is easier to read <?php if ($something == true) { ?> Text text textText text text Text text text Text text text Text text text Text text text Text text textText text text Text text text Text text text Text text text Text text text Text text textText text text Text text text Text text text Text text text Text text text Text text text Text text textText text textText text textText text textText text text Text text text <?php while(false) { ?> Text text text Text text Text text text Text text text Text text text Text text text Text text text Text text textText text text Text text text Text text text Text text text Text text text Text text texttext Text text text Text text text Text text text Text text text <?php } ?> Text text text Text textText text text Text text text Text text text Text text text Text text text Text text textText text text Text text text Text text text Text text text Text text text Text text text text Text text text Text text text Text text text Text text textText text text Text text text Text text text Text text text Text text text Text text text <?php } ?> versus <?php if ($something == true) : ?> Text text textText text text Text text text Text text text Text text text Text text text Text text textText text text Text text text Text text text Text text text Text text text Text text textText text text Text text text Text text text Text text text Text text text Text text text Text text textText text textText text textText text textText text text Text text text <?php while(false) : ?> Text text text Text text Text text text Text text text Text text text Text text text Text text text Text text textText text text Text text text Text text text Text text text Text text text Text text texttext Text text text Text text text Text text text Text text text <?php endwhile; ?> Text text text Text textText text text Text text text Text text text Text text text Text text text Text text textText text text Text text text Text text text Text text text Text text text Text text text text Text text text Text text text Text text text Text text textText text text Text text text Text text text Text text text Text text text Text text text <?php endif; ?> The second syntax is a bit easier to read, which is why people use them sometimes for templates where php and HTML are freely mixed. They are EXACTLY the same functionally. More reading: http://us2.php.net/manual/en/control-structures.alternative-syntax.php Quote Link to comment https://forums.phpfreaks.com/topic/176144-coding-question/#findComment-928216 Share on other sites More sharing options...
RussellReal Posted October 1, 2009 Share Posted October 1, 2009 somewhere after there is an endwhile; and an endif; basically its the same as doing while (...) { } except... while (...) : endwhile; its I guess to make visual basic programmers to feel more comfortable with php.. its nothing to get excited about and the ternary operator is the : in an inline if statement, the : you're referencing is just some bridge to reach out to vb programmers Quote Link to comment https://forums.phpfreaks.com/topic/176144-coding-question/#findComment-928217 Share on other sites More sharing options...
Philip Posted October 1, 2009 Share Posted October 1, 2009 As mentioned before, you should see an endwhile; somewhere later in the script. In this case it is not a ternary operator, but an alternate syntax. From the PHP manual: while (expr): statement ... endwhile; Or, both of the following are the same: <?php /* example 1 */ $i = 1; while ($i <= 10) { echo $i++; /* the printed value would be $i before the increment (post-increment) */ } /* example 2 */ $i = 1; while ($i <= 10): echo $i; $i++; endwhile; ?> Blahh... i need to turn on the notify when there is a reply again Quote Link to comment https://forums.phpfreaks.com/topic/176144-coding-question/#findComment-928218 Share on other sites More sharing options...
CyberShot Posted October 1, 2009 Author Share Posted October 1, 2009 Ok, Thanks guys Quote Link to comment https://forums.phpfreaks.com/topic/176144-coding-question/#findComment-928219 Share on other sites More sharing options...
redarrow Posted October 1, 2009 Share Posted October 1, 2009 your confusing him now. endwhile; and endif are totally different subject of ternary. Quote Link to comment https://forums.phpfreaks.com/topic/176144-coding-question/#findComment-928221 Share on other sites More sharing options...
RussellReal Posted October 1, 2009 Share Posted October 1, 2009 your confusing him now. endwhile; and endif are totally different subject of ternary. dude keep up thats what me and kingphilip just said Quote Link to comment https://forums.phpfreaks.com/topic/176144-coding-question/#findComment-928222 Share on other sites More sharing options...
genericnumber1 Posted October 1, 2009 Share Posted October 1, 2009 Blahh... i need to turn on the notify when there is a reply again Why would you ever disable such a cool feature? Quote Link to comment https://forums.phpfreaks.com/topic/176144-coding-question/#findComment-928223 Share on other sites More sharing options...
Philip Posted October 1, 2009 Share Posted October 1, 2009 your confusing him now. Blahh... i need to turn on the notify when there is a reply again Why would you ever disable such a cool feature? Because it gets annoying when you reply to topics that people get offtopic to while you type a meaningful post? Quote Link to comment https://forums.phpfreaks.com/topic/176144-coding-question/#findComment-928226 Share on other sites More sharing options...
genericnumber1 Posted October 1, 2009 Share Posted October 1, 2009 Because it gets annoying when you reply to topics that people get offtopic to while you type a meaningful post? Touche. your confusing him now. In the future I will defer to your superior grasp of people's problems and your probable solutions. Quote Link to comment https://forums.phpfreaks.com/topic/176144-coding-question/#findComment-928228 Share on other sites More sharing options...
RussellReal Posted October 1, 2009 Share Posted October 1, 2009 Because it gets annoying when you reply to topics that people get offtopic to while you type a meaningful post? Touche. your confusing him now. In the future I will defer to your superior grasp of people's problems and your probable solutions. ^^ lol wut Quote Link to comment https://forums.phpfreaks.com/topic/176144-coding-question/#findComment-928229 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.