Echo....Echo......Echo

Discussion in 'PHP' started by geekazoid, Jul 13, 2006.

  1. #1
    I want to use the echo majiger in php. but im unsure to as how you would do this >>>

    I would like to write one word in my page in a large font and blue. Then i want the same word to be echoed into my title and also in the bottom of my page in a differant script.

    So is it possible to echo a word without echoing the font and also is possible to echo a word into the title ? and if so does anyone know how ? Thanx peeps
     
    geekazoid, Jul 13, 2006 IP
  2. T0PS3O

    T0PS3O Feel Good PLC

    Messages:
    13,219
    Likes Received:
    777
    Best Answers:
    0
    Trophy Points:
    0
    #2
    No, not without echoeing the font...

    
    $the_word = "Geekazoid";
    echo '<title>' . $the_word . '</title>';
    echo '<h4>' . $the_word . '</h4>';
    echo '<p>DP member <font color="red" size="2">' . $the_word . ' is learning PHP fast!</font></p>';
    
    PHP:
     
    T0PS3O, Jul 13, 2006 IP
  3. JEET

    JEET Notable Member

    Messages:
    3,832
    Likes Received:
    502
    Best Answers:
    19
    Trophy Points:
    265
    #3
    Yes, you can echo the word anywhere. In title, in meta tags, in links, in body, anywhere... The font would depend on whether the HTML tag supports fonts or not.
    <TITLE> does not support fonts so you will have to simply echo like:

    <?php
    $word= "zoid";
    echo "<title> $word </title>";
    ?>

    You can also do the following:
    <html><head>
    <title>
    <?php
    $word= "zoid";
    echo $word;
    ?>
    <title></head><body>...

    See in the second example the HTML tag is outside the php code. Use this to write fonts in HTML and then echo word using PHP.
    Bye
     
    JEET, Jul 13, 2006 IP
  4. clancey

    clancey Peon

    Messages:
    1,099
    Likes Received:
    63
    Best Answers:
    0
    Trophy Points:
    0
    #4
    As mentioned, you cannot change the color of a word without some sort of tagging. If you do not want to use the font tag, use CSS.

    
    <?php
    echo '<html><body>
    <style tyle=text/css>
    span.red {color: red; }
    span.pink {color: pink;}
    </style>';
    
    echo '<span class="red">word</span> -- ';
    echo '<span class="pink">newWord</span>';
    echo '</body></html>';
    ?>
    
    Code (markup):
    You could have the class names be a combination of text and a number. You could then generate a random number within the range covered by your class definitions and surprise yourself!
     
    clancey, Jul 13, 2006 IP
  5. penagate

    penagate Guest

    Messages:
    277
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    0
    #5
    You should never use echo() to output HTML. PHP is designed by nature to output. Using echo() hinders both readability and performance. Simply close the <?php tags like this:

    <?php
      // some PHP code
    ?>
    <!-- some HTML code -->
    <?php
      // more PHP code
    ?>
    
    PHP:
    echo() is useful really only for debugging purposes. It is a bad idea in a production situation and you certainly should not use it as the primary means of page output.

    Hope that helps you

    - P
     
    penagate, Jul 13, 2006 IP
  6. geekazoid

    geekazoid Peon

    Messages:
    208
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Thankyou guys its working now.
     
    geekazoid, Jul 13, 2006 IP
  7. penagate

    penagate Guest

    Messages:
    277
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    0
    #7
    As an addition to my previous post, there is actually one use for echo() - outputting PHP variables in the HTML. But then if you have short tags enabled (as by default) you can just do this:
    <?=$variableName?>
    
    PHP:
    It is a matter of preference which you use. But that does not void my previous points ;-)

    - P
     
    penagate, Jul 13, 2006 IP
  8. jnestor

    jnestor Peon

    Messages:
    133
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #8
    The problem comes when your HTML is littered with tons of PHP variables. Say you want to output a series of links with CSS styling. You have 3 basic options:

    echo the html:
    echo "<a href=\"$url\" class=\"$class\">$link_text</a>";
    or equally ugly:
    echo '<a href="'.$url.'" class="'.$class.'">'.$link_text.'</a>';

    lots of embedded php in your html
    <a href="<?=$url?>" class="<?=$class?>"><?=$link_text?></a>

    or echo the html with a heredoc
    echo <<<HTML
    <a href="$url" class="$class">$link_text</a>
    HTML;

    Obviously a small example. When your HTML gets more complicated you can see how it easily gets pretty unwieldy with escaping quotes and keeping track of <? ?> tags etc. When things get to a certain level of complication I think the heredoc format is the best though it's really nothing more than echoing all your HTML. On big/complex projects however I think you'd need to bring in a templating system like Smartty (which I really need to learn).
     
    jnestor, Jul 13, 2006 IP
  9. Rjx

    Rjx Peon

    Messages:
    15
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Heredoc is excellent for HTML snippets, one thing to note that i've discovered is that constants don't get parsed, only variables.

    With reference to Smarty (i don't want to start a flamewar here) it has always seemed like an overengineered solution to templating. At the end of the day you end up rewriting PHP in PHP. A lot of the time it makes more sense to write your own templating kit that you can then redeploy on other sites that you create.
     
    Rjx, Jul 14, 2006 IP
  10. clancey

    clancey Peon

    Messages:
    1,099
    Likes Received:
    63
    Best Answers:
    0
    Trophy Points:
    0
    #10
    For production, I normally build the page to be output and them pump it out in one blast. This is a form of templating because the header and footers are kept separate from the content section and any add on segments. Once assembled I would push the content in a single print or echo statement. For simple pages, I normally prepare them as completed phtml pages, inserting bits of code as needed.

    Incidentally, penagate, I have installed PHP numerous times on Windows and Linux boxes and the default ini has always disabled short tags. In fact, the ini file contains the following statement:

     
    clancey, Jul 14, 2006 IP