Numbers
Part of the Fundamentals section of Coddy's PHP journey — lesson 6 of 71.
Variables are containers that hold data values. They are used to store, manipulate, and display information within a program.
In short, a variable is like a memory unit that we can access by typing the name of the variable.
Each variable has a unique name and a value that can be of different types.
In PHP, all variable names must start with a dollar sign ($). To initialize a variable, we use the following format:
$variable_name = value;Let's take a look at the different types of numbers:
int - an integer, such as 1 or -2.
float - a real number, such as 1.32 or 0.98.
For example:
To initialize a variable of type int with the name a and the value 3:
$a = 3;To initialize a variable of type float with the name b and the value 13.2:
$b = 13.2;Challenge
EasyWrite code that initializes a variable named var with the value 5.
- Replace the
?with the correct value - Lines starting with
#are comments - they explain what to do but don't affect your code - Only change the line that says
$var = ?;
Cheat sheet
Variables are containers that hold data values. In PHP, variable names must start with a dollar sign ($).
To initialize a variable:
$variable_name = value;Number types:
int- an integer (e.g., 1, -2)float- a real number (e.g., 1.32, 0.98)
Examples:
$a = 3; // int
$b = 13.2; // floatTry it yourself
<?php
# Write your code below
$var = ?;
# Don't change the line below
echo "var = " . $var . "\n";
?>This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Fundamentals
4Comparison & Logical Operators
Comparison OperatorsEquality & IdentityLogical Operators Part 1Logical Operators Part 2Recap - Simple Logic2Variables and Data Types
NumbersStrings and QuotesBooleansNaming ConventionsRecap - Variable InitEmpty VariablesString ConcatenationGetting User InputCast to Different Types5Conditional Logic
If StatementIf - ElseThe Ternary OperatorNull Coalescing OperatorSwitch StatementRecap - Making Decisions3Basic Operators
Arithmetic OperatorsModulo OperatorExponentiation OperatorCombined AssignmentIncrement/DecrementOperator PrecedenceRecap - Simple CalculationsString Operators