Code efficiency - transferring from php to html and back

Discussion in 'PHP' started by CurryMan, Oct 15, 2009.

  1. #1
    Hi,

    Does anyone know how much of an overhead on a web server's processing power is it to frequently shift in and out of <?php [...] ?>

    I've got a dynamically generated page that's a bit slow and I'm wondering if I recode it to avoid transitioning in and out of HTML, whether this is likely to make a siginificant difference.

    Thanks.
     
    CurryMan, Oct 15, 2009 IP
  2. HomeComputerGames

    HomeComputerGames Peon

    Messages:
    871
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I'm not sure I understand.
    What is returned to the browser is all HTML and is what PHP sends to the browser.
    If you mean having bits of PHP code throughout your document I doubt this is your problem.

    What does this page do?
    Does this slow page access MYSQL?
     
    HomeComputerGames, Oct 15, 2009 IP
  3. CurryMan

    CurryMan Peon

    Messages:
    22
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Yes, it accesses MySQL.

    Let me give 2 examples:

    1. Switching between PHP and HTML

    <table><tr><td>
    <?php
    echo $x;
    ?>
    </td><td>
    <?php
    echo $y;
    ?>
    </td></tr></table>

    2. Staying in PHP

    <?php
    echo '<table><tr><td>'.$x.'</td><td>'.$y.'</td></tr></table>';
    ?>

    Would one of these be faster than the other?

    Thanks.
     
    CurryMan, Oct 15, 2009 IP
  4. almondj

    almondj Peon

    Messages:
    768
    Likes Received:
    11
    Best Answers:
    1
    Trophy Points:
    0
    #4
    I've assumed the latter is the better for all coding.

    Mostly because this:

    BEGIN PHP
    ECHO HTML
    END PHP

    VS.

    BEGIN HTML
    END HTML
    BEGIN PHP
    END PHP
    BEGIN HTML
    END HTML
     
    almondj, Oct 15, 2009 IP
  5. CurryMan

    CurryMan Peon

    Messages:
    22
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Okay, thanks for the advice guys.

    The slowness in the page is probably because of frequent SQL queries, sometimes hundreds in one page, but if modifying the way the PHP is written will speed it up at all, I'll give it a go.

    Cheers.
     
    CurryMan, Oct 16, 2009 IP
  6. premiumscripts

    premiumscripts Peon

    Messages:
    1,062
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Mate, keep your PHP / HTML readable (first form). This will hardly have any effect at all. It's probably immeasureable.. As you said, slowness is due to other causes such as DB.
     
    premiumscripts, Oct 16, 2009 IP
  7. ThomasTwen

    ThomasTwen Peon

    Messages:
    113
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Nobody needs that kind of queries. You are probably able to reduce this amount by more efficient coding and sql usage. Otherwise, you need to change your database structure.
     
    ThomasTwen, Oct 16, 2009 IP
  8. markowe

    markowe Well-Known Member

    Messages:
    1,136
    Likes Received:
    26
    Best Answers:
    0
    Trophy Points:
    165
    #8
    Agreed, compared to other factors like database access this one would have an absolutely negligible effect on speed of execution. You will see big-league PHP code that switches in an out of PHP a huge amount like this. Although technically it may be quicker to echo HTML from PHP than to switch out of PHP, the latter way is a lot neater in my opinion, than echoing it, and this may also be considered the better practice since you are maintaining a separation between code and output.

    I am no expert, but there are ways and means to significantly optimise SQL code - do you really need multiple queries where one could do the job? Are the fields you are searching/sorting on indexed? Are you importing large amounts of data into the dbase which could be imported using LOAD instead of INSERT..? I'm sure there are plenty more things you can try too.
     
    markowe, Oct 19, 2009 IP