Jump to content

coding question


CyberShot

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 :)

Link to comment
Share on other sites

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 :P

Link to comment
Share on other sites

your confusing him now.

:facepalm:

 

Blahh... i need to turn on the notify when there is a reply again :P

 

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? :P

Link to comment
Share on other sites

Because it gets annoying when you reply to topics that people get offtopic to while you type a meaningful post? :P

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.