var is a language structure used in PHP to declare and initialize variables. It is used to specify the name and type of the variable. It can also cast variable types via the var_dump() function (PHP 7.4 and above).
Usage of var in PHP
What is var?
var is a language construct in PHP used to declare variables. It tells PHP that you are creating a variable and specifying its type.
Syntax
<code class="php">var <variable_name> = <value>;</code>
Where:
<variable_name>
is the name of the variable<value>
is the value of the variableExample
<code class="php">var $age = 30;</code>
This will create a file called $age
variable and set its value to 30.
Uses of var
var is used for:
var_dump()
) Coercion
Use # in var ##var_dump() You can force a variable to a specific type. For example:
<code class="php">var_dump((int) "123"); // 输出:int(123) var_dump((float) "3.14"); // 输出:float(3.14)</code>
Note: Casting is limited to PHP 7.4 and higher. In older versions, it will have no effect.
The above is the detailed content of How to use var in php. For more information, please follow other related articles on the PHP Chinese website!