1
  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 ?

4 Answers 4

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

4 Comments

Variables defined inside a loop would overwrite each other.
I meant if the first was defined inside a loop, and the second defined as a global, or in a different loop
variables defined in a loop in php are not local to the loop. They will be in the scope of whatever the loop is in. A loop in the global scope will create a global variable.
Well, I feel stupid; I was thinking C#, you are correct (and I didn't even know that rule in php, that variables defined in a loop will take the loop's scope). Good to know.
0

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.

Comments

0

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.)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.