5

I would like to initialize my variables at the top of my file, to prevent any Undefined variable notices. But what do you guys think is considered to be best practice (in PHP), in case of a string type variable?

Initialize the variable with a value false by default?

$variable = false;

Or perhaps just an empty string

$variable = '';

Or even a null value

$variable = null;
  • Possible duplicate of Returning null or a empty value/throw exception? – gnat Jan 25 '17 at 9:46
  • 1
    Well I've seen that post, but it's about return types. But I'm just wondering, in case of initializing string variables, what about that? Is it "better" to start with null, false or even '' Cause I think the return type of a method quite depends on the context, and actually I always return a boolean, throw an error or return an actual value. But it doesn't answer my question about initializing values, actually – Erik van de Ven Jan 25 '17 at 9:48
15

If you don't have a sensible value to initialize your variable with, then you should not be creating that variable at that point in your code.

If you are getting notices that you are using an undefined variable, then that is a clear indication that you have a problem in your program flow. The correct course of action here is to give those variable a sensible value before they are being used. If such a sensible value doesn't exist, then you need to ask yourself why the variable is being used in the first place.

Just blindly masking Undefined variable notices is not going to make the real problem go away. The Undefined variable notice is only a side-effect of that real problem.

| improve this answer | |
  • Thanks, haven't really thought about that at first, but makes a lot of sense actually. Thank you for your explanation. – Erik van de Ven Jan 25 '17 at 13:12
  • Right, PHP gives you that warning when you try to evaluate and expression with an undefined variable. If you're saying $a = $b + $c when you never gave $c a value, there's very likely something wrong with your code. The warning is there so you know you've done something that's probably wrong. Setting $c to a null value at the top of the file is just sweeping the problem under the rug. – Sean McSomething Jan 25 '17 at 19:50
0

I'd have added a comment rather than an answer because I don't know php, but cannot do that yet. I'll speak generally from experience with other languages. The point of initializing a variable is that you want it to have a default value, unless otherwise specified. If there isn't a useful default value from the outset and you'd rather indicate that no value has been assigned yet, then you use some value consistently.

One advantage that I see in choosing null over "" is that an empty string is still a valid string, whereas null clearly tells us that no value has been assigned.

Setting the value to FALSE sounds wrong to me, since that would signal people reading your code that the variable is a boolean, rather than a string.

Whatever you choose, remember that in the end it's about how you use the variable. That is, if you intend to have some error handling code, then choose your values accordingly. Don't just assume that because you've initialized the variable, everything will be alright. Always think about all the scenarios and code paths and what would happen if the default (initialized) value were to be used.

Finally, if you're working in a team, check your company or team guidelines. It's so much better when everyone uses the same practice, even if not everybody agrees with everything!

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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