How to check for a prime number in PHP?

+1 vote
537 views
I was asked a question in my practical to check if a number is prime or not.

Can anyone help me? Thanks in advance

+1 vote

This code will help you check if a number is prime or not:

<?php

function IsPrime($n) { for($x=2; $x<$n; $x++) { if($n % $x ==0) { return false;} } return true; }$a = IsPrime(13);

echo \$a ? 'This is a Prime Number.' :  'This is not a Prime Number.';

?>