Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP array vs PHP Constant?

I am curious, is there any performance gain, like using less memory or resources in PHP for:

50 different setting variables saved into array like this

$config['facebook_api_secret'] = 'value here';

Or 50 different setting variables saved into a Constant like this

define('facebook_api_secret', 'value here');
like image 556
JasonDavis Avatar asked Jan 17 '10 05:01

JasonDavis


2 Answers

I think this is in the realm of being a micro-optimization. That is, the difference is small enough that it isn't worth using one solution over the other for the sake of performance. If performance were that critical to your app, you wouldn't be using PHP! :-)

Use whatever is more convenient or that makes more sense. I put config data into constants if only because they shouldn't be allowed to change after the config file is loaded, and that's what constants are for.

like image 144
Bill Karwin Avatar answered Sep 21 '22 00:09

Bill Karwin


In my informal tests, I've actually found that accessing/defining constants is a bit slower than normal variables/arrays.

it's not going to make a different anyway; more than likely whatever you do with these will happen in thousandths of a second.

Optimizing your DB (indexing, using EXPLAIN to check your queries) and server set up (using APC) will make more a difference in the long run.

like image 24
JAL Avatar answered Sep 23 '22 00:09

JAL