...

View Full Version : Factors affecting PHP resource usage



PonchoX
02-02-2010, 12:57 AM
hi.

i'm learning PHP. Early stages.

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


<?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 echo "Hello world!" ?>
HTML Mark-up.
<?php echo "Hello world!" ?>
More HTML Mark-up
<?php echo "Hello world!" ?>


Rather than:


<?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?

masterofollies
02-02-2010, 01:40 AM
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
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.

MattF
02-02-2010, 02:22 AM
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:



<?php echo $1; ?>Text here<?php echo $2; ?>More text here...etc


when you can easily use concatenation:



<?php echo $1.'Text here'.$2.'More text here...etc


That might just be me though. :D

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

PonchoX
02-02-2010, 02:58 AM
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.

masterofollies
02-02-2010, 04:40 AM
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

PonchoX
02-02-2010, 05:07 AM
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.

Fou-Lu
02-02-2010, 05:30 AM
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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum