<?php
function isPrime($number) {
if ($number <= 1) {
return false;
}
for ($i = 2; $i <= sqrt($number); $i++) {
if ($number % $i === 0) {
return false;
}
}
return true;
}
$testNumber = 13;
if (isPrime($testNumber)) {
echo "$testNumber is a prime number.";
} else {
echo "$testNumber is not a prime number.";
}
?>
- The script defines a function
isPrime
that takes a number as a parameter and checks whether it is a prime number. - A prime number is a natural number greater than 1 that is not a product of two smaller natural numbers.
- The function uses a
for
loop to iterate from 2 to the square root of the number to check for divisors. - If the number has any divisors other than 1 and itself, it is not a prime number.
- The script then calls the function with a sample number (
$testNumber
) and prints whether it is a prime number or not.