Numbers and arithmetic operations are a fundamental part of programming. In this lesson, we’ll learn how to use and manipulate numbers in PHP.
PHP has two number data types. The integer data type includes positive and negative whole numbers (such as 3, 4599, -98, and 0). The floating point data type is used to represent decimal numbers (such as 4.98273, 2.1, -9.7, -182736.8).
echo 5; // Prints: 5 echo -22.8; // Prints: -22.8
We can also declare variables and assign numbers as their values:
$my_int = 78; $my_float = -897.21; echo $my_int; // Prints: 78 echo $my_float; // Prints: -897.21
In the code above, we created the
$my_int variable and assigned the integer value of 78 to it. Next, we created the
$my_float variable and assigned the floating point value of -897.21 to it.
Let’s practice making number variables!
Declare a variable with any name you’d like and assign an integer value to it. Use
echo to print your variable to the terminal.
We’re going to make and print another number variable, but we don’t want it to print on the same line. Use
echo to print the string
Next, declare a variable with any name you’d like and assign a floating point value to it. Use
echo to print this new variable to the terminal.