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 7 of 7
  1. #1
    Regular Coder
    Join Date
    Mar 2005
    Location
    Brighton, UK
    Posts
    117
    Thanks
    0
    Thanked 0 Times in 0 Posts

    <?php ?> Performance Question

    Hi,
    I have been working on a project recently and I decided to go down the simplest route I could think of in order to separate all my PHP code from the HTML, that is, use however many scripts I need to generate the data, then parse that data to a function that just includes a template file (just a php file that is mainly HTML with a few <?php echo … ?> in), the output is buffered using the ob_ functions and the resulting html is sent wherever it needs to be, simple.

    But I was thinking, is having a php file with loads of <?php ?> tags in bad for performance? I remember someone telling me that each time you open and close the tags it takes a performance hit, is this true? And if so, is it actually a major performance hit, or insignificant?

    For example, in these two examples is the first one better for performance (even though it is harder to follow and edit):

    PHP Code:
    <?php 
        
    echo '<p>My name is '.$name.' and I am '.$age.' years old.</p>';
    ?>
    PHP Code:
    <p>My name is <?php echo $name ?> and I am <?php echo $age ?> years old.</p>
    Thanks for your help,
    Jack

  • #2
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,073
    Thanks
    11
    Thanked 98 Times in 96 Posts
    There is no performance hit, the first part of the php parsing process is tokenizing, this decides what parts of your code are actually PHP code and what parts are HTML etc, this part is done all at once, there is no 'jumping in and out' of php mode etc.
    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)

  • #3
    Regular Coder
    Join Date
    Mar 2005
    Location
    Brighton, UK
    Posts
    117
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Cool, thanks!

  • #4
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    I've run a test
    This
    PHP Code:
    <?php  
        
    echo '<p>My name is '.$name.' and I am '.$age.' years old.</p>'
    ?>
    will get you a ~0.001second gain after 10000 iterations.
    (PHP5.1.2 with Zend Optimizer 3.0)
    I'm not sure if this was any help, but I hope it didn't make you stupider.

    Experience is something you get just after you really need it.
    PHP Installation Guide Feedback welcome.

  • #5
    Regular Coder
    Join Date
    Mar 2005
    Location
    Brighton, UK
    Posts
    117
    Thanks
    0
    Thanked 0 Times in 0 Posts
    will get you a ~0.001second gain after 10000 iterations
    lol, well, thats that answered , nice one, cheers

  • #6
    Senior Coder missing-score's Avatar
    Join Date
    Jan 2003
    Location
    UK
    Posts
    2,194
    Thanks
    0
    Thanked 0 Times in 0 Posts
    However, to my knowledge, it is best to place very large sets of data outside of PHP tags rather than echoing them.. Ive never actually tested it but it does make sense. Please shoot me if I'm wrong

    The other thing worth noting is that you can't properly test performance using such a small set of data, as an example:

    In an application I am working on for a client, An e-mail template needs to be read from a file and then sent. The file may be need to be read 1 - 20 times, depending. I created a class with a static function that would (when called), open the file file specified, cache it, and return the contents.. If the cache was already present it would return the cached file contents to save opening the file again.

    I found that when then file contained less than 20 characters it was actually quicker to read the file multiple times than check to see if the file had been cached and then return the stored data. However when I tested an 800k file, the performance increase was immense, from about 2 seconds reading the file 10 times to 0.15 seconds with the cache.

    Sorry to ramble... What i'm basically saying is, you can't judge performance properly with such a small test, however if thats all you need it for then its best to go with whicever method is most cumfortable for you to use.

  • #7
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    I've used a small amount of data becouse that is what you usually echo. Echoing a large block of data (markup of this page) was done 10x slower by echoing it out 100 thanby "jumping in and out" of PHP.
    Though when the echoed value got stored in a variable it was faster (more than 10x) I'm pretty sure you don't output the exact same code blocks houndreads of times though. By printing it once the gain loss would be ~5*10^-6seconds
    Tests show that you should not worry about speed.
    I'm not sure if this was any help, but I hope it didn't make you stupider.

    Experience is something you get just after you really need it.
    PHP Installation Guide Feedback welcome.


  •  

    Posting Permissions

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