• Resolved OM2

    (@om2)


    I see this type of coding all over the place in WordPress:

    <?php if (!is_paged() && is_home()) { ?>current_page_item<?php } else { ?>page_item<?php } ?>" id="header-menu-home"><a href="<?php echo get_settings('home'); ?>/"><?php _e('HOME','piano-black'); ?></a>

    I’m confused!
    I always thought every bit of PHP code was self contained – i.e. start with <?php and end with ?>
    I thought all code went in there

    Is it OK to start and stop code as above and have several instances of <?php ?>
    (Obviously it must be!)
    Do variables carry over from one instance to another?

    ALSO: WHY would you want to do it the way above? Why not have everything inside one <?php ?>

    THANKS!

    Omar

Viewing 3 replies - 1 through 3 (of 3 total)
  • <?php // Anything here is treated as PHP code ?>
    Anything here is treated as HTML
    <?php // Again anything here is treated as PHP ?>
    And again anything here is treated as HTML

    When you’re writing code for a template file for example, there may be a large amount of HTML, in such a case it may be easier for you(whoever is editting the code) to understand or change in the above format over a pure PHP equivalent..

    I think it’s simply down to preference… although usually i find the PHP approach easier myself, i generally write in the above format when giving examples, it’s more fool proof and easier to read/understand when it comes to users not familiar with code.. (at least i found that to be the case)..

    For what it’s worth, i did ask this question myself some time ago, and the above is the conclusion i came to, other peoples reasons may differ to mine.

    that above code is actually switching from php to html to php to html to php to html …..

    so, everytime the php tag is closed, some html code starts (which could be just plain text, or as in your example, some part of a class in a div).

    if you want to write everything in one php tag, you could use ‘echo’ to display the html parts, but this might not be very easy to read, as well.

    Thread Starter OM2

    (@om2)

    guys, thanks for the replies
    thats really helpful actually – i had no idea u could do that

    i used to use <<<

    i.e.

    <?php

    // php code here

    echo <<<myDefaultString
    // lots of html code
    myDefaultString;

    // more php code here

    ?>

    i have to admit: i much prefer the other way – the way i was doing it was always a bit buggy – miss an odd character and everyhting doesn’t work
    this way: i think it’s easier to read

    thanks!! ??

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Question about PHP coding – using several <?php ?>’ is closed to new replies.