Skip to content
Menu
Code for Java c
  • Home
  • Java
    • Java Examples
    • Java tutorials
  • C
    • C tutorials
    • C Examples
  • C++
    • C++ Tutorials
    • C++ Examples
  • Python
    • Python Tutorials
    • Python Examples
  • About
    • About me
    • contact us
    • disclaimer
    • Privacy Policy
Code for Java c
PHP| Check if a number is prime or not

PHP| Function to Check if a number is prime or not

Posted on December 21, 2022

Table of Contents

  • PHP| Function to Check if a number is prime or not
    • Code to check prime number
      • Check the prime number using the PHP function- #1
      • Check the prime number using the PHP function- #2

PHP| Function to Check if a number is prime or not

In this article, we will discuss the concept of the PHP| Function to Check if a number is prime or not

In this post, we are going to learn how to write a program to check whether the given number is prime or not, using a function and displaying the result on the screen in the PHP programming language.

Code to check prime number

Check the prime number using the PHP function- #1

In this program, the user defines a function with the parameter, and when the user calls the function with an argument, the program checks whether the given number is prime or not in the PHP programming language.

Program 1

<?php
//php code to check whether a number is prime or not 
//using function 
function checkPrime($num){
  //function definition
$n=0;
for($i=2; $i<($num/2+1); $i++){
  if($num%$i==0){
  $n++;
  break;
  }
}

if($n==0){
echo $num." is a prime number.";
//print if the number is a prime
}else{
echo $num." is not a prime number.";
//print if the number is not prime
}
}
checkPrime(20);
//Call the function with argument
?>

When the above code is executed, it produces the following result

20 is not a prime number.

 

Check the prime number using the PHP function- #2

In this program, the user defines a function with the parameter, and when the user calls the function with an argument, the program checks whether the given number is prime or not in the PHP programming language.

Program 2

<?php
//php code to check whether a number is prime or not 
//using function 
function checkPrime($num){
  //function definition with parameter
for($i=2; $i<$num; $i++){
  if($num%$i==0){
  return 0;
  }
}
return 1;
}
$x=checkPrime(31);
//Call the function with argument
if($x==0){
echo "this is not a prime number.";
//print if the number is not prime
}else{
echo "this is a prime number.";
//print if the number is prime
}

?>

When the above code is executed, it produces the following result

this is a prime number.

 

 

Similar post

Java programming code to check whether it is prime or not

C programming code to check prime or not

C++ programming code to check whether it is prime or not

Python programming code to check prime or not

C# programming code to check prime or not

C# programming code to check prime or not using function

 

Code to print prime numbers from 1 to 100 or 1 to n in Java

Code to print prime numbers from 1 to 100 or 1 to n in C

Code to print prime numbers from 1 to 100 or 1 to n in C++

Code to print prime numbers from 1 to 100 or 1 to n in Python

 

Related

Recent Posts

  • Subtract two numbers using method overriding
  • PHP Star triangle Pattern program
  • Using function or method to Write temperature conversion : Fahrenheit into Celsius
  • Function or method:temperature conversion from Fahrenheit into Celsius – Entered by user
  • Write temperature conversion program: Fahrenheit into Celsius
  • How to write a program to convert Fahrenheit into Celsius

tag

Addition (6) Array (38) C++ language (91) C language (98) c sharp (23) Division (6) Function (29) if else (32) Java language (102) JavaScript (5) loops (137) Multiply (7) Oop (2) patterns (65) PHP (13) Python Language (38) Subtraction (7) temperature (20)

Archives

Categories

Address

Global information technology

Puloly south, PointPedro

Jaffna

Srilanka

©2025 Code for Java c | Powered by SuperbThemes