  # PHP Check if a number is prime Examples:
`Input: 21 Output: Not Prime Input: 31 Output: Prime`
Simple Method:
A simple solution is to iterate over all the numbers from 2 to n / 2 and, for each number, check if it divides n. If we find any number that is divisible, we will return 0 (false), otherwise we will return 1 (true).Below is the PHP implementation of this approach:``` // PHP code to check if a number is prime or not // function to check if the number is primary or not function primeCheck ( \$number ) { if ( \$number == 1) return 0; for ( \$i = 2; \$i < = \$number / 2; \$i ++) { if ( \$number % \$i == 0) return 0; } return 1; }   // Driver code \$number = 31; \$flag = primeCheck ( \$number ); if ( \$flag == 1) echo "Prime" ; else echo " Not Prime "?> Output:Prime Time complexity: O (n)Efficient method: We can optimize the above approach by observing that instead of checking up to n we can check to sqrt (n), since the larger factor n must be a multiple of the smaller factor that has already been tested.So, we`ll go through the range [2, sqrt (number)] to check if the number is divisible by any number or not. If it is a dividend, then it is not a prime number.Below is the implementation of this approach in PHP: // PHP code to check if the number is prime or not // function to check if the number is whether the number is primary or not function primeCheck ( \$number ) { if ( \$number == 1) return 0; for ( \$i = 2; \$i < = sqrt ( \$number ); \$i ++) { if ( \$number % \$i == 0) return 0; } return 1; }   // Driver code \$number = 31; \$flag = primeCheck ( \$number ); if ( \$flag == 1) echo "Prime" ; else echo " Not Prime "?> Output:Prime Time complexity: O (sqrt (n)) ```
``` ```
``` ```
``` ```
``` Get Solution for free from DataCamp guru © 2021 Python.Engineering Best Python tutorials books for beginners and professionals Python.Engineering is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to amazon.com Computations Development Cryptography For dummies Machine Learning Big Data Loops Counters NumPy NLP PHP Regular Expressions File Handling Arrays String Variables Knowledge Database X Submit new EBook \$(document).ready(function () { \$(".modal_galery").owlCarousel({ items: 1, itemsCustom: false, itemsDesktop: [1300, 1], itemsDesktopSmall: [960, 1], itemsTablet: [768, 1], itemsTabletSmall: false, itemsMobile: [479, 1], singleItem: false, itemsScaleUp: false, pagination: false, navigation: true, rewindNav: true, autoPlay: true, stopOnHover: true, navigationText: [ "<img class='img_no_nav_mob' src='/wp-content/themes/nimani/image/prevCopy.png'>", "<img class='img_no_nav_mob' src='/wp-content/themes/nimani/image/nextCopy.png'>" ], }); \$(".tel_mask").mask("+9(999) 999-99-99"); }) ```