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
    New Coder
    Join Date
    Apr 2009
    Location
    Newport Beach, California
    Posts
    96
    Thanks
    11
    Thanked 1 Time in 1 Post

    Factors affecting PHP resource usage

    hi.

    i'm learning PHP. Early stages.

    I'm curious. Say we have 3 separate statements. Could be anything, such as:

    PHP Code:
    <?php echo "Hello world!" ?>
    Would it consume more server resources to put these statements in 3 separate scripts through our HTML mark-up, such as:

    PHP Code:
     <?php echo "Hello world!" ?>
    HTML Mark-up. 
    <?php echo "Hello world!" ?>
    More HTML Mark-up 
    <?php echo "Hello world!" ?>
    Rather than:
    PHP Code:
    <?php echo "Hello world!"
    echo "Hello world!"
    echo "Hello world!" ?>
    Speaking purely theoretically. I realize that, practically speaking, these different examples would make little sense and therefore NOT be interchangable.

    In other words, it seems as tho in the later example, the PHP interpreter gets turned on (and OFF) only once.
    Whereas in the first example, it get turned on & off 3 times. Would the former therefore consume more server resources?

    And do we therefore try to minimize interpreter start ups? For the sake of resource usage?
    Last edited by PonchoX; 02-01-2010 at 11:59 PM.

  • #2
    Senior Coder
    Join Date
    May 2005
    Posts
    2,137
    Thanks
    96
    Thanked 72 Times in 72 Posts
    This is something that many web designers argue on.

    One thing I do know for certain is, use single quotes if you aren't using a variable.

    PHP Code:
    <?php
    echo 'Hello World';
    ?>
    This will run faster so it doesn't look for the variable. I find using 100 echos may slow it, rather than using 5 big ones.

    HOWEVER the difference is so little. I don't think it changes the speed much at all maybe like 0.0002 faster.
    Rowsdower! has accused me of having mental problems, and the administrator allowed it. What a great forum huh?

  • #3
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    As to whether any real world difference would be apparent or not would depend, but less is best each and every time. No use switching in and out of PHP mode when you can do it within one statement or concatenate the strings. One thing which you see a lot of which I personally dislike is something along the lines of the following:

    Code:
    <?php echo $1; ?>Text here<?php echo $2; ?>More text here...etc
    when you can easily use concatenation:

    Code:
    <?php echo $1.'Text here'.$2.'More text here...etc
    That might just be me though.

    Use single quotes rather than double too, as mentioned above.

  • #4
    New Coder
    Join Date
    Apr 2009
    Location
    Newport Beach, California
    Posts
    96
    Thanks
    11
    Thanked 1 Time in 1 Post
    thanks.

    i did not know about using single quotes when no variables present (as my example demonstrates), tho this makes sense, cuz i know you need double quotes for variable substitution to work.

    and i see what you mean about including your html-text INSIDE the php script. i will watch for that.

    appreciate your comments. these little things help.

  • #5
    Senior Coder
    Join Date
    May 2005
    Posts
    2,137
    Thanks
    96
    Thanked 72 Times in 72 Posts
    Also remember that when using PHP in a file. You cannot name it like index.html or else php will be plain text. You need to save it as index.php
    Rowsdower! has accused me of having mental problems, and the administrator allowed it. What a great forum huh?

  • #6
    New Coder
    Join Date
    Apr 2009
    Location
    Newport Beach, California
    Posts
    96
    Thanks
    11
    Thanked 1 Time in 1 Post
    oh yeah, i knew that. but thanks for mentioning it.

    i'm using this vdo series taught by a guy named kevin skoglund:

    http://www.lynda.com/home/DisplayCourse.aspx?lpk2=435

    i really like his style, especially how he spells things out. he doesn't assume the listener knows very much.

  • #7
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Resource wise... I wouldn't expect either with or without the markup to cause any problems. Time wise, the markup will take a bit more time simply because you're jumping into and out of processing. It would probably be faster to actually print / echo you're html out instead of breaking in and out. PHP is pre-processed though, so if there is a difference it would be negligable.
    Measuring the time on these is probably impossible though.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)


  •  

    Posting Permissions

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