• marktaff

    (@marktaff)


    I’m hacking on a template, specifically comments.php, and I came across the following code snippet, which is confusing. Apparently even the PHP devs find it confusing, according to google. But none of the google results gave me an answer.

    Oh? The question? Yes, right away.

    What does the colon (:) do at the line ends? I’d like to rewrite it so its less confusing. Thanks.

    Mark

    if ( $comments ) :

    $myvar=0;
    foreach ($comments as $comment) :
    $myvar++;
    if ($myvar == 1) {$swap = "background-color: #eeeee6; padding: 5px;";}

Viewing 4 replies - 1 through 4 (of 4 total)
  • ColdForged

    (@coldforged)

    Welcome to hell… it’s another way to declare a code block. There’s a corresponding endif somewhere that marks the end of that block. Similar with while() : ... endwhile;

    Thread Starter marktaff

    (@marktaff)

    Thanks, ColdForged. Now I can refactor the code to make it more clear.

    Ming

    (@ming)

    movabletripe

    (@movabletripe)

    It is also used in a Ternary Operator.

    I personally love ternary operators. They are a little harder to understand at first (at least compared to a simple if-then-else snippet) but they are a great way to cut down some bloat. Example:

    <?php

    echo ($answer ? "blue" : "green");

    //is the same as

    if($answer)
    {
    echo "blue";
    }
    else
    {
    echo "green";
    }

    ?>

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘What does PHP colon (:) do?’ is closed to new replies.