0% found this document useful (0 votes)
3 views

PROGRAMS in PHP

The document contains PHP programs that demonstrate various functionalities including checking for Armstrong numbers, palindromes, prime numbers, even numbers, reversing strings, and sorting arrays. Each program includes a function definition and an example of its usage with output results. These examples serve as practical implementations of basic programming concepts in PHP.

Uploaded by

ckmanish8
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

PROGRAMS in PHP

The document contains PHP programs that demonstrate various functionalities including checking for Armstrong numbers, palindromes, prime numbers, even numbers, reversing strings, and sorting arrays. Each program includes a function definition and an example of its usage with output results. These examples serve as practical implementations of basic programming concepts in PHP.

Uploaded by

ckmanish8
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

PROGRAMS in PHP

1. Armstrong Number
function isArmstrong($num) {
$sum = 0;
$temp = $num;
while ($temp > 0) {
$digit = $temp % 10;
$sum += $digit ** 3;
$temp = (int)($temp / 10);
}
return $sum == $num;
}

$num = 153;
echo isArmstrong($num) ? "$num is an Armstrong number." : "$num is not an
Armstrong number.";
Output:
153 is an Armstrong number.
2. Palindrome
function isPalindrome($str) {
return $str === strrev($str);
}
$str = "racecar";
echo isPalindrome($str) ? "$str is a palindrome." : "$str is not a palindrome.";
Output:
racecar is a palindrome.
3. Prime Number Check
function isPrime($num) {
if ($num <= 1) return false;
for ($i = 2; $i <= sqrt($num); $i++) {
if ($num % $i == 0) return false;
}
return true;
}
$num = 29;
echo isPrime($num) ? "$num is a prime number." : "$num is not a prime
number.";
Output:
29 is a prime number.
4. Even Number Check
function isEven($num) {
return $num % 2 == 0;
}
$num = 4;
echo isEven($num) ? "$num is an even number." : "$num is an odd number.";
Output:
4 is an even number.
5. Reverse a String
function reverseString($str) {
return strrev($str);
}
$str = "hello";
echo "Reversed string: " . reverseString($str);
Output:
Reversed string: olleh
6. Sort an Array
function sortArray($arr) {
sort($arr);
return $arr;
}
$arr = [5, 2, 9, 1, 5, 6];
print_r(sortArray($arr));
Output:
Array ( [0] => 1 [1] => 2 [2] => 5 [3] => 5 [4] => 6 [5] => 9 )

You might also like