Prime Numbers in PHP

A prime number is a natural number greater than 1 that is not a product of two smaller natural numbers. The number 2 is the only even prime number, and prime numbers play a fundamental role in number theory and cryptography.

PHP Example:

Let’s generate the first 15 prime numbers using PHP:

<?php
function isPrime($num) {
    if ($num < 2) {
        return false;
    }
    for ($i = 2; $i <= sqrt($num); $i++) {
        if ($num % $i == 0) {
            return false;
        }
    }
    return true;
}

$count = 0;
$number = 2;

while ($count < 15) {
    if (isPrime($number)) {
        echo $number;
        if ($count < 14) {
            echo ", ";
        }
        $count++;
    }

    $number++;
}
?>

Output:

The above PHP code will produce the following output:

2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37

This output displays the first 15 prime numbers, separated by commas.