APznzabmENvFYnRZXBIVrD7KMzRVhRr-lQUs0Mv2UAM4pqgpvtJ4KbqHHT32CP0LkQvb-a4lbt-sEZaWpcp6mjzQLmTUKEuV31C3hNa4zMdpA4xP9CK_LbOarbpQi14AvxGsJWee49MMFXJcYgV1yEgCLCN9GrZKNLr0B8XOS2hf2gY69mI2KBkVSIvzilcoj2htptu35lNQ-sPGCP6gyhb

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

1.Write a PHP program to sort list without using sort().

<?php
$array=array('2','4','8','5','1','7','6','9','10','3');
echo "Unsorted array is:
"; echo "<br />";
foreach ($array as $value)
{ echo $value . ",";
}
echo "<br />";
echo "<br />";
for($j = 0; $j < count($array); $j ++)
{ for($i = 0; $i < count($array)-1; $i +
+){
if($array[$i] > $array[$i+1]) {
$temp = $array[$i+1];
$array[$i+1]=$array[$i];
$array[$i]=$temp;
}
}
}
echo "Sorted Array is:
"; echo "<br />";
#print_r($array);
foreach ($array as $value)
{ echo $value . ",";
}
?>
2. Write a PHP program to decompose a string into individual elements and
store them
in an array.
Use PHP's explode() function to split a string by delimiter and store the
separate segments in a numerically indexed array:
<?php
// define string
$alphabetStr = "a b c d e f g h i j k";
// break string into array
// using whitespace as the separator
// result: ("a","b","c","d","e","f","g","h","i","j","k")
print_r(explode(" ", $alphabetStr));
?>
You want to strip an array of all duplicate elements to obtain a unique set.
(Removing
Duplicate Elements in an Array)
<?php
// define an array containing duplicates
$numbers =
array(10,20,10,40,35,80,35,50,55,10,55,30,40,70,50,10,35,85,40,90,30);
// extracts all unique elements into a new array
// result: "10, 20, 40, 35, 80, 50, 55, 30, 70, 85, 90"
echo join(", ", array_unique($numbers));
?>
4. Write a PHP program to sort a multidimensional array using multiple keys.
<?php
// create a multidimensional array
$data = array();
$data[0] = array("title" => "Net Force", "author" => "Clancy,
Tom", "rating" => 4);
$data[1] = array("title" => "Every Dead Thing", "author" =>
"Connolly, John", "rating"=> 5);
$data[2] = array("title" => "Driven To Extremes", "author" =>
"Allen, James", "rating" => 4);
$data[3] = array("title" => "Dark Hollow", "author" =>
"Connolly, John", "rating" => 4);
$data[4] = array("title" => "Bombay Ice", "author" =>
"Forbes, Leslie", "rating" => 5);
// separate all the elements with the same key
// into individual arrays
foreach ($data as $key=>$value) {
$author[$key] = $value['author'];
$title[$key] = $value['title'];
$rating[$key] = $value['rating'];
}
// sort by rating and then author
array_multisort($rating, $author,
$data); print_r($data);
?>
5. Write a PHP script to protect a publicly-displayed e-mail address from
being captured
by an e-mail address harvester.
<?php
// function to protect
// publicly-displayed e-mail addresses
// replace @ with "at"
// . with "dot"
// - with "dash"
// _ with "underscore"
function protectEmail($email) {
// define array of search and replacement terms
$search = array(".", "-", "_", "@");
$replace = array(" dot ", " dash ", " underscore ", " at ");
// perform search and replace operation
return str_replace($search, $replace, $email);
}
// result: "dontap at cst dash a dot acm dot
org" print
protectEmail("[email protected]");
?>
6.Write a PHP program to check if a number is an Armstrong number or not
PHP Code:
<?php
function armstrong_number($num) {
$sl = strlen($num);
$sum = 0;
$num = (string)$num;
for ($i = 0; $i < $sl; $i++) {
$sum = $sum + pow((string)$num{$i},$sl);
}
if ((string)$sum == (string)$num) {
return "True";
} else {
return "False";
}
}
echo "Is 153 Armstrong number? ".armstrong_number(153);
echo "\nIs 21 Armstrong number? ".armstrong_number(21);
echo "\nIs 4587 Armstrong number? ".armstrong_number(4587);"\n";
?>

7.Palindrome number Program in PHP

<?php

$num = 121;
$p=$num;
$revnum = 0;
while ($num != 0)
{
$revnum = $revnum * 10 + $num % 10;
//below cast is essential to round remainder towards zero
$num = (int)($num / 10);
}

if($revnum==$p)
{
echo $p," is Palindrome number";
}
else
{
echo $p." is not Palindrome number";
}

?>

8. Factorial of a number in PHP


<?php
$num = 4;
$factorial = 1;
for ($x=$num; $x>=1; $x--)
{
$factorial = $factorial * $x;
}
echo "Factorial of $num is $factorial";

?>

Output: Factorial of 4 is 24
9.Fibonacci Series Program in PHP

<?php

function printFibonacci($n)
{

$first = 0;
$second = 1;

echo "Fibonacci Series \n";

echo $first.' '.$second.' ';

for($i = 2; $i < $n; $i++){

$third = $first + $second;

echo $third.' ';

$first = $second;
$second = $third;

}
}
/* Function call to print Fibonacci series upto 6 numbers. */

printFibonacci(6);

?>

10.program to find prime numbers in between a Range

<?php

error_reporting(E_ALL);

$num =23;

for( $j = 2; $j <= $num; $j++ )


{
for( $k = 2; $k < $j; $k++ )
{
if( $j % $k == 0 )
{
break;
}

}
if( $k == $j )
echo Prime Number : $j;

}?>

You might also like