In PHP, a variable starts with the $
sign, followed by the name of the variable:
$x = 5;
$y = "John"
In the example above, the variable $x
will hold the value 5
, and the variable $y
will hold the value "John".
Note: When you assign a text value to a variable, put quotes around the value.
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.
Think of variables as containers for storing data.
A variable can have a short name (like $x
and $y
) or a more descriptive name ($age, $carname, $total_volume
).
$
sign, followed by the name of the variableRemember that PHP variable names are case-sensitive!
The PHP echo
and print
statements are used to output data
The following example will show how to output text and a variable:
$txt = "q3schools.com";
echo "I love $txt!";
The following example will produce the same output as the example above:
$txt = "q3schools.com";
echo "I love " . $txt . "!";
The following example will output the sum of two variables:
$x = 5;
$y = 4;
echo $x + $y;
In the example above, notice that we did not have to tell PHP which data type the variable is.
PHP automatically associates a data type to the variable, depending on its value. Since the data types are not set in a strict sense, you can do things like adding a string to an integer without causing an error.
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.
In PHP, variables can hold different types of data. Here are some of the main variable types in PHP:
<?php
// Integer
$age = 30;
echo "Age: " . $age . "<br>";
// Float
$price = 19.99;
echo "Price: $" . $price . "<br>";
// String
$name = "John Doe";
echo "Name: " . $name . "<br>";
// Boolean
$is_valid = true;
echo "Is Valid: " . ($is_valid ? 'true' : 'false') . "<br>";
// Array
$colors = array("red", "green", "blue");
echo "Colors: " . implode(", ", $colors) . "<br>";
// Object
class Person {
public $name;
public $age;
}
$person = new Person();
$person->name = "John";
$person->age = 30;
echo "Person: " . $person->name . ", Age: " . $person->age . "<br>";
// NULL
$variable = null;
echo "Variable: " . var_export($variable, true) . "<br>";
// Resource (file handle)
$file_handle = fopen("example.txt", "r");
if ($file_handle) {
echo "File Handle: " . $file_handle . "<br>";
fclose($file_handle); // Close the file handle
}
// Callable (function)
function my_function($arg) {
echo "Hello, " . $arg . "<br>";
}
$callback = 'my_function';
$callback("World");
// Iterable (array)
$numbers = [1, 2, 3, 4, 5];
foreach ($numbers as $number) {
echo "Number: " . $number . "<br>";
}
?>
To get the data type of a variable, use the var_dump()
function.
$x = 5;
var_dump($x);
See what var_dump()
returns for other data types:
var_dump(5);
var_dump("John");
var_dump(3.14);
var_dump(true);
var_dump([2, 3, 56]);
var_dump(NULL);
Assigning a string to a variable is done with the variable name followed by an equal sign and the string:
$x = "John";
echo $x;
String variables can be declared either by using double or single quotes, but you should be aware of the differences.
You can assign the same value to multiple variables in one line:
$x = $y = $z = "Fruit";