user2236124
user2236124

Reputation:

What will load faster. Converted Variables or $_POST variables?

I'm fairly new to PHP and still don't know some of the most basic basics of PHP so bear with me.

When writing a script; IE: (Plz ignore syntax errors)

if isset($_POST['name']) {$Name = $_POST['nsme'];}

When using this name in the page, which way is better and loads faster??

A:) echo $Name. ' went to the river';

B:) echo $_POST['name']. 'went to the river';

Obviously this is a fictional example, I'm just wondering which way is better whether it be an echo or any other function and if anyone wouldn't mind chiming in on this, I'd be most appreciative and I thank you all once again.

Upvotes: 1

Views: 108

Answers (5)

NullPoiиteя
NullPoiиteя

Reputation: 57322

If you care about speed don't worry you can use any of them ,deference significantly low but creating variable which is used only once is not a good idea

However if you are doing

$Name = $_POST['nsme'];

and using $name variable i am sure you want to read about Singleton variable

and if you are using $name at other place too its perfectly

Upvotes: 1

Narek
Narek

Reputation: 3823

If you have var $foo and make:

$bar = $foo;

It doesn't create another copy of $foo in memory until you change $foo or $bar so you will have almost same var for both.

You will have same speed, but $bar will look better than $_POST['bar'] and with it easier to work.

Upvotes: 0

Kapil Sharma
Kapil Sharma

Reputation: 10417

echo $_POST['name'].' went to the river'; 

Will be faster as you are skipping one step.

However if you need to use $_POST['name'] multiple times, second approach will be better.

Upvotes: 1

bwoebi
bwoebi

Reputation: 23787

A direct variable access is always faster. At least the zend lexer has not recognize for dimensions...

At least for multiple uses...

(use always an isset()-check at least else you'll have notices.)

Upvotes: 0

chandresh_cool
chandresh_cool

Reputation: 11830

Obviously

echo $_POST['name'].' went to the river'; 

would be faster, as you are skipping one step of assigning the post variable to a php variable.

Upvotes: 1

Related Questions