Advertisement
Google Ad Slot: content-top
PHP Variables
Variables are used to store data that can be manipulated and retrieved during the execution of a script. Variables in PHP are easy to use, dynamically typed (meaning that you do not need to declare their type explicitly), and essential for creating dynamic, interactive web applications.
- Declaring variables
- Variables types
- Outputting Variables
- Loosely Typed Language
- Get the Type
- Assign Multiple Values
Declaring Variables in PHP
- PHP variables start with a dollar sign (
$) followed by the variable name. - Variable names must begin with a letter or an underscore (
_) but cannot start with a number. - Variable names can only contain alphanumeric characters and underscores (
A-Z,a-z,0-9, and_). - Variable names are case-sensitive (
$variableand$Variableare different).
In the example above, the variable $x will hold the value 5, and the variable $y will hold the value "Hello, World!".
Note
When you assign a text value to a variable, put quotes around the value.
Variable Types
PHP has no command for declaring a variable, and the data type depends on the value of the variable.
Data type |
Description |
Example |
|---|---|---|
String |
Stores a person's full name as a string. |
$name = "John Doe"; |
Integer |
Stores the age of a person as an integer. |
$age = 30; |
Double |
Stores a salary amount as a floating-point number. |
$salary = $5000.50; |
Boolean |
Indicates whether the person is employed (Boolean: true/false). |
$isEmployed = True; |
Array |
Store multiple values in a single variable |
$array = [1, "apple", true]; |
Object |
Store and manipulate complex data structures with properties (variables) and methods (functions) |
$p1 = new Person("Alice", 28); |
NULL |
It represents the absence of a value |
$var = null; |
Outputting Variables
You can display the value of a variable using echo or print.
PHP as a Loosely Typed Language
- PHP is considered a loosely typed (or dynamically typed) language, meaning you do not have to declare the data type of a variable when you create it.
- Instead, PHP automatically determines the data type of the variable based on the value it is assigned. This behavior makes PHP flexible and easy to use, especially for beginners in programming.
- In PHP 7, type declarations were added. This gives an option to specify the data type expected when declaring a function, and by enabling the strict requirement, it will throw a "Fatal Error" on a type mismatch.
- You will learn more about
strictandnon-strictrequirements, and data type declarations in the PHP Functions chapter.
Get the Type
1) gettype()
The gettype() function in PHP is used to get the type of a variable. It returns the data type of the specified variable as a string. This can be helpful for debugging and for checking variable types dynamically.
2) var_dump
The var_dump() function in PHP is used to display structured information (including the data type and value) about one or more variables. It is a powerful debugging tool that provides detailed insight into the contents of variables, making it helpful for developers to understand complex data structures.
How var_dump() Works?
The var_dump() function prints out:
- The type of the variable.
- The length (if applicable, such as for strings).
- The value of the variable.
Assign Multiple Values
You can assign the same value to multiple variables in one line: