Results 1 to 7 of 7

Thread: Shorten your PHP Code, and make it easy.

  1. #1
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default Shorten your PHP Code, and make it easy.

    I've been seeing a lot of people having code like this:
    PHP Code:
    <?php
    echo "<html>";
    echo 
    "<head>";
    echo 
    "<title>Title</title>";
    echo 
    "</head>";
    echo 
    "</html>";
    ?>
    Now, some of you out there may say "There's no error!", you're right there isn't, but it would take up less room if you do something like this:
    PHP Code:
    <?php
    echo <<< PAGE
    <html>
    <head>
    <title>Title</title>
    </head>
    </html>
    PAGE;
    ?>
    Or even this:
    PHP Code:
    <?php
    echo "
    <html>
    <head>
    <title>Title</title>
    </head>
    </html>
    "
    ;
    ?>
    So this is just a useful tip, you don't need to use it or anything, just thought it'd be helpful.
    Questions will be answered.
    Jeremy | jfein.net

  2. #2
    Join Date
    Jul 2006
    Location
    just north of Boston, MA
    Posts
    1,806
    Thanks
    13
    Thanked 72 Times in 72 Posts

    Default

    When writing HTML code in php, its generally thought as better practice to separate the content from the actual code... therefore in those two examples the first one would be a better idea, however what I prefer is something more along the lines of

    PHP Code:
    <?php
    if(condition)
    {
    ?>
    <p>Label: <?php echo $value?></p>
    <?php
    }
    else
    {
    ?>
    <p>Error: <?php echo $error?></p>
    <?php
    }
    ?>
    that is alittle bit better because it separates out the two, however you are still not entirely free and clean. the BEST solution, would be to use a template's engine like Smarty which gives you the full data and code extraction

  3. #3
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,156
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    I'm not sure I really agree here.

    There's certainly a valid point to simplifying code, but the point is readability. As such, having lots of small bits is actually easier to read than one large chunk.

    I think that the main point is simply to have well organized code.

    In the case above, it makes some sense to separate those elements (like boogyman did, especially). But certainly streamlining similar processes is in general good practice.

    In addition, it would be great if when asking for help your code had comments. It would be much faster to help as well, if that was the case.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  4. #4
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    I don't know if your saying my code would be easier if it had comments because it's the simplest thing ever. But you said having small bits is easier, and tidier. I think that it almost is identical to this:
    PHP Code:
    <?php
    echo <<< PAGE
    <html>
    <head>
    <title>Title</title>
    </head>
    </html>
    PAGE;
    ?>
    Except with more space.
    Jeremy | jfein.net

  5. #5
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,156
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Well, smaller bits meant that having one echo per line is actually pretty easy to read. Especially when those represent different sections of the html page.

    And, no, I didn't mean you should add comments, but that people should when posting questions, in addition to having organized code. "You" = "someone".
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  6. #6
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    Ahh, ok I see. What does your mean then? lol.
    Jeremy | jfein.net

  7. #7
    Join Date
    Jan 2007
    Posts
    629
    Thanks
    10
    Thanked 28 Times in 28 Posts

    Default

    This is good for template files, though (I think that was Nile's original point). Say you have one PHP file with nothing but a large template function, like:

    PHP Code:
    <?php
    function createPage($title,$body,$author,$date){

    echo <<< END
        <html>
    <head>
        <title>
    $title</title>
    </head>
    <body>
        <div id="main">
            <div id="menu">
                menu
            </div>
            <div id="body">
                
    $body
            </div>
            <div id="footer">
                <p>Page by 
    $author on $date</p>
            </div>
        </div>
    </body>
        </html>
    END;

    }
    ?>
    Can you imagine putting individual echo statements for each line, or inline echo statements for each var? Or trying to slash out ever double quote? But for inline echo-ing, it makes it hard to read. Everything has its place.

    I guess the main thing is preference.
    Last edited by Jas; 06-26-2008 at 07:30 PM.
    --Jas
    function GreatMinds(){ return "Think Like Jas"; }
    I'm gone for a while, but in the meantime: Try using my FTP script | Fight Bot Form Submissions

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
  •