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

PHP File

The document contains 10 PHP code snippets that demonstrate various PHP functions and concepts: 1. A script showing local, global, and static variable scope by calling a function that outputs variable values. 2. A script that reverses a number, converts it to a string, and calculates the sum of its digits. 3. A script that generates the first 20 prime numbers using a while loop. 4. A script that calculates the factorial of a given number using a for loop. 5. A script that generates the first 20 Fibonacci numbers using a recursive function. 6. A script implementing at least seven numeric functions like ceil, floor, sqrt, etc. 7.

Uploaded by

Hrishabh Rajput
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)
208 views

PHP File

The document contains 10 PHP code snippets that demonstrate various PHP functions and concepts: 1. A script showing local, global, and static variable scope by calling a function that outputs variable values. 2. A script that reverses a number, converts it to a string, and calculates the sum of its digits. 3. A script that generates the first 20 prime numbers using a while loop. 4. A script that calculates the factorial of a given number using a for loop. 5. A script that generates the first 20 Fibonacci numbers using a recursive function. 6. A script implementing at least seven numeric functions like ceil, floor, sqrt, etc. 7.

Uploaded by

Hrishabh Rajput
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/ 7

1. Write the PHP script to show local, global and static scope of a variable.

<?php
$x=2;
function sum()
{
static $a=3;
$x=6;
echo "static=".$a."<br>";
$a+=5;
echo "static=".$a."<br>";
echo "local=".$x."<br>";
$x+=5;
echo "local=".$x."<br>";
}
sum();
sum();
echo $x;
?>

2. Write the PHP script to reverse a given number and calculate its sum.

<html>
<body>
<form name="f1" action="reverse.php" method="POST">
<input type="number" name="t1" placeholder="Enter the num">
<input type="submit">
</Form>
<?php
$a=$_POST['t1'];
$b=strrev($a);
echo $b.",";
$sum=0;
while($b!=0)
{
$sum=$sum+($b%10);
$b=($b/10);
}
echo $sum;
?>
</body>
</html>

3. Write a PHP script to generate a list of first 20 prime numbers.

<?php
$count=0;
$num=2;
while($count<20)
{
$countt=0;
for($i=1;$i<=$num;$i++)
{
if(($num%$i)==0)
{
$countt++;
}
}
if($countt<3)
{
echo $num.",";
$count=$count+1;
}
$num=$num+1;
}
?>
4. Write a PHP script to calculate factorial of a given number.

<html>
<body>
<form name="f1" action="factorialtrayush.php" method="POST">
<input type="number" name="t1" placeholder="enter the number">
<input type="submit">
</form>
<?php
$x=$_POST['t1'];
$y=1;
for($i=$x;$i>0;$i--)
{
$y=$i*$y;
}
echo $y;
?>
</body>
</html>

5. Write a PHP script to generate a series of first 20 Fibonacci number using


recursive function.

<html>
<body>
<form name="f1" action="fibonacci.php" method="POST">
<input type="number" name="number" placeholder="Enter the number">
<input type="submit">
</form>
<?php
if($_POST)
{
$num = $_POST['number'];
function series($num)
{
if($num == 0)
{
return 0;
}
else if( $num == 1)
{
return 1;
}
else
{
return (series($num-1) + series($num-2));
}
}
for ($i = 0; $i < $num; $i++)
{
echo series($i);
echo "\n";
}
}
?>
</body>
</html>

6. Write a PHP script to implement atleast seven numeric functions.

<html>
<body>
<form name="f1" action="numericfunction.php" method="POST">
<input type="float" name="t1" placeholder="enter the number">
<input type="number" name="t2" placeholder="enetr the number">
<input type="float" name="t3" placeholder="enetr the number">
<input type="submit">
</form>
<?php
$a=$_POST['t1'];
$b=$_POST['t2'];
$c=$_POST['t3'];
echo ceil($a).",";
echo floor($c).",";
echo sqrt($b).",";
echo decoct($b).",";
echo octdec($b).",";
echo decbin($b).",";
echo bin2hex($b).",";
echo rand($b,$b+15).",";
echo pow($b,$b).",";
?>
</body>
</html>

7. Write php script to compare two given string.

<html>
<body>
<form name="f1" action="comparestring.php" method="POST">
<input type="text" name="t1" placeholder="Enter first string">
<input type="text" name="t2" placeholder="Enter the second string">
<input type="submit">
</form>
<?php
$a=$_POST['t1'];
$b=$_POST['t2'];
if(strcmp($a,$b)==0)
{
echo "same";
}
else
{
echo "not same";
}
?>
</body>
</html>
8. Write php script to check whether a given string is uppercase, lowercase or
mixture

<html>
<body>
<form name="f1" action="uppercase.php" method="POST">
<input type="text" name="t1" placeholder="Enter the string">
<input type="submit">
</form>
<?php
$a=$_POST['t1'];
if($a==strtoupper($a))
{
echo "uppercase";
}
else if($a==strtolower($a))
{
echo "lowercase";
}
else
{
echo "mixcase";
}
?>
</body>
</html>
9. Write the php script to check whether a string is palindrome or not.

<html>
<body>
<form name="f1" action="palindrome.php" method="POST">
<input type="number" name="t1" placeholder="Enter the number">
<input type="submit">
</form>
<?php
$a=$_POST['t1'];
if($a==strrev($a))
{
echo "palindrome";
}
else
{
echo "non palindrome";
}
?>
</body>
</html>

10. Write the PHP script to display all details of PHP server use phpinfo().

You might also like