-
Hajipur, Bihar, 844101
Numbers are one of the fundamental data types in PHP. They are used to perform mathematical operations, calculations, and logical comparisons. PHP supports integers and floats (decimal numbers), and offers a wide range of functions and operators to work with them effectively. Understanding numbers is essential for developing dynamic web applications, handling calculations, and managing data.
Integers are whole numbers without a decimal point. They can be positive, negative, or zero.
<?php
$positive = 100; // Positive integer
$negative = -50; // Negative integer
$zero = 0; // Zero
echo "Positive: $positive<br>";
echo "Negative: $negative<br>";
echo "Zero: $zero<br>";
?>
Integers are used for counting items, array indexes, loops, and calculations.
PHP automatically handles large integers, but extremely large numbers may be converted to float.
Floats (also called doubles) are numbers with decimal points.
<?php
$price = 49.99;
$temperature = -12.5;
echo "Price: $" . $price . "<br>";
echo "Temperature: " . $temperature . "°C<br>";
?>
Floats are essential for prices, measurements, scientific calculations, and averages.
You can also use exponential notation:
$num = 1.5e3; // 1.5 × 10^3 = 1500
echo "Exponential: $num<br>";
Implicit conversion occurs when performing operations between integers and floats.
PHP allows strings containing numbers to be treated as numbers in calculations.
<?php
$num1 = "10";
$num2 = 5;
echo $num1 + $num2; // Outputs: 15
?>
PHP automatically converts numeric strings to integers or floats during arithmetic operations.
Be careful with strings containing non-numeric characters; only the numeric part will be used.
PHP supports standard arithmetic operations:
<?php
$a = 10;
$b = 3;
echo "Addition: " . ($a + $b) . "<br>"; // 13
echo "Subtraction: " . ($a - $b) . "<br>"; // 7
echo "Multiplication: " . ($a * $b) . "<br>"; // 30
echo "Division: " . ($a / $b) . "<br>"; // 3.3333
echo "Modulus: " . ($a % $b) . "<br>"; // 1
echo "Exponent: " . ($a ** $b) . "<br>"; // 1000
?>
Operator precedence matters: *
and /
are evaluated before +
and -
.
Parentheses ()
can control evaluation order.
echo 10 + 5 * 2; // Outputs 20
echo (10 + 5) * 2; // Outputs 30
You can check whether a value is an integer or float using built-in functions:
<?php
$num1 = 25;
$num2 = 12.5;
$num3 = "15";
echo is_int($num1) ? "$num1 is an integer<br>" : "$num1 is not an integer<br>";
echo is_float($num2) ? "$num2 is a float<br>" : "$num2 is not a float<br>";
echo is_numeric($num3) ? "$num3 is numeric<br>" : "$num3 is not numeric<br>";
?>
is_int()
checks for integers.
is_float()
checks for floating-point numbers.
is_numeric()
checks if a value is numeric, including numeric strings.
PHP allows type casting to convert values between integers, floats, and strings.
<?php
$floatNum = 12.75;
$intNum = (int)$floatNum; // Cast float to integer
echo $intNum . "<br>"; // Outputs: 12
$intVal = 20;
$floatVal = (float)$intVal; // Cast integer to float
echo $floatVal . "<br>";
?>
Casting is useful for calculations requiring specific types.
It helps avoid unexpected results when working with numeric strings.
PHP provides many built-in functions for numbers.
abs()
– Absolute Value<?php
echo abs(-50) . "<br>"; // Outputs: 50
?>
round()
– Round Numbers<?php
echo round(3.14159, 2) . "<br>"; // Outputs: 3.14
?>
ceil()
– Round Up<?php
echo ceil(4.2) . "<br>"; // Outputs: 5
?>
floor()
– Round Down<?php
echo floor(4.8) . "<br>"; // Outputs: 4
?>
max()
and min()
– Maximum and Minimum<?php
echo max(2, 10, 5) . "<br>"; // Outputs: 10
echo min(2, 10, 5) . "<br>"; // Outputs: 2
?>
sqrt()
– Square Root<?php
echo sqrt(16) . "<br>"; // Outputs: 4
?>
Functions simplify mathematical operations and improve accuracy.
PHP can generate random numbers using rand()
or mt_rand()
:
<?php
echo rand(1, 100) . "<br>"; // Random number between 1 and 100
echo mt_rand(1, 50) . "<br>"; // Faster and more secure
?>
Useful in games, lotteries, simulations, or selecting random items.
PHP allows defining numeric constants:
<?php
define("PI", 3.14159);
echo "Value of PI: " . PI;
?>
Constants are immutable and do not use the $
symbol.
Ideal for fixed values like mathematical constants or configuration values.
Dividing by zero causes warnings or errors. Always check the denominator.
<?php
$divisor = 0;
if($divisor != 0) {
echo 10 / $divisor;
} else {
echo "Cannot divide by zero!";
}
?>
Be careful with numeric strings containing letters; PHP may ignore trailing characters.
$num = "15abc";
echo $num + 5; // Outputs 20, only numeric part used
Use type casting to avoid unexpected results.
PHP supports integers and floats for all calculations.
Arithmetic operations: +
, -
, *
, /
, %
, **
.
Operator precedence affects calculation order; use parentheses as needed.
Check types with is_int()
, is_float()
, or is_numeric()
.
Type casting converts numbers between types.
Built-in functions: abs()
, round()
, ceil()
, floor()
, max()
, min()
, sqrt()
.
Generate random numbers with rand()
or mt_rand()
.
Constants store fixed numeric values.
Avoid dividing by zero and handle numeric strings carefully.
Numbers are essential for calculations, logic, and dynamic programming in PHP. Understanding numbers, operators, functions, and type conversion ensures your programs are accurate and efficient.
Create a PHP script that declares an integer variable $count = 10
and prints it using echo
. Add a comment explaining it is an integer.
Declare a float variable $price = 49.99
and print it using echo
. Include a comment explaining it is a decimal number.
Write a PHP script that adds two numbers, $a = 15
and $b = 7
, and prints the result. Include a comment explaining the addition operation.
Create a PHP script that divides $num1 = 20
by $num2 = 4
and prints the result. Include a comment explaining division and check for division by zero.
Write a PHP file that finds the remainder of $num1 = 17
divided by $num2 = 5
using the modulus operator %
and prints it. Include a comment.
Create a PHP script that casts a float $floatNum = 12.75
to an integer and prints the result. Include a comment explaining type casting.
Write a PHP script that uses round()
, ceil()
, and floor()
on $num = 4.6
and prints the results. Include comments explaining each function.
Create a PHP file that finds the maximum and minimum of the numbers 5, 10, and 3 using max()
and min()
. Include comments explaining the functions.
Write a PHP script that generates a random number between 1 and 100 using rand()
and prints it. Include a comment explaining its usage.
Create a PHP file that defines a constant PI
with the value 3.14159
and prints it. Include a comment explaining why constants are used.