How can PHP developers efficiently determine if a number is prime by optimizing the for loop and condition checks?

To efficiently determine if a number is prime in PHP, developers can optimize the for loop by only iterating up to the square root of the number being checked. This is because if a number n is not a prime and has a factor greater than its square root, then it must also have a factor smaller than its square root. Additionally, developers can optimize the condition checks by checking for divisibility by 2 separately and then iterating only over odd numbers greater than 2.

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

// Test the function
$num = 17;
if (isPrime($num)) {
    echo $num . " is a prime number.";
} else {
    echo $num . " is not a prime number.";
}