开发者

Code optimization in PHP

  1. I need 3 temporary variables is there any difference between this codes ? $temp1, $temp2, $temp3开发者_开发问答 vs $temp[0],$temp[1],$temp[2];

  2. If i have a variable name $X in page1.php and in page2.php and then i include page2.php in page1.php, is the variable $X going to be overwritten ? can i stop this ?


Generally speaking, arrays and individual variables shouldn't have any size or performance drawback in a low-level language like C/C++. In these languages arrays are just a chain of data types back-to-back in memory.

Because PHP has more advanced array constructs which enable you to do some extra things which you can't do in most low-level languages such as determining the length of the array, it is likely that the interpreter will introduce a size and/or speed overhead to array operations, however for modest-sized I'd say this would be negligible.

If optimisation is high on your list of priorities, then maybe you should consider a different programming language. PHP is not known for its bleeding-edge performance.

If you have a global variable $X in page1.php and include page2.php which also defines a global variable $X, then they will be the same global $X variable. I do not believe there is any way to avoid this.

You shouldn't be defining $X more than once in several pages to begin with. Why not just give them different names?

Also, something which might lead to a similar situation: When you have a situation where page1.php includes page2.php and page3.php, and page 3.php includes page2.php also, it's important to use the require_once() function to make sure pages are only included once.


First, there isn't enough of a difference to really need to choose between one or the other.
Second, it depends on the scope of each variable, if they are defined inside say a loop, then no one wouldn't overwrite the other. Otherwise yes. Here is a good bit on variable scope: http://php.net/manual/en/language.variables.scope.php


An interpreter has to reevaluate the expressions it processes every time. $temp1[0] will very likely take longer than $temp1 to evaluate, as a consequence. Whether this matters in a visible way to the execution of program depends on how many times this is executed; if just a few, you won't be able to realistically measure the difference; if millions of times, you might be able to visibly see the difference.

If you were using a really good, optimizing compiler, it might make no difference in any case, as a good compiler can see you are referencing a unique location.

As a general rule, I'd write $temp1 rather than $temp[1] just because it is less to type, easier to read, and generally runs faster; what's not to like?

Regarding your $X question: the value of $X at global scope won't be overwritten by the include. If the include has an assignment to $X, the act of "including" it causes the assignment to occur and $X will be overwritten.


Using $temp[0] would be better if you plan on expanding the amount of temporary variables in the future. Also, if you decide to do some operation on all of the variables then a simple foreach() loop would work, but would be much harder if you used the other method of $temp0, $temp1, etc. Therefore, use $temp[0] and protect yourself for the future.

I am pretty sure that $X would be overwritten and you could create classes or possibly use a temporary variable?

(I am not sure about my answer to the second part, but am sure about the first part.)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜