Server-Side Scripting (PHP)
1.
Simple php program.
First.php
<!DOCTYPE html>
<html>
<head>
<title>My First Php Program</title>
</head>
<body>
<h1>My First PHP Page</h1>
<?php
echo "Welcome to my page";
?>
</body>
</html>
2.
Simple php program
for echo keyword.
Case.php
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<h3>Echo keyword is insensitive</h3>
<?php
echo "Welcome to my page <br>";
ECHO "Welcome to my page <br>";
EchO "Welcome to my page <br>";
?>
</body>
</html>
3.
Simple php program to
demonstrate variable declaration.
Variable.php
<?php
$a=20;
$c=15;
function myFunction()
{
$b=10;
global $c;
echo "<p>Value of a inside function is :
$a</p>";
echo "<p>Value of b inside function is : $b</p>";
echo "<p>Value of c inside function is :
$c</p>";
}
myFunction();
echo "<p>Value of a outside function is :
$a</p>";
echo "<p>Value of b outside function is :
$b</p>";
?>
4.
Simple php program
for static keyword.
Static.php
<?php
function myCount()
{
static $c=0;
echo $c;
$c++;
}
echo "Output of myCount() with use of 'static' keyword :
<br>";
myCount();
echo "<br>";
myCount();
echo "<br>";
myCount();
?>
5.
Simple php program to
demonstrate var_dump() method.
Datatype.php
<?php
echo "<br>---String---<br>";
$x="Hello World !";
echo var_dump($x);
echo "<br>---Decimal---<br>";
$x=1234.20;
echo var_dump($x);
echo "<br>---Integer---<br>";
$x=1234;
echo var_dump($x);
?>
6.
Simple php program to
find out greater number using if statement.
Greater.php
<?php
$a=10;
$b=20;
if($a>$b)
{
echo "$a is greater than $b";
}
else
{
echo "$b is greater $a";
}
?>
7.
Simple php program to
print table of 2 using for loop.
Tab.php
<?php
$a=2;
for($i=1;$i<=10;$i++)
{
echo "<br>".$a*$i;
}
?>
8.
Simple php program to
demonstrate various string functions.
String.php
<?php
$str="PHP runs on various platforms.";
echo "Entered String is :- ".$str;
echo "<br> String Length :- ".strlen($str);
echo "<br>Total Words in a string :-
".str_word_count($str);
echo "<br> Reverse String :- ".strrev($str);
echo "<br>Position of 'o' in a string :-
".strpos($str,"o");
echo "<br>Replace PHP with php :-
".str_replace("PHP","php",$str);
echo "<br>Extract some part of string :-
".substr($str,6,10);
echo "<br>Convert string to lowercase :-
".strtolower($str);
echo "<br>Count the occurance of substracted string :-
".substr_count($str);
echo "<br>Capitalize each word of string :-
".ucwords($str);
?>
9.
Simple php program to
create indexed array.
array.php
<?php
$flowers=array("Rose","Marigold","Lotus","Jasmine","Mogara");
echo "I Like these flowers ".$flowers[0]." ,
".$flowers[1]." , ".$flowers[2]." ,
".$flowers[3]." , ".$flowers[4];
echo "<br> Count Elements of Array :
".count($flowers);
?>
10. Simple php program to
create indexed array.
Foreach.php
<?php
$flowers=array("Rose","Marigold","Lotus","Jasmine","Mogara");
echo "Flower names are :- <br>";
foreach($flowers as $value)
{
echo
"$value<br>";
}
?>
11. Simple php program to
create associative array.
Associative.php
<?php
$mobiles=array("Redmi"=>"6000","Samsung"=>"8000","Oppo"=>"7500","Vivo"=>"9000","Oneplus"=>"20000");
echo "<br> Redmi Phone cost is :-
".$mobiles['Redmi'];
echo "<br> Oppo Phone cost is :-
".$mobiles['Oppo'];
?>
12. Simple php program to
create multidimensional array.
multi.php
<?php
$cars=array(
array("Volvo",23,19),
array("BMW",16,18),
array("Hyundai",20,17),
array("Land Rover",22,20)
);
echo $cars[0][0]." : In stock : ".$cars[0][1]." ,
Sold : ".$cars[0][2]."<br>";
echo $cars[1][0]." : In stock : ".$cars[1][1]." ,
Sold : ".$cars[1][2]."<br>";
echo $cars[2][0]." : In stock : ".$cars[2][1]." ,
Sold : ".$cars[2][2]."<br>";
echo $cars[3][0]." : In stock : ".$cars[3][1]." ,
Sold : ".$cars[3][2]."<br>";
?>
13. Simple php program to
perform addition using user defined function.
add.php
<?php
function add()
{
$a=10;
$b=30;
$c=$a+$b;
echo "Addition of $a and $b is : ".$c;
}
add(); //Calling of function
?>
14. Simple php program to
demonstrate parameterized function.
Stud.php
<?php
function stud($rno,$sname)
{
echo "Roll no is $rno and Name is $sname <br>";
}
stud(101,"Ajay");
stud(102,"Ritesh");
stud(103,"Sonam");
stud(104,"Sonakshi");
?>
15. Simple php program using
return keyword.
Return.php
<?php
function sum(int $x, int $y)
{
$z=$x+$y;
return $z;
}
echo "5 + 10 = ".sum(5,10)."<br>";
echo "5 + 10 = ".sum(7,13)."<br>";
?>
16. Simple php program to
demonstrate form handling.
Welcome.html
<!DOCTYPE html>
<html>
<head>
<title>Form Handeling</title>
</head>
<body>
<form action="welcome.php"
method="post">
Name : <input type="text" name="name">
<br>
Email : <input type="text" name="email">
<br>
<input type="submit">
</form>
</body>
</html>
welcome.php
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
Welcome
<?php
echo $_POST["name"];
?> <br>
Your email address is :
<?php
echo $_POST["email"];
?>
</body>
</html>
17. Simple php program of
addition using form.
Add1.php
<?php
if(isset($_POST["submit"]))
{
$num1=$_POST["n1"];
$num2=$_POST["n2"];
$result=$num1+$num2;
//echo "Addition of $num1 and $num2 is : ".$result;
}
?>
Add1.html
<html>
<head>
<title>Form Handling</title>
</head>
<body>
<form method="POST" action="add1.php">
Number 1 : <input type="number"
name="n1"> <br>
Number 2 : <input type="number"
name="n2"> <br>
Number 3 : <input type="number"
name="result" value="<?php echo $result
?>"><br>
<input type="submit" name="submit">
</form>
</body>
</html>
18. Simple php program to
calculate bmi of user.
Bmi.html
<!DOCTYPE html>
<html>
<head>
<title>BMI Calculator</title>
</head>
<body>
<form method="get" action="bmi.php">
Weight in Kg :- <input type="text" name="weight">
<br>
Height in Cm :- <input type="text"
name="height"><br>
<input type="submit" name="submit"
value="Calculate">
</form>
</body>
</html>
bmi.php
<?php
$height=$_GET["height"];
$weight=$_GET["weight"];
$heighInMs=$height/100;
$bmi=$weight/($heighInMs*$heighInMs);
if($bmi < 18.5)
{
$message = "You are underweight";
}
else if($bmi >=18.5 && $bmi <=24.9)
{
$message = "Congrats!! You have normal weight";
}
elseif($bmi >24.9 && $bmi<=29.9)
{
$message = "You are overweight.";
}
else
{
$message = "Be careful!! You are Obese";
}
echo "<br> Your BMI is :
".$bmi."<br>";
echo $message;
?>
No comments:
Post a Comment