Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    WA
    WA is offline
    Administrator
    Join Date
    Mar 2002
    Posts
    2,596
    Thanks
    2
    Thanked 19 Times in 18 Posts

    Interesting thing I just learned about outputting text

    Most PHP books begin by telling you how you can output text using echo or print. I find it interesting how the langauge can display text without even any explicit function calls:

    PHP Code:
    <?
    $test
    =1;
    if (
    $test==1){
    ?>

    <b>This text is displayed if $test equals 1</b>

    <?
    }
    ?>
    Coming from JavaScript, this really illustrates how PHP is a server side language, and JavaScript is not
    - George
    - JavaScript Kit- JavaScript tutorials and 400+ scripts!
    - JavaScript Reference- JavaScript reference you can relate to.

  • #2
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,278
    Thanks
    4
    Thanked 83 Times in 82 Posts
    Yes that is a useful feature being able to just intermix php and html in that manner. I use that technique in both PHP and JSP.
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #3
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,040
    Thanks
    10
    Thanked 92 Times in 90 Posts
    A common mistake (especially with those with a PERL background) is to echo() or print() everything.

    PHP parses pages differently to some other languages in that it does not jump in and out of 'parser' mode when parsing a file so...

    <?
    echo "<html>";
    echo "<body>";
    echo $php_var;
    echo "more_html";
    echo $another_var;
    echo "</body>";
    echo "</html>";
    ?>

    is SLOWER than

    <html>
    <body>
    <?echo $php_var;?>
    ..some html
    <?echo $another_var;?>
    </body>
    </html>

    as PHP ignores anything that is not enclosed in PHP tags and parses the bits it finds.

    another excellent saver is

    <?readfile('some_file.txt);?>

    which passes the contents of some_file.txt directly to the browser so its a bit like using include() only a little faster (good for big blocks of text/html)

    other lifesaver that most PHPers find out after hours of string_replace()ing etc , is

    echo nl2br($string);

    which takes text from input with newlines (say a textarea) and tags a <br /> onto the end for you (XHTML compliant as well!) - the newline is not replaced so your code still looks neat when you view source etc , &

    trim($string)

    which cleans up whitespace and newlines especially good for password input etc

    just thought I would pop those out now as they are perhaps where I wasted the most time when I started with PHP ... anyone else with any lifesavers ??
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #4
    Registered User
    Join Date
    Jun 2002
    Location
    Arizona
    Posts
    175
    Thanks
    0
    Thanked 0 Times in 0 Posts
    htmlentities($string, ENT_QUOTES)

    cleans out all the html tags so people can't mess up your hard work by posting something like <table><tr>

  • #5
    Regular Coder
    Join Date
    May 2002
    Location
    London, England
    Posts
    367
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Also, this will work... (nabbed some example html from this page )

    PHP Code:
    <?
    //php code here
    echo<<<code;
    <
    td width="50%" height="50"><b><font color="#FFFFFF"
     
    face="Arial">&nbsp;</font><font size="5" face="Arial" 
    color="#DEDFDF">CodingForums.com</font></b><font color="#FFFFFF" face="Arial"><font face="Tahoma" size="5"><b><br>
    code;
    ?>
    I.e. no commenting needed

    Jee
    Jeewhizz - MySQL Moderator
    http://www.sitehq.co.uk
    PHP and MySQL Hosting

  • #6
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,278
    Thanks
    4
    Thanked 83 Times in 82 Posts
    Originally posted by Jeewhizz
    Also, this will work... (nabbed some example html from this page )

    PHP Code:
    <?
    //php code here
    echo<<<code;
    <
    td width="50%" height="50"><b><font color="#FFFFFF"
     
    face="Arial">&nbsp;</font><font size="5" face="Arial" 
    color="#DEDFDF">CodingForums.com</font></b><font color="#FFFFFF" face="Arial"><font face="Tahoma" size="5"><b><br>
    code;
    ?>
    I.e. no commenting needed

    Jee
    Yes that is heredoc syntax. Available since php4. You can also store it in a variable for echoing at a later time as well. I used it in a reply for someone else here

    php form to generate a html document
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster


  •  

    Posting Permissions

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