Results 1 to 11 of 11

Thread: Writing HTML inside of PHP documents..

  1. #1

    Thread Starter
    PowerPoster Pc_Madness's Avatar
    Join Date
    Dec 2001
    Location
    Melbourne, Australia
    Posts
    2,765

    Writing HTML inside of PHP documents..

    Hey guys, just wondering how you guys go about writing HTML inside a PHP document? I've been doing it all inside Prints, which means I have to escape the talking marks, so although its probably easier to work with, I find it less readable, and just generally impossible to get it all indented properly for when I view the source in a browser.

    PHP Code:
                print "<div id=\"loginwrapper\">\n";
                print 
    "  <div id=\"CreateLoginDiv\">\n";
                print 
    "    <div id=\"CreateLoginDivTitle\">Please Login:</div>\n";
                print 
    "    <div id=\"CreateLoginDivContent\">\n";
                if(
    $bError) print "<center><div class=\"ErrorBox\"><h4>Error</h4>Invalid Username or Password. Please try again.</div></center>";
                print 
    "      <form method=\"POST\" action=\"./login.php\">\n
                                 <label>Username<input type=\"text\" name=\"username\" "

    What method do you guys use?
    Don't Rate my posts.

  2. #2
    Addicted Member Peter1's Avatar
    Join Date
    Aug 2002
    Posts
    166

    Re: Writing HTML inside of PHP documents..

    For large blocks of HTML code I usually just do this:

    PHP Code:
    //PHP Code...
    ?>
        <div class="header">
            More HTML Code
        </div>
    <?php
    //More PHP Code ...
    Works inside If's as well

    PHP Code:
    //PHP Code...
    if(true)
    {
    ?>
        <div class="header">
            More HTML Code
        </div>
    <?php
    }else{
    ?>
        <div class="header">
            A Different Header
        </div>
    <?php
    }
    //More PHP Code ...

  3. #3

    Thread Starter
    PowerPoster Pc_Madness's Avatar
    Join Date
    Dec 2001
    Location
    Melbourne, Australia
    Posts
    2,765

    Re: Writing HTML inside of PHP documents..

    Yeah, but then if you need to add in a single line of php you end up with the little nested <? print "" ?> through out your HTML. I kinda see that solution as the other end of the problem, where the HTML is readable, but the PHP isn't..
    Don't Rate my posts.

  4. #4
    Addicted Member Peter1's Avatar
    Join Date
    Aug 2002
    Posts
    166

    Re: Writing HTML inside of PHP documents..

    You can print shorthand by doing <?="I want to print this text" ?> or <?=$myText ?>

  5. #5
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Writing HTML inside of PHP documents..

    This is how I do it: https://www.vbforums.com/showthread.php?p=2168862

    PHP is/was originaly intended to be a templating language. Which is why, by default you are always in HTML mode. It is never a good idea to output large chunks of HTML using print and echo statements. Two of the reasons, readability and escaping quotes you have already identified; but here are a couple more:
    • When ever you use a string enclosed in double quotes, PHP needs to check the entire string for embedded variables. This gives the PHP interpreter unecessary processing and makes the script slower.
    • Any kind of output in the middle of a script is a bad idea. You should always keep the output clearly separate from the process. Limit any logic in output to display logic and keep the process logic, such as accessing databases in the main body of the script.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  6. #6
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Writing HTML inside of PHP documents..

    Quote Originally Posted by Pc_Madness
    Yeah, but then if you need to add in a single line of php you end up with the little nested <? print "" ?> through out your HTML. I kinda see that solution as the other end of the problem, where the HTML is readable, but the PHP isn't..
    PHP Code:
    <?php // tmeplate example ?>
    <p><?php echo($variable?></p>
    It seems readable to me
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  7. #7

    Thread Starter
    PowerPoster Pc_Madness's Avatar
    Join Date
    Dec 2001
    Location
    Melbourne, Australia
    Posts
    2,765

    Re: Writing HTML inside of PHP documents..

    Perhaps if you were doing something like,
    PHP Code:
    <a href="http://myurl.php?parameter1=<?php print $variable1 ?>&parameter2=<?php print $variable2?>&parameter3=<?print $variable3?>">Link</a>
    It kinda gets to the point where you could probably save your self the trouble and just print out the whole line instead. In my IDE (TextWrangler) all strings show as pink, while variables are shown in blue, so it stands out pretty clearly when I nest them with Print..

    Any kind of output in the middle of a script is a bad idea. You should always keep the output clearly separate from the process. Limit any logic in output to display logic and keep the process logic, such as accessing databases in the main body of the script.
    Hmmmm, thats a good point... does my code snippet above count as output or logic? As the only other way of doing it would be to use a variable to hold the string or something like that... :\ Working with databases has really forced me to processing and output, but I feel like I could improve it abit more...
    Last edited by Pc_Madness; Sep 20th, 2005 at 06:49 AM.
    Don't Rate my posts.

  8. #8
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: Writing HTML inside of PHP documents..

    How about this?
    PHP Code:
    <?php // comment ?>
    <a href="http://myurl.php?<?php echo("parameter1={$variable1}&amp;parameter2={$variable2}&amp;parameter3={$variable3}"?>">Link</a>
    Using the curly braces around the embedded variables isn't required, but it makes them stand out more and makes their parsing quicker. Small lines like this are ok, that is why PHP allows you to embed variables in a string.

    Hmmmm, thats a good point... does my code snippet above count as output or logic? As the only other way of doing it would be to use a variable to hold the string or something like that... :\ Working with databases has really forced me to processing and output, but I feel like I could improve it abit more...
    I've never seen any clearly defined rules as to what should be classified as output of processing. You need to think when you create your output, what if I wanted to change the backend database - if the answer to your question, is I would have to change some of the code in the output, then the answer is, that that code should be in the main part of the script.

    When it comes to databases. Especially loading multiple rows into an HTML table, I load the rows first into an array and traverse the array in the output part of the script. Yes, it does mean you need to store a copy of the data, however you gain a big advantage in portability as it makes no difference whether the data came from a database, a text file or a giraffes bottom.
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  9. #9
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: Writing HTML inside of PHP documents..

    As of late, I only use a template engine for output. Usually Smarty.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  10. #10
    New Member
    Join Date
    May 2012
    Location
    pakistan
    Posts
    1

    Re: Writing HTML inside of PHP documents..

    Quote Originally Posted by Pc_Madness View Post
    Hey guys, just wondering how you guys go about writing HTML inside a PHP document? I've been doing it all inside Prints, which means I have to escape the talking marks, so although its probably easier to work with, I find it less readable, and just generally impossible to get it all indented properly for when I view the source in a browser.

    PHP Code:
                print "<div id=\"loginwrapper\">\n";
                print 
    "  <div id=\"CreateLoginDiv\">\n";
                print 
    "    <div id=\"CreateLoginDivTitle\">Please Login:</div>\n";
                print 
    "    <div id=\"CreateLoginDivContent\">\n";
                if(
    $bError) print "<center><div class=\"ErrorBox\"><h4>Error</h4>Invalid Username or Password. Please try again.</div></center>";
                print 
    "      <form method=\"POST\" action=\"./login.php\">\n
                                 <label>Username<input type=\"text\" name=\"username\" "

    What method do you guys use?
    this was right code my dear

    <?php
    print "<div id=\"loginwrapper\">\n";
    print " <div id=\"CreateLoginDiv\">\n";
    print " <div id=\"CreateLoginDivTitle\">Please Login:</div><br>\n";
    print " <div id=\"CreateLoginDivContent\">\n";
    print " <form method=\"POST\" action=\"./login.php\">\n
    <label>first name: <input type=\"text\" name=\"firs_tname\" /></label><br> ";
    print " <label>last name: <input type=\"text\" name=\"last_name\" /></label><br> ";
    print " <label>password: <input type=\"password\" name=\"password\" /></label> ";
    print " <label><input type=\"submit\" name=\"subm\" value=\"go for...\" /></label><br> ";

    ?>

  11. #11
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Writing HTML inside of PHP documents..

    what the heck? Really? You dredged up a SIX year old thread, just to repost what the OP posted in the first place and go "yeah, that's right." .... did you not read the whole thread? Where the general consensus was to NOT do that?

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

Posting Permissions

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



Click Here to Expand Forum to Full Width