Is it possible to declare empty variable in PHP?

Is there any explicit way in PHP to declare variable without assigning any value? Similarly to Java or Javascript? Something like this.


I assume that default value for this variable would be {-code-2}. If it's not possible, is there any logical explanation why it wasn't implemented in PHP?

There are many articles with tables depicting boolean results of {-code-3}, {-code-4}, etc. And there is also an example of value {-code-5}, but does it make sense to list it?

+

Answer

Answer

Answer

Answer

Answer

----+

Answer

----+

Answer

---+

Answer

-----+ | Value of variable ($var) | {-code-3}($var) | {-code-4}($var) | is_null($var) | +

Answer

Answer

Answer

Answer

Answer

----+

Answer

----+

Answer

---+

Answer

-----+ | {-code-2} | bool(false) | bool(true) | bool(true) | | {-code-5} | bool(false) | bool(true) | bool(true) | +

Answer

Answer

Answer

Answer

Answer

----+

Answer

----+

Answer

---+

Answer

-----+

https://www.virendrachandak.com/demos/php-isset-vs-empty-vs-is_null.php

Answer

Answer

Answer

Answer

Answer

----+

Answer

----+

Answer

---+

Answer

-----+ | Value of variable ($var) | isset($var) | empty($var) | is_null($var) | +

Answer

Answer

Answer

Answer

Answer

----+

Answer

----+

Answer

---+

Answer

-----+ | NULL | bool(false) | bool(true) | bool(true) | | var $var; (a variable declared, but without a value) | bool(false) | bool(true) | bool(true) | +

Answer

Answer

Answer

Answer

Answer

----+

Answer

----+

Answer

---+

Answer

-----+

Answer

Solution:

PHP doesn't have an explicit statement to initialise variables, like Javascript's var. PHP variables get initialised when they're first assigned to. It's a design choice, for better or worse. To create a variable you always have to assign something to it. Since a variable must have some value and PHP's value for "nothing" is null, you want:

$variable = null;

(Alternative ways to create variables exists, like references, e.g.: parse_str($foo, $bar); var_dump($bar);, but let's leave that aside.)

The var statement exists in Javascript to determine scope; Javascript has nested scopes and needs an explicit initialisation to determine what a variable's scope is. Scoping in PHP works differently and doesn't have that ambiguity, hence such a separate statement isn't necessary. Originally PHP also liked implicit globals a lot (a terrible idea in hindsight), which somewhat clashes with the idea of explicit initialisation statements in the first place.

var $var; (a variable declared, but without a value)

That is an incomplete/incorrect quote, it should be "a variable declared but without a value in a class", since that's the only place the var keyword can be used.


As an example, Python also uses initialisation-by-assignment, and also has nested scope. It uses a different approach to resolve this:

foo = 42

def bar():
    foo = 69

    def baz():
        nonlocal foo
        foo = 2

The rule in Python is, that a variable is local to a function if any assignment is done to the variable inside the function. So the foo = 69 creates a new local variable inside bar here. To allow assignment to the variable from an inherited scope, the nonlocal or global keyword must be used to explicitly denote that variable as inherited. foo = 2 here reassigns foo = 69, but neither reassigns foo = 42.

Answer

Solution:

There is no var keyword which works the same way as in javascript. From w3schools:

Note: Unlike other programming languages, PHP has no command for declaring a variable. It is created the moment you first assign a value to it.


Your other question:

If it's not possible, is there any logical explanation why it wasn't implemented in PHP?

One logical explanation could be that in PHP all variables start with a $ symbol. So PHP immediately knows when something is a variable and not a function or keyword, without the need for a declaration.

In fact, javascript code also works without declarations, but here, they are still recommended in most cases.


If you want to declare a variable and set its value to null in PHP, just use:

$myVar = null;

Answer

Solution:

In class context you can make it without explicit null:

class BarClass {
    private FooClass $foo;

As simple variable, you can't. Use $variable = null (or do not write it at all). As all not set vars are null by default:

$var1 = null;

var_dump(isset($var1), empty($var1), is_null($var1));
// bool(false), bool(true), bool(true)

var_dump(isset($var2), empty($var2), is_null($var2));
// Notice:  Undefined variable: var2 - From `is_null`
// bool(false), bool(true), bool(true)

Source