Seva's notes

Wisdom, Awareness, Responsibility :)

Variable declaration in PHP

with 2 comments

Today while playing around PHP and stuff I have been thinking of patterns of handling optionally existing variables.

The 2 most commonly used ones are:


if (isset($var) && $var != null) doSomething($var);
// or
if (isset($var) && $var != 0) doSomething($var);
// or
if (isset($var) && ($var != '') doSomething($var);

if (!isset($var)) $var = something();

However my experience points me that in the majority of the cases it’s enough to check the boolean value of the variable to proceed:


if ($var) doSomething($var);

if (!$var) $var = something();

Which reads very kosher. But then we have a problem of getting notice that the variable is undefined, which is not kosher.

So the way kosherize the snippet is to enforce variable definition before usage. Meaning we would like to either use its value if it’s defined, or to initialize it with null. But of course I don’t like to use isset() for that. So what to do? I found an answer!


$var = &$var;

As you can see in this way you declare further usage of the variable. If the variable exists – it does nothing. If it’s not it is silently initialized with null. No notices are yielded. Also it’s better than if(isset()) combination because of readability – the only alphanumeric tokens on the line are variable names.

Isn’t it nice?

Written by Seva

2009-09-28 (September 28) at 02:21:10

Posted in development, php, thought

Tagged with

2 Responses

Subscribe to comments with RSS.

  1. First, thanks for the blog. It’s really helpful to see what other PHP developers are doing.

    Second, this is a classic problem for all programming languages. Your solution is indeed clever, I can’t help wondering if you’ve merely pointed out a problem in the way PHP does its error reporting.

    I guess I’m semantically uncomfortable with this construct. You are counting on side-effect of dereferencing. I suppose I’d like a cleaner language construct (like an operator) dedicated to this purpose that’s more efficient that isset().

    Can’t one sidestep this issue of uninitialized variables entirely by turning off manner of automatic variable registration and using error_reporting(E_STRICT)?

    Anyway, thanks for the post.

    Joe J.

    2010-02-03 (February 3) at 05:24:34

    • The problem is it’s impossible to disable throwing error/warning/notice events in PHP configuration. It’s only possible to disable reporting them.

      Also, not to forget that variable initialization issue explained is a sign of bad software design – it’s more convenient to always use strict contract for passing variables (function/method calls) to execute modular code instead of plain include/require strategy.

      Seva

      2010-02-03 (February 3) at 05:34:41


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: