38

Is it necessary to initialize / declare a variable before a loop or a function?

Whether I initialize / declare variable before or not my code still works.

I'm sharing demo code for what I actually mean:

$cars = null;

foreach ($build as $brand) {
     $cars .= $brand . ",";
}

echo $cars;

Or

foreach ($build as $brand) {
     $cars .= $brand . ",";
}

echo $cars;

Both pieces of code works same for me, so is it necessary to initialize / declare a variable at the beginning?

3 Answers 3

50

PHP does not require it, but it is a good practice to always initialize your variables.

If you don't initialize your variables with a default value, the PHP engine will do a type cast depending on how you are using the variable. This sometimes will lead to unexpected behaviour.

So in short, in my opinion, always set a default value for your variables.

P.S. In your case the value should be set to "" (empty string), instead of null, since you are using it to concatenate other strings.

Edit

As others (@n-dru) have noted, if you don't set a default value a notice will be generated.

5
  • Null and empty ""... aren't they same?
    – Omer
    Jun 20, 2015 at 15:31
  • 6
    No, they are not the same. One is of type string, the other is null. You can see the difference when playing around with the is_null function. is_null($null) // Returns true and generates a notice empty("") // Returns true
    – Alexander
    Jun 20, 2015 at 15:35
  • So you mean NULL = Unassigned and "" = an empty STRING?
    – Omer
    Jun 20, 2015 at 15:45
  • I don't think there is such a thing as unassigned variables in PHP. Variables are created the first time you use them. If you use a variable that is not yet created it has a null value by default. "" is an empty string yes. You should be aware that the empty function returns true in a couple of cases (empty string, false, null, unexisting variable).
    – Alexander
    Jun 20, 2015 at 15:59
  • 1
    Since PHP 8 If you don't set a default value a "warning" will be generated! More details: php.net/manual/en/migration80.incompatible.php Jul 15, 2021 at 19:00
21

You had better assign it an empty string $cars = '';, otherwise (in case you have error reporting on) you should see a notice:

Notice: Undefined variable: cars

PHP will assume it was empty and the resultant string will be the same, but you should prefer not to cause an extra overhead caused by a need of logging that Notice. So performance-wise it is better to assign it empty first.

Besides, using editors like Aptana, etc, you may wish to press F3 to see where given variable originated from. And it is so comfortable to have it initialized somewhere. So debugging-wise it is also better to have obvious place of the variable's birth.

0
0

It depends: If you declare a variable outside a function, it has a "global scope", that means it can only be accessed outside of a function.

If a variable is declared inside a function, it has a "local scope" and can be used only inside that function. (http://www.w3schools.com/php/php_variables.asp)

But it seems that the variable "cars" that you defined outside the function works for your function even without the global keyword...

1
  • 7
    firstly note that there is no function declared in the code posted. So, there is no scope of having global keyword. You should understand what is asked before answering a question. Also what you have written is quite misleading. I would ask you to refer to php.net/manual/en/language.variables.basics.php to understand php variables.
    – Blip
    May 8, 2017 at 9:55

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.