Last updated on November 28th, 2023
How to create a variable in PHP?
You can choose for a variable name any word or combination of letters and numbers, except the so-called reserved words of the language. A list of reserved words in PHP can be found on the following page: https://www.php.net/manual/en/reserved.php
In PHP each variable name is always preceded by a dollar sign ($variable).
The variables are like containers where you can store information. Imagine them as labeled boxes. You can assign a value to a variable using the equal sign (=). I.e. the syntax for assigning a value to a variable is as follows:
$variable = value;
The construct must always end with a semicolon, and in some cases the value of the variable must be enclosed in quotation marks:
$variable = “value”;
For example, you can have a box (variable) called $name and put the name “John” (value) inside it like this:
$name = “John”;
As can be seen from the example, the construction ends with a semicolon (;). In this case the value of the variable must be enclosed in quotation marks (“) because the value from the above example is a string of characters, in this case letters (John), that should be able to be displayed on a screen.
So, $name = “John”; declares a variable named $name with the value “John”.
Variable names may contain uppercase and lowercase Latin letters, Arabic numerals, and an underscore, but may only begin with a letter or an underscore. This means that $var, $Var1 and $_var are valid variable names, but $1var is an invalid name.
Variable names are case sensitive, ie. $var, $Var and $VAR are three different variables.
What types of variables are there?
There are 4 basic types of variables in PHP:
1. Integer (int)
Example: $age = 25;
Integer variables store whole numbers like 25, 0, or -10. They are used for counting, quantities, and numerical operations.
2. Float (Floating-Point or Decimal)
Example: $price = 19.99;
Float variables store numbers with decimal points, like prices, percentages, or measurements that require precision.
3. String
Example: $name = “John”;
String variables store text or characters, like names, addresses, or any sequence of characters enclosed in double or single quotes.
4. Boolean (bool):
Example: $isSunny = true;
Boolean variables can have only two values, true or false. They are used for representing binary choices, like yes/no or on/off.
These basic variable types are the building blocks of PHP. You can use them to store and manipulate different kinds of data in your PHP scripts.
Examples of using variables in PHP
1. Integer (int) Example:
<?php $age = 15; echo "My age is $age years."; ?>
In this example, we declare an integer variable named $age and assign it the value 15.
The echo statement is used to display a message that includes the value of the $age variable.
When you run this script, it will output “My age is 15 years.” The variable $age holds the integer value 15, and we include it in the string using double quotes (“), which allows us to embed variables directly within the string.
2. Float (Floating-Point or Decimal) Example:
<?php $price = 19.99; echo "The product costs $price dollars."; ?>
In this example, we declare a float variable named $price and assign it the value 19.99.
Similar to the first example, we use the echo statement to display a message that includes the value of the $price variable.
When you run this script, it will output “The product costs 19.99 dollars.” The variable $price holds the floating-point number 19.99, which represents a price with decimal precision.
3. String Example:
<?php $name = "John"; echo "Hello, my name is $name."; ?>
In this example, we declare a string variable named $name and assign it the value “John”.
Once again, we use echo to display a message that includes the value of the $name variable.
When you run this script, it will output “Hello, my name is John.” Here, $name stores a string of characters, and we use double quotes (“) to create a string that includes the value of $name.
4. Boolean (bool):
<?php $isSunny = true; if ($isSunny) { echo "It's a sunny day!"; } else { echo "It's not sunny today."; } ?>
In this example, we declare a boolean variable named $isSunny and assign it the value true.
We then use an if statement to check the value of $isSunny. If it’s true, we display one message; if it’s false, we display another message.
When you run this script, it will output “It’s a sunny day!” because the $isSunny variable is set to true. The boolean variable is used to represent binary choices, and in this case, it indicates that the weather is sunny.