PHP Program to Check Even or Odd
Determines if a number is even or odd using the modulo operator (%).
<?php
$number = 7;
if ($number % 2 === 0) {
echo "$number is Even\n";
} else {
echo "$number is Odd\n";
}
?>7 is Odd
Key Takeaway: Practice PHP programming with 84+ practical code examples covering syntax, arrays, strings, functions, OOP, JSON, and file handling — all with expected terminal outputs.
Audience & Use Case: Designed for web developers, backend engineers, Laravel developers, computer science students, and interview candidates mastering modern PHP.
Who is this for: Web developers, Laravel learners, API engineers, and CS students.
Related Tools:PHP Compiler · PHP Official Manual · JavaScript Examples · Python Examples
Determines if a number is even or odd using the modulo operator (%).
<?php
$number = 7;
if ($number % 2 === 0) {
echo "$number is Even\n";
} else {
echo "$number is Odd\n";
}
?>7 is Odd
Performs simple addition of two numerical variables.
<?php $num1 = 15; $num2 = 27; $sum = $num1 + $num2; echo "Sum of $num1 and $num2 is: $sum\n"; ?>
Sum of 15 and 27 is: 42
Finds the maximum value among three numbers using max().
<?php $x = 42; $y = 89; $z = 64; $maxVal = max($x, $y, $z); echo "The largest number among $x, $y, and $z is $maxVal\n"; ?>
The largest number among 42, 89, and 64 is 89
Computes the factorial of an integer using a for loop.
<?php
$n = 5;
$factorial = 1;
for ($i = 1; $i <= $n; $i++) {
$factorial *= $i;
}
echo "Factorial of $n is $factorial\n";
?>Factorial of 5 is 120
Generates the first N numbers of the Fibonacci sequence.
<?php
$n = 10;
$a = 0;
$b = 1;
echo "Fibonacci series up to $n terms: ";
for ($i = 0; $i < $n; $i++) {
echo $a . " ";
$next = $a + $b;
$a = $b;
$b = $next;
}
echo "\n";
?>Fibonacci series up to 10 terms: 0 1 1 2 3 5 8 13 21 34
Determines whether an integer is prime using trial division.
<?php
function isPrime($num) {
if ($num < 2) return false;
for ($i = 2; $i * $i <= $num; $i++) {
if ($num % $i === 0) return false;
}
return true;
}
$testNum = 29;
echo $testNum . (isPrime($testNum) ? " is a Prime Number" : " is Not a Prime Number") . "\n";
?>29 is a Prime Number
Uses PHP math library functions like abs, sqrt, pow, round, and rand.
<?php echo "Absolute (-15): " . abs(-15) . "\n"; echo "Square Root (64): " . sqrt(64) . "\n"; echo "Power (2^8): " . pow(2, 8) . "\n"; echo "Round (3.75): " . round(3.75) . "\n"; echo "Random (1-100): " . rand(1, 100) . "\n"; ?>
Absolute (-15): 15 Square Root (64): 8 Power (2^8): 256 Round (3.75): 4 Random (1-100): 42
Calculates population standard deviation of numbers in a PHP array.
<?php
function calculateStdDev(array $arr): float {
$count = count($arr);
if ($count === 0) return 0.0;
$mean = array_sum($arr) / $count;
$variance = 0.0;
foreach ($arr as $val) {
$variance += pow($val - $mean, 2);
}
return sqrt($variance / $count);
}
$numbers = [10, 12, 23, 23, 16, 23, 21, 16];
$stdDev = calculateStdDev($numbers);
echo "Standard Deviation: " . round($stdDev, 4) . "\n";
?>Standard Deviation: 4.899
Generates Arithmetic Progression (AP) terms using range().
<?php
$start = 5;
$end = 50;
$step = 5;
$apSeries = range($start, $end, $step);
echo "Arithmetic Progression Series: " . implode(", ", $apSeries) . "\n";
?>Arithmetic Progression Series: 5, 10, 15, 20, 25, 30, 35, 40, 45, 50
Uses PDO prepared statements with parameterized queries to prevent SQL injection vulnerabilities.
<?php
$sql = "SELECT id, username, email FROM users WHERE email = :email AND status = :status";
$params = [
':email' => 'user@example.com',
':status' => 'active'
];
echo "Prepared SQL Query:\n$sql\n\nBound Parameters:\n";
print_r($params);
?>Prepared SQL Query:
SELECT id, username, email FROM users WHERE email = :email AND status = :status
Bound Parameters:
Array
(
[:email] => user@example.com
[:status] => active
)| Domain | Core Concepts | Primary Use Case | Official Docs |
|---|---|---|---|
| Variables & Types | $var, string, int, bool | Variables, string concatenation, type declarations | PHP Types |
| Arrays & Data | Indexed arrays, Associative ['key' => 'val'] | Key-value mappings, list iteration with foreach | PHP Arrays |
| Functions | function name(type $param): returnType | Modular code, default arguments, return value typing | PHP Functions |
| OOP Constructs | class, extends, public/protected | Encapsulation, constructors, inheritance, web frameworks | PHP OOP |
Use the online PHP compiler to test snippet modifications, test array manipulation logic, and build custom functions for backend applications.
[], and standard OOP paradigms compatible with PHP 7.4 through PHP 8.x and frameworks such as Laravel and Symfony. PHP powers over 75% of the active web today. Practicing short, clear programs is the most efficient path to mastering PHP backend development. Each snippet focuses on a fundamental building block — from basic variable output and control structures to complex associative arrays, JSON serialization, and object-oriented design.
Learn echo syntax, variable assignment, type coercion, and simple arithmetic operations.
Master key-value arrays, foreach loops, function type hints, and string/math helpers.
Explore classes, inheritance, visibility, JSON APIs, and file read/write operations.