How can the code be optimized or refactored to improve readability and efficiency?
The code can be optimized by breaking down the complex logic into smaller, more manageable functions. This will improve readability and make the code easier to understand and maintain. Additionally, using built-in PHP functions and language features can help improve efficiency.
function isPrime($num){
if($num < 2){
return false;
}
for($i = 2; $i <= sqrt($num); $i++){
if($num % $i == 0){
return false;
}
}
return true;
}
$num = 17;
if(isPrime($num)){
echo "$num is a prime number.";
} else {
echo "$num is not a prime number.";
}