PHP Math & Algorithm Programs

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.

Last updated: September 2026 84 Verified Examples

Quick Start & How to Use

  1. Select a PHP program topic from the sidebar menu (or mobile dropdown).
  2. Examine the clear PHP code syntax and corresponding terminal output.
  3. Copy the code directly or run it live in our Online PHP Compiler.
  4. Experiment with arrays, functions, and OOP classes to solidify your server-side programming skills.

Who is this for: Web developers, Laravel learners, API engineers, and CS students.

Related Tools:PHP Compiler · PHP Official Manual · JavaScript Examples · Python Examples

PHP Program to Check Prime Number

Run Online

Determines whether an integer is prime using trial division.

Program (PHP)
<?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";
?>
Expected Output
29 is a Prime Number

PHP Program to Add Two Numbers

Run Online

Performs simple addition of two numerical variables.

Program (PHP)
<?php

$num1 = 15;
$num2 = 27;
$sum = $num1 + $num2;

echo "Sum of $num1 and $num2 is: $sum\n";
?>
Expected Output
Sum of 15 and 27 is: 42

PHP Program to Check Even or Odd

Run Online

Determines if a number is even or odd using the modulo operator (%).

Program (PHP)
<?php

$number = 7;

if ($number % 2 === 0) {
    echo "$number is Even\n";
} else {
    echo "$number is Odd\n";
}
?>
Expected Output
7 is Odd

PHP Program to Find Largest of Three Numbers

Run Online

Finds the maximum value among three numbers using max().

Program (PHP)
<?php

$x = 42;
$y = 89;
$z = 64;

$maxVal = max($x, $y, $z);

echo "The largest number among $x, $y, and $z is $maxVal\n";
?>
Expected Output
The largest number among 42, 89, and 64 is 89

PHP Program to Find Factorial of a Number

Run Online

Computes the factorial of an integer using a for loop.

Program (PHP)
<?php

$n = 5;
$factorial = 1;

for ($i = 1; $i <= $n; $i++) {
    $factorial *= $i;
}

echo "Factorial of $n is $factorial\n";
?>
Expected Output
Factorial of 5 is 120

PHP Program to Display Fibonacci Series

Run Online

Generates the first N numbers of the Fibonacci sequence.

Program (PHP)
<?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";
?>
Expected Output
Fibonacci series up to 10 terms: 0 1 1 2 3 5 8 13 21 34 

PHP Math Functions Example

Run Online

Uses PHP math library functions like abs, sqrt, pow, round, and rand.

Program (PHP)
<?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";
?>
Expected Output
Absolute (-15): 15
Square Root (64): 8
Power (2^8): 256
Round (3.75): 4
Random (1-100): 42

How to write a PHP program to find the Standard Deviation of an array?

Run Online

Calculates population standard deviation of numbers in a PHP array.

Program (PHP)
<?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";
?>
Expected Output
Standard Deviation: 4.899

How to print an arithmetic progression series using inbuilt functions in PHP?

Run Online

Generates Arithmetic Progression (AP) terms using range().

Program (PHP)
<?php

$start = 5;
$end = 50;
$step = 5;

$apSeries = range($start, $end, $step);

echo "Arithmetic Progression Series: " . implode(", ", $apSeries) . "\n";
?>
Expected Output
Arithmetic Progression Series: 5, 10, 15, 20, 25, 30, 35, 40, 45, 50

How to prevent SQL Injection in PHP?

Run Online

Uses PDO prepared statements with parameterized queries to prevent SQL injection vulnerabilities.

Program (PHP)
<?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);
?>
Expected Output
Prepared SQL Query:
SELECT id, username, email FROM users WHERE email = :email AND status = :status

Bound Parameters:
Array
(
    [:email] => user@example.com
    [:status] => active
)

PHP Core Concepts & Feature Breakdown

DomainCore ConceptsPrimary Use CaseOfficial Docs
Variables & Types$var, string, int, boolVariables, string concatenation, type declarationsPHP Types
Arrays & DataIndexed arrays, Associative ['key' => 'val']Key-value mappings, list iteration with foreachPHP Arrays
Functionsfunction name(type $param): returnTypeModular code, default arguments, return value typingPHP Functions
OOP Constructsclass, extends, public/protectedEncapsulation, constructors, inheritance, web frameworksPHP OOP

Keep Practicing

Use the online PHP compiler to test snippet modifications, test array manipulation logic, and build custom functions for backend applications.

Frequently Asked Questions

You can run all snippets instantly in your browser using our Online PHP Compiler without setting up Apache, Nginx, or local PHP runtimes.

Yes. All programs utilize modern PHP standard syntax, typed properties/arguments, short array syntax [], and standard OOP paradigms compatible with PHP 7.4 through PHP 8.x and frameworks such as Laravel and Symfony.

Absolutely. These examples cover essential backend concepts frequently requested during technical evaluations: string operations, prime checks, Fibonacci algorithms, associative array iteration, JSON parsing, and object-oriented inheritance.

Learn PHP by Practicing Examples

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.

Beginner Foundations

Learn echo syntax, variable assignment, type coercion, and simple arithmetic operations.

Arrays & Logic

Master key-value arrays, foreach loops, function type hints, and string/math helpers.

OOP & Files

Explore classes, inheritance, visibility, JSON APIs, and file read/write operations.