468,257 Members | 1,431 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,257 developers. It's quick & easy.

When does assigning variables become more efficient than $_POST

This may not be a big deal, but I'm just kinda curious. If I have a
page that submits a form to itself, when is it better to assign each
element in $_POST to a variable to use for the rest of the page?

My situation probably doesn't make a difference, but say I have about
10 form fields, and when the page is posted back to itself, i call
each of those form fields 2 times (1 for error checking, 1 for SQL
insert, or to pre-fill form).

So where does it become more efficient to only call $_POST once and
assign those to variables? Do you use the variables if they are
needed any more than one time?

Thanks for you thoughts!
Jul 16 '05 #1
1 2573
On 11 Jul 2003 14:35:43 -0700, sc******@cableone.net (dSchwartz) wrote:
This may not be a big deal,
It isn't. :-0
but I'm just kinda curious. If I have a
page that submits a form to itself, when is it better to assign each
element in $_POST to a variable to use for the rest of the page?

My situation probably doesn't make a difference, but say I have about
10 form fields, and when the page is posted back to itself, i call
each of those form fields 2 times (1 for error checking, 1 for SQL
insert, or to pre-fill form).

So where does it become more efficient to only call $_POST once and
assign those to variables? Do you use the variables if they are
needed any more than one time?


If you're bothered about it, benchmark it.

<pre>
<?php
function getmicrotime(){
list($usec, $sec) = explode(" ",microtime());
return ((float)$usec + (float)$sec);
}

$time_start = getmicrotime();
for ($i=0; $i<100000; $i++) {
$dummy = $_GET['x'];
}
$time_end = getmicrotime();
$time = $time_end - $time_start;
echo "Using \$_GET: $time seconds";

print "\n";

$value = $_GET['x'];

$time_start = getmicrotime();
for ($i=0; $i<100000; $i++) {
$dummy = $x;
}
$time_end = getmicrotime();
$time = $time_end - $time_start;
echo "Using variable: $time seconds";

?>
</pre>

On my creaky old P200 Linux server this comes out with:

Using $_GET: 1.2699910402298 seconds
Using variable: 1.0599220991135 seconds

That's if you use the value 100,000 times, this saves a grand total of just
over 0.2 seconds.

But what are the odds of you using a form variable that many times? And it's
almost certain you can get speed increases of many orders of magnitude greater
by improving your algorithms rather than shaving milliseconds off by avoiding
array access.

--
Andy Hassall (an**@andyh.co.uk) icq(5747695) (http://www.andyh.co.uk)
Space: disk usage analysis tool (http://www.andyhsoftware.co.uk/space)
Jul 16 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Phil Powell | last post: by
7 posts views Thread by juglesh | last post: by
8 posts views Thread by Ian Davies | last post: by
3 posts views Thread by Taras_96 | last post: by
8 posts views Thread by =?Utf-8?B?VHJlY2l1cw==?= | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by kermitthefrogpy | last post: by
reply views Thread by zattat | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.