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?
+
PHP doesn't have an explicit statement to initialise variables, like Javascript'svar
. 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" isnull
, 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.)
Thevar
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 thevar
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 thefoo = 69
creates a new local variable insidebar
here. To allow assignment to the variable from an inherited scope, thenonlocal
orglobal
keyword must be used to explicitly denote that variable as inherited.foo = 2
here reassignsfoo = 69
, but neither reassignsfoo = 42
.
There is novar
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 tonull
in PHP, just use:
$myVar = null;
In class context you can make it without explicitnull
:
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)
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
JavaScript is a multi-paradigm language that supports event-driven, functional, and mandatory (including object-oriented and prototype-based) programming types. Originally JavaScript was only used on the client side. JavaScript is now still used as a server-side programming language. To summarize, we can say that JavaScript is the language of the Internet.
https://www.javascript.com/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.