2

I am fairly new to PHP (less than a year), and to improve my development environment, I recently started using NetBeans IDE.

A warning keeps popping up everywhere stating that "Variable might not have been initialized".

I'll give an example of a variable that results in this hint/warning:

$start = $per_page * $page;

My question is: How can I initialize a PHP variable? Also, how important is it that a variable is initialized in PHP?

I appreciate any advice you can provide.

Note: I tried to place the following code above my variable, to no avail.

$start = '';
7
  • 7
    The warning is suggesting that $per_page or $page might not have been initialized. You're initializing $start to the product of these variables, but were these two variables initialized? Commented Oct 24, 2011 at 18:32
  • 4
    Have you initialized $per_page and $page? Commented Oct 24, 2011 at 18:33
  • 1
    Are you sure Netbeans is referring to a PHP variable; I normally only see that error if I forget var when using JavaScript; you've not got a mix of PHP and JS in the document? $start = ''; would initialise the variable... (unless it's in class scope but then you'd get a syntax error). Commented Oct 24, 2011 at 18:35
  • I see. @CD001 - No I don't have a mixture of PHP and JS in this document. @BoltClock, @Jonathan - I have not initialized $per_page or $page. I will try that. Is my syntax correct for initialization? e.g. $per_page = '';? Commented Oct 24, 2011 at 18:40
  • @CD001 - Thanks a bunch! Much appreciated. ALSO, anyone else noticing that Stack Overflow's image sprite isn't working correct? I'm not getting a lot of the up and down arrows to the left of questions and comments. I checked out the code, and it would seem that they are using a new version (v4) of the sprite which seems to be missing half of the images. Unless this is just my browser... Commented Oct 24, 2011 at 18:43

1 Answer 1

2
$foo ='';

That’s how you initialize a variable. So you are correct.

$start = $per_page * $page;

For the above code, if one of the variables on the right side of the equation is not being initialized anywhere in the code, your IDE will complain thinking that they might be null. You might want to initialize them on separate lines to see if you will get the same warning.

Unlike for Java- and C#-like languages, in which you get lot of null pointer exceptions, the same thing doesn’t count for PHP. PHP is weakly-typed language, so you won't get any null pointer exceptions.

$start = $notinitiliazedvar;

This will basically have a default value.

<?php
class Foo{
    public $name;
    public $id;

    function __construct(){

    }

    public function toString(){
        return "{$this->name}, {$this->id}";
    }
}

$f = new Foo();
$f->name = $test;

echo $f->name;

?>

You won't get any output with this code. So it’s OK. It’s just your IDE being paranoid.

$test = 1;
echo $test;
$test = "test";
echo $test;
Sign up to request clarification or add additional context in comments.

9 Comments

Thanks. So what you're saying is that while it doesn't hurt to initialize my variables, it also isn't integral when using PHP?
You sould initialize the var s whatever type you are expecting not necessarily an empty string. In this cas i have to assume page and per page are going to be integers so i would use 0, though null might also be acceptable if you want to check for initialization first.
well, you need to initialize what you need to initialize, php is very flexible language (weird IMO:)).
@stefmikhail: Its not integral. But it is good practice - depending on the error level you have set you will get warnings in your log.
@prodigitalson - I see, so if my variable will have an integer value, then initialize it as 0, unless I need it be a different integer. And if the variable will have a string value, then use ''?
|

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.