note 35184 deleted from language.variables by aidan
| From: | aidan@php.net | Date: | Sat, 18 Sep 2004 04:44:54 +0000 |
| Subject: | note 35184 deleted from language.variables by aidan | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-76919@lists.php.net to get a copy of this message | ||
Note Submitter: c dot hester at ukonline dot co dot uk
----
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.