SitePoint Sponsor

User Tag List

Results 1 to 20 of 20
  1. #1
    SitePoint Addict
    Join Date
    Jan 2007
    Posts
    296
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Using PHP with HTML code inside an Echo Statement

    I am trying to write an echo statement to the page that puts a textarea on the page with a red color for the text.

    Normal HTML code would like this and my text would be in red

    Code:
    <textarea style="color:#FF0000;" name="message">Please Enter</textarea>
    Now I have tried this and I can't get it to work (it is part of a bigger script I have just trimmed it down for this example.)

    Code:
    <?php 
    {echo '<textarea style=\"color:#FF0000;\" name=\"message\">Please Enter</textarea>';}
    ?>
    Cheers

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    echo '<textarea style="color:#FF0000;" name="message">Please Enter</textarea>'
    Read
    http://www.php.net/types.string

  3. #3
    SitePoint Addict
    Join Date
    Jan 2007
    Posts
    296
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Cheers that is perfect

  4. #4
    SitePoint Zealot
    Join Date
    Jul 2008
    Posts
    188
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If your HTML statement is echoed between single quotes, you do not need to use escape characters. This is only if you're placing it between double quotes.

  5. #5
    hi galen's Avatar
    Join Date
    Jan 2006
    Location
    New Haven, CT
    Posts
    1,228
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Or if you want to do it a better way, just don't EVER echo html

  6. #6
    SitePoint Zealot
    Join Date
    Jul 2008
    Posts
    143
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?
    ob_start
    ();
    ?>
    <textarea style="color:#FF0000;" name="message">Please Enter</textarea>
    <?
    echo ob_get_clean();
    ?>
    This method is great for printing a large amount of html without having to worry about the html quotes and stuff.

  7. #7
    derrrp
    Join Date
    Aug 2006
    Location
    earth
    Posts
    923
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by galen View Post
    Or if you want to do it a better way, just don't EVER echo html
    hmmm...what if you have variables that need to be output inside HTML?

    What kind of syntax do you use?

    curious...
    No, I REALLY dislike having to use Joomla.

  8. #8
    SitePoint Wizard cranial-bore's Avatar
    Join Date
    Jan 2002
    Location
    Australia
    Posts
    2,634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    zurpit - the heredoc syntax is a far more efficient way that using object buffering like that.
    PHP Code:
    echo <<<EOD
    A big "block of text", with <whatever> characters you want.
    EOD; 

  9. #9
    SitePoint Zealot
    Join Date
    Jul 2008
    Posts
    188
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Galen and Zurpit.com's suggestions are a very good way of doing it. If you need to use a variable within HTML statement, you can just insert the <? echo "..." ?> between the HTML such as:

    Code:
     
    <? ob_start(); ?> 
    <textarea style="color:#FF0000;" name="message">
    <? echo $variable; ?>
    </textarea> 
    <? echo ob_get_clean(); ?>
    Or another example for setting the colour from a variable would be:

    Code:
     
    <? ob_start(); ?> 
    <textarea style="color:<? echo $colour; ?>;" name="message">Message</textarea> 
    <? echo ob_get_clean(); ?>

  10. #10
    SitePoint Zealot
    Join Date
    Jul 2008
    Posts
    143
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Good point, another clean way of printing a php variable in your html:

    instead of using:
    PHP Code:
    <? echo $variable?>
    you can use:

    PHP Code:
    <?=$variable;?>

  11. #11
    SitePoint Zealot
    Join Date
    Jul 2008
    Posts
    188
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I need to get used to all these shortcuts in PHP. They're very useful and will probably save a lot of time in the long-term.

    Thanks for the tip. I'll be using that from now on.

  12. #12
    SitePoint Zealot
    Join Date
    Jul 2008
    Posts
    143
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can also do conditional statement:

    instead of:
    PHP Code:
    <?
    if($isTrue){
        echo 
    "true";
    }else{
        echo 
    "false";
    }
    ?>
    you can do:
    PHP Code:
    <?=($isTrue)?"true":"false";?>

  13. #13
    hi galen's Avatar
    Join Date
    Jan 2006
    Location
    New Haven, CT
    Posts
    1,228
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by zurpit.com View Post
    You can also do conditional statement:

    instead of:
    PHP Code:
    <?
    if($isTrue){
        echo 
    "true";
    }else{
        echo 
    "false";
    }
    ?>
    you can do:
    PHP Code:
    <?=($isTrue)?"true":"false";?>
    don't echo html!

    PHP Code:
    <?php if($isTrue): ?>
    true
    <?php else: ?>
    false
    <?php endif; ?>

  14. #14
    SitePoint Zealot
    Join Date
    Jul 2008
    Posts
    143
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    echoing HTML is good sometimes.

    You can do this:

    PHP Code:

    <?php if($isValid): ?>

    <span class="valid">Some Text</span>

    <?php else: ?>

    <span class="invalid">Some Text</span>

    <?php endif; ?>
    Or do this (much shorter)
    PHP Code:
    <span class=<?=($isValid)?"invalid":"valid";?>>Some Text</span>

  15. #15
    derrrp
    Join Date
    Aug 2006
    Location
    earth
    Posts
    923
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ick...I find it annoying to see opening and closing php tags every other line and would rather just include HTML in the echo.

    In loops and conditionals especially...IMHO it looks messy to break in and out of php just to output HTML.

    Is there that big of a performance difference?
    No, I REALLY dislike having to use Joomla.

  16. #16
    SitePoint Zealot
    Join Date
    Jul 2008
    Posts
    188
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by crowden View Post
    ick...I find it annoying to see opening and closing php tags every other line and would rather just include HTML in the echo.

    In loops and conditionals especially...IMHO it looks messy to break in and out of php just to output HTML.

    Is there that big of a performance difference?
    I think by integrating the HTML in the echo, it makes the code easier to read. I would be interested to know if there is a performance difference between the two as well.

  17. #17
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Limotek View Post
    I think by integrating the HTML in the echo, it makes the code easier to read. I would be interested to know if there is a performance difference between the two as well.
    AFAIK, if there are lots of HTMLs in the echo '' (again there might be slightly difference double quotes and single quotes too) then of course there will be performance difference since the PHP compiler needs to compile the whole HTMLs as a PHP code. So if there is a simple and short HTML to print in between a long PHP code then better to echo it otherwise just do as galen says above.
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  18. #18
    SitePoint Enthusiast
    Join Date
    Jul 2008
    Posts
    90
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've been finishing off a project for a client where the previous developer has put almost all of a pages HTML within ECHO's. I'm find it really difficult to navigation my way around the code.

    I like to keep as much HTML as possible as HTML, this way, I can see DIV's and tables in dreamweaver, which makes amending layouts much easier. If you put a table or div with PHP code, it's really difficult to make changes as you can't visually see the DIV or tables when you're in dreamweaver.

    Yesterday, I had a thought. PHP on the server parses everything between the PHP tags, so, the more PHP there is, the more code needs to be parsed, and surely this will have an effect on speed. On a small project I doubt you'd see much difference, however on a larger project where there essentially could be thousands of lines of additional code caused by HTML in ECHO's, it's going to have a bigger effect.

    I wanted to test this for myself, and found a little script which starts a timer, and then ends it. I put some HTML and PHP code between the start and end of the timer.

    The script looped 99999 times, and created a drop down item for each loop. I did one the way I like to code, and the other with HTML within PHP tags.

    Now, this isn't conclusive evidence but, through all of my tests, the code with HTML within PHP ECHO's took longer to be generated on the server. Although there wasn't much difference in it (1000ms at most), there was a difference, and on a big project it could be wasted processor usage.

    What I find puzzling as well is, if you put HTML within an ECHO, then you're using processor usage to echo out the HTML. When it gets to the browser, the browser uses processor usage to render the page. So essentially, you're parsing the same code twice....for no reason whatsoever, and with absolutely no benefit.

    Save server resources and code correctly I say.

  19. #19
    hi galen's Avatar
    Join Date
    Jan 2006
    Location
    New Haven, CT
    Posts
    1,228
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    My biggest issue with echoing HTML is it's a lot harder to main and update.

  20. #20
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by littlened View Post
    I
    What I find puzzling as well is, if you put HTML within an ECHO, then you're using processor usage to echo out the HTML. When it gets to the browser, the browser uses processor usage to render the page. So essentially, you're parsing the same code twice....for no reason whatsoever, and with absolutely no benefit.
    The browser has to parse it no matter what you do, so that is irrelevant.

    The server processor is used no matter whether you echo or whether the html is used outside of <?php tags or not. Keep in mind, php is parsing every character in the file regardless. It needs to find its opening and closing tags. The webserver doesn't pre parse the file and send the blocks of php code to php. Php is sent a filename, and php is responsibe for loading the file and returning a result to the webserver. This means php reads the file from disk and parses it in its entirety.

    But yes, outputting strings which are outside of <?php tags is often a bit faster than using echo. The difference is generally insignificant, espescially if using echo on single quoted strings. To do a fair comparison, a single echo should be used(probably spanning multiple lines). Multiple calls to echo would only be fairly compared to multiple breaking in and out of php tags.

    Double quoted strings take longer to parse than single quoted strings, because php recognizes more escape sequences in double quoted strings, as well as having to do variable expansion.

    Output buffering settings have a large effect on the speed of all of this.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •