WordPress.org

Ready to get started?Download WordPress

Forums

simple php echo question (3 posts)

  1. syrupcore
    Member
    Posted 8 years ago #

    This should work but it's not. I'm sure I have my syntax screwy.

    <?php if (count($kids) > 0) {echo "<h2><?php (get_the_title($parent))?></h2>";} ?>

    I have declared this previously:
    $kids = get_pages("child_of=$id");

    the $kids bit is working fine with a bit used later. I think I just have more php all mixed up in the first line. can some one please tell me where?

    thank you.

  2. skippy
    Member
    Posted 8 years ago #

    <?php
    if (count($kids) > 0) {
    echo "<h2><?php (get_the_title($parent))?></h2>";
    } ?>

    You never close the PHP tag you opened at the very beginning, so opening a new PHP tag with <?php causes the PHP parser to get confused. Try this:

    <?php
    if (count($kids) > 0) {
    echo '<h2>' . get_the_title($parent) . '</h2>';
    }
    ?>

    Or:
    <?php
    if (count($kids) > 0) {
    $t = get_the_title($parent);
    echo "<h2>$t</h2>";
    }
    ?>

    Or even:
    <?php
    if (count($kids) > 0) {
    echo '<h2>';
    the_title($parent);
    echo '</h2>';
    }
    ?>

    I showed you all three of these to highlight certain differences. Note that for (I think) every get_the_* function there is also a the_* function. get_the_* returns to you the value you requested for use in a variable, or other function (like echo). the_* displays the value directly to the browser.

    Note also the different uses of single and double quotes. Single quotes do no variable interpolation. So echo '$foo'; will echo the literal value $foo, dollar sign and all. Because they do no interpolation, they're a little faster.

    Double quotes do interpolate variables, so echo "$foo"; will echo the value contained in the variable $foo.

    Hope that helps!

  3. syrupcore
    Member
    Posted 8 years ago #

    Skippy! Thanks so much. I haven't had a chance to test it but will this afternoon and report back. at the moment, I just moved the site to a new server and I managed to break it. sweet.

    you've come to my rescue more than once. If you're in portland, beer is on me.

    will

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.