treehouse : what would you like to learn today?
Web Design Web Development iOS Development

why does this work?

  • I'm going to ask a strange question. I'm using this code and it works, but I don't understand why.
    I'm new to php, and am just learning the syntax. This syntax looks wrong to me....but it works.
    I ask because I'm trying to write another conditional statement and using this as a model...

    What I don't understand is...why is ?> at the beginning and<?php at the end of the internal statements?



    <?php if(is_front_page())
    { ?> <object width=\"1000\" height=\"296\"><param name=\"movie\" value=\"ns_header.swf\"><embed src=\"<?php bloginfo('template_directory');?>/images/ns_header.swf\" width=\"1000\" height=\"296\"></embed>
    </object> <?php }
    else
    { ?><img class=\"headerimg\" src=\"<?php
    bloginfo('template_directory');?>/images/headerimgs/rotate.php\" alt=\"A
    Random Header Image\"/><?php } ?>
  • It's just closing the php interpreter to spill out some html, and then reopening it.

    If you don't know how code works, just play with it. For example, get rid of all the php start and stops. Then, put all the html tags in something like this:

    echo "<html_tags>Go in double quotes</html_tags";

    The only way to learn to code is to experiment.
  • Breaking that snippet down, here's a pseudo-code version. Opening and closing PHP tags, <?php and ?>, need to be included around everything that is php code, so the server understands how to process it correctly:


    <?php if (current page is home page) { ?>

    ... normal html code here... (only shows up if the current page is the home page)

    <?php } else (if the current page is not the home page) { ?>

    ... more normal html code... (only shows up if the current page is not the home page)

    <?php } ?>


    http://www.w3schools.com/php/php_if_else.asp