In this code snippet, we’ll show an example of how to check if a given number, is a prime number.

A PHP function to check if a given number, is a prime number

<?php

/**
 * Checks if a given number is prime.
 *
 * @param int $number The number to check.
 * @return bool Returns true if the number is prime, false otherwise.
 */
function is_prime(int $number): bool
{
    if ($number <= 1) {
        return false;
    }

    $number_sqrt = sqrt($number);
    for ($i = 2; $i <= $number_sqrt; $i++) {
        if (($number % $i) == 0) {
            return false;
        }
    }
    return true;
}

echo is_prime(-1) . PHP_EOL;
echo is_prime(3) . PHP_EOL;
echo is_prime(13578) . PHP_EOL;
echo is_prime(999983) . PHP_EOL;

MIT Licensed Code – See License

Tags: PHP, is_prime, sqrt, prime, prime numbers, maths, php method, php function