0

Simple PHP script for practice to learners and students

To print a string using php

<?php

echo “Welcome to learn PHP”;

print “Enjoy it”;

?>

output

Welcome to learn PHP

Enjoy it

Note: Using echo and print, you can display the string in php

To display a number from 1 t0 5  in for loop

<?php

for($i=1; i<=5; $i++)
{
echo $i;
echo “<br/>”;
}

?>

output

1
2
3
4
5

To find the factorial of a number
<?php
$num = 5;
$factorial = 1;
for ($i=$num; $i>=1; $i–) {
$factorial = $factorial * $i;

}
echo “Factorial of $num is $factorial”;
?>

output

Factorial of 5 is 120

To sort a number in ascending and descending order

<?php
$numbers=array(13,0,7,4,6,5,3,2,1,9,8);
sort($numbers);
$arrlength=count($numbers);
echo “The Values are sorted in ascending order”;
echo “<br>”;
for($i=0;$i<$arrlength;$i++)
{
echo $numbers[$i];
echo “<br>”;
}
$numbers1=array(13,0,7,4,6,5,3,2,1,9,8);
rsort($numbers1);
$arrlength1=count($numbers1);
echo “The Values are sorted in descending order”;
echo “<br>”;
for($i=0;$i<$arrlength1;$i++)
{
echo $numbers1[$i];
echo “<br>”;
}
?>

output

The Values are sorted in ascending order

0 1 2 3 4 5 6 7 8 9 13

The Values are sorted in descending order

13 9 8 7 6 5 4 3 2 1 0

To sort a names in ascending order

<?php
$names=array(“Ramu”, “Raja”, “Babu”, “Govind”, “vinayak”, “Kalki”, “Sunil”, “Thiyagu” );
sort($names);
$arrlength=count($names);
for($x=0;$x<$arrlength;$x++)
{
echo $names[$x];
echo “<br>”;
}
?>

output

Babu
Govind
Kalki
Raja
Ramu
Sunil
Thiyagu
vinayak

To print the reverse number of a given number

<?php
$num = 12345;
$rem =0;
$rev =0;
$input = $num;
while($num > 1) {
$rem = $num %10;
$rev = ($rev * 10)+ $rem;
$num = $num /10;

}
echo “Given Number: “. $input;
echo “<br/>”;
echo ” Reverse Number : “. $rev;
?>

output

Given Number: 12345
Reverse Number : 54321

To print the reverse string of a given string

<?php
$string = “hello”;
$i = 0;
while(isset($string[$i]))
{
$i++;
}
$i–;
while(isset($string[$i]))
{
echo $string[$i];
$i–;
}
?>

output

olleh

To print the fibbonacci series based on the count

<?php
$count=0;
$f1=0;
$f2=1;
echo $f1.”,”;
echo $f2.”,”;
while($count<8)
{
$f3=$f2+$f1;
echo $f3.” , “;
$f1=$f2;
$f2=$f3;
$count=$count+1;
}

?>

output

0,1,1 , 2 , 3 , 5 , 8 , 13 , 21 , 34

To print first 20 prime numbers
<?php
$count = 0 ;
$number = 2 ;
while ($count < 20 ) {
$divcount=0;
for ( $i=1;$i<=$number;$i++)
{
if (($number%$i)==0) {
$divcount++;
}
}
if ($divcount<3) {
echo $number.” , “;
$count=$count+1;
}
$number=$number+1;
}
?>

output

2 , 3 , 5 , 7 , 11 , 13 , 17 , 19 , 23 , 29 , 31 , 37 , 41 , 43 , 47 , 53 , 59 , 61 , 67 , 71

To check whether the given string is a palindrome or not
<?php
function checkPalindrome( $string )
{
$string = str_replace( ‘ ‘, ”, $string );
return $string == strrev( $string );
}
$string = ‘mad am’;
if( checkPalindrome( $string ) == true )
{
echo ‘Given string is a palindrome’;
}
else
{
echo ‘Given string is not a palindrome’;
}
?>

output

Given string is a palindrome

 

Filed in: PHP

Recent Posts

Bookmark and Promote!

Leave a Reply

Submit Comment
© 2017 Expert Shout. All rights reserved. XHTML / CSS Valid.