Table of Contents
- Introduction
- Declaring PHP Variables
- Variable-naming Rules
- Integers
- Floating-point Numbers
- Booleans
- Literals vs Variables
- Variable Typing
Introduction
PHP uses variables for storing data to use in expressions and functions, and PHP supports the following standard scalar data types:
- Integers (e.g.
5
or-17
). - Floating-point numbers (e.g.
2.4
or3.14159
). - Strings (e.g.
"Hello"
or'World'
). - Booleans (
TRUE
orFALSE
).
The type of a variable is not set by the user but rather, it is decided at runtime by PHP depending on the context in which that variable is used.
In this article, we will discuss integers, floats and bools and leave strings to the next topic.
Declaring PHP Variables
PHP does not need any special keyword (such as JavaScript’s var
) to declare a variable. Instead, variables in PHP are represented by a dollar sign $
followed by the name of the variable. The variable name is case-sensitive.
We declare a variable by assigning the variable a value.
Example
Declaring a variable.1<?php
2 $name = "Donald Trump";
3 echo $name;
4?>
Donald Trump
In the above example, we declared the variable $name
by assigning to it the string "Donald Trump"
.
Variable-naming Rules
When creating PHP variables, the following rules must be followed:
- variable names must begin with a dollar sign (
$
). - variable names (after the dollar sign) must start with a letter or the
_
(underscore) character. - variable names can contain only the characters
a–z
,A–Z
,0–9
, and_
(under score). - variable names may not contain spaces. If a variable name must comprise more
than one word, a good idea is to separate the words with the
_
(underscore) character (e.g.$user_name
). - variable names are case-sensitive. The variable
$High_Score
is not the same as the variable$high_score
.
Integers
An integer is a number of the set $\mathbb{ℤ} = \{\ldots, -2, -1, 0, 1, 2, \ldots\}.$
Example
Declaring PHP integers.1<?php
2 $a = 1234;
3 $b = 1_234_567; // (as of PHP 7.4.0, underscores may be used )
4 echo $a;
5 echo "<BR>";
6 echo $b;
7?>
1234
1234567
Floating-point Numbers
Floating point numbers (also known as “floats”, “doubles”, or “real numbers”) can be specified using any of the following syntaxes.
Example
Declaring PHP floats.1<?php
2 $a = 1.234; // decimal
3 $b = 1.2e3; // exponential notation
4 $c = 7E-10; // exponential notation
5 $d = 1_234.567; // as of PHP 7.4.0
6 echo "$a <BR> $b <BR> $c <BR> $d";
7?>
1.234
1200
7.0E-10
1234.567
ADVERTISEMENT
Booleans
A bool expresses a truth value. A basic Boolean value can be either true
or false
. Both are case-insensitive.
Example
Declaring bool literals using constantstrue
or false
.
1<?php
2 $foo = true; // assign the constant TRUE to $foo
3 $bar = false; // assign the constant FALSE to $bar
4 echo "$foo <BR> $bar"; // print
5?>
1
Notice that while $foo
is displayed as 1
, $bar
does not display any value.
PHP assigns the numerical value of 1
to TRUE
and NULL
to FALSE
.
Example
Boolean variables can be added.1<?php
2 $x = true; // value = 1
3 $y = true; // value = 1
4 $z = false; // value = 0
5 echo $x + $y; // 1 + 1
6 echo "<BR>";
7 echo $x + $z; // 1 + 0
8?>
2
1
Literals vs Variables
These are the basic building blocks of PHP expressions. A literal is simply something that evaluates to itself, such as the number 68
or the string "Hello World"
. On the other hand, a variable evaluates to the value that has been assigned to it. The simplest PHP expression can be a single literal or variable, because both return a value.
Example
Literals vs variables. 1<?php
2 $name = "Bruce";
3 $age = 42;
4 echo 73 . "<br>"; // Numeric literal
5 echo "Hello" . "<br>"; // String literal
6 echo true . "<br>"; // Constant literal
7 echo false . "<br>"; // Constant literal
8 echo $name . "<br>"; // String variable
9 echo $age . "<br>"; // Numeric variable
10?>
73
Hello
1
Bruce
42
Note that the boolean value false
returns NULL
(empty). Both boolean values true
and false
are considered constant literals since they are predefined constants in PHP. Also, the period .
is a string concatenation character.
Variable Typing
PHP is a loosely typed language. This means that variables do not have to be explicitly declared before they are used and that PHP always converts variables to the type required by their context when they are accessed.
Example
Automatic conversion from a string to a number.1<?php
2 $string1 = "12345";
3 $number2 = 67890;
4 echo $string1 + $number2;
5?>
80235
In this example, the string $string1
is automatically converted to a numeric variable in the third line when it is added to the number $number2
.
Conversely, the same logic applies when a number is converted to a string.
Example
Automatic conversion from a number to a string.1<?php
2 $number = 12345;
3 echo "The value of the number is $number.";
4?>
The value of the number is 12345.