note 35184 added to language.variables
| From: | c dot hester at ukonline dot co dot uk | Date: | Fri, 22 Aug 2003 14:23:03 +0000 |
| Subject: | note 35184 added to language.variables | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-54872@lists.php.net to get a copy of this message | ||
Here's a simple way to speed up scripts considerably.
I made a page that differs depending on a variable passed in the address. I found that if I checked
the variable was blank several times the page took longer to create. Whereas if I set the variable
to a default word, the speed jumped from 1.5 seconds to 0.5. (This matched the speed of the page
when the variable was defined already by the address.)
Example - the link might be any of these:
test.php?a=page
test.php?a=page&value=yellow
test.php?a=page&value=red
In the first link I left the variable off so the default page appears. I didn't want to add it
to the link as it makes the address unnecessarily long. (The page is always accessed from the
default link.)
The code used was like this:
if ($value == "") echo "<h1>Default page</h1>";
if ($value == "yellow") echo "<h1>Yellow page</h1>";
if ($value == "red") echo "<h1>Red page</h1>";
I used the variable $value several times in a set of small loops. To speed the page up, all I did
was assign the variable at the top of the page like this instead of leaving it blank:
if ($value == "") $value = "default";
Then I amended my code like this:
if ($value == "default") echo "<h1>Default page</h1>";
if ($value == "yellow") echo "<h1>Yellow page</h1>";
if ($value == "red") echo "<h1>Red page</h1>";
I was amazed at the speed increase.
----
Manual Page -- http://www.php.net/manual/en/language.variables.php
Edit Note -- http://master.php.net/manage/user-notes.php?action=edit+35184
Delete Note -- http://master.php.net/manage/user-notes.php?action=delete+35184&report=yes
Reject Note -- http://master.php.net/manage/user-notes.php?action=reject+35184&report=yes