Web Technologies Practical's by Om Waman
Web Technologies Practical's by Om Waman
Web Technologies Practical's by Om Waman
01
AIM : Write a program in PHP to Demonstrate looping statement in PHP.
1. For loop:
<?php
for($n=1;$n<=5;$n++){
echo"$n<br/>";
}
?>
Output:
2. Foreach loop:
<?php
$season=array("summer","winter","spring","autumn");
foreach( $season as $arr ){
echo "Season is: $arr<br />";
}
?>
Output:
3. While loop:
<?php
$n=1;
while($n<=5){
echo "$n<br/>";
$n++;
}
?>
Output:
4. Do While loop:
<?php
$n=1;
do{
echo "$n<br/>";
$n++;
}while($n<=4);
?>
Output:
Practical no.02
Aim: Write a Program in PHP to Demonstrate Conditional Statements in PHP.
1. if statement
2. if else statement
3. If-else-if Statement
4. nested if Statement
1. if statement:
<?php
$num=12;
if($num<100){
echo "$num is less than 100";
}
?>
Output:
2. if else statement:
<?php
$num=12;
if($num%2==0){
echo "$num is even number";
}else{
echo "$num is odd number";
}
?>
Output:
3. If-else-if Statement:
<?php
$marks=69;
if ($marks<33){
echo "fail";
}
else if ($marks>=34 && $marks<50) {
echo "D grade";
}
else if ($marks>=50 && $marks<65) {
echo "C grade";
}
else if ($marks>=65 && $marks<80) {
echo "B grade";
}
else if ($marks>=80 && $marks<90) {
echo "A grade";
}
else if ($marks>=90 && $marks<100) {
echo "A+ grade";
}
else {
echo "Invalid input";
}
?>
Output:
4. nested if Statement
<?php
$age = 23;
$nationality = "Indian";
//applying conditions on nationality and age
if ($nationality == "Indian")
{
if ($age >= 18) {
echo "Eligible to give vote";
}
else {
echo "Not eligible to give vote";
}
}
?>
Output:
Practical no. 03
Aim :- Write a program in PHP to create an array, insert elements in array, accessing element from array
and displaying elements of array.
<?php
$season=array("summer","winter","spring","autumn");
echo "Season are: $season[0], $season[1], $season[2] and $season[3]";
?>
Output:
<?php
//declare array
$season = array ("Summer", "Winter", "Autumn", "Rainy");
Output:
Practical no. 04
Aim:- Write a program in PHP to demonstrate including multiple files in PHP webpage
menu.php
<?php
echo '<a href="/default.asp">Home</a> -
<a href="/html/default.asp">HTML Tutorial</a> -
<a href="/css/default.asp">CSS Tutorial</a> -
<a href="/js/default.asp">JavaScript Tutorial</a> -
<a href="default.asp">PHP Tutorial</a>';
?>
footer.php
<?php
echo "<p>Copyright © 1999-" . date("Y") . " Practicalexam.com</p>";
?>
index.php
<html>
<body>
<div class="menu">
<?php include 'menu.php';?>
</div>
Output:
Practical No .05
Aim :- Write a program in PHP to creating and calling your own function.
<?php
function sayHello(){
echo "Hello PHP Function";
}
sayHello();//calling function
?>
Output :-
Practical: no. 06
Aim:- write a program in PHP to declare a class, create an object , demonstrate writing method and
declaring properties, accessing object.
<?php
class cars {
// Properties
var $name;
var $color;
// Methods
function set_name($name) {
$this->name = $name;
}
function get_name() {
return $this->name;
}
function set_color($color) {
$this->color = $color;
}
function get_color() {
return $this->color;
}
}
$BMW = new cars(); // creating object
$audi = new cars();
$volvo = new cars();
$BMW->set_name('BMW ');
$BMW->set_color('red');
$audi->set_name('audi ');
$audi->set_color('blue');
$volvo->set_name('volvo ');
$volvo->set_color('black');
echo $BMW->get_name();
echo " --> ";
echo $BMW->get_color();
echo "<br>";
echo $audi->get_name();
echo " --> ";
echo $audi->get_color();
echo "<br>";
echo $volvo->get_name();
echo " --> ";
echo $volvo->get_color();
?>
Output:
Practical: no. 07
<?php
$str1="PHP";
$str2=" Programming";
echo "Original : $str1 and $str2<br>";
$l1=strlen($str1);
$l2=strlen($str2);
echo "Length : $l1 and $l2<br>";
?>
Output:
Practical: no. 08
Aim:- Write a program in PHP to create/design a User Registration Form, validate form data and display
entered form data on webpage
<?php
// define variables and set to empty values
$name = $email = $gender = $phone = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = test_input($_POST["name"]);
$email = test_input($_POST["email"]);
$phone = test_input($_POST["phone"]);
$gender = test_input($_POST["gender"]);
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<?php
echo "<h2>Your Input:</h2>";
echo $name;
echo "<br>";
echo $email;
echo "<br>";
echo $phone;
echo "<br>";
echo $gender;
?>
Output:
Practical: no. 09
Aim:- Use MySQL in command line mode for following operations:
1. Show Databases 6. Select data from a table
2. Create a Database 7. Rename a table
3. Use Database 8. Delete data from table
4. Create Table 9. Delete a table
5. Add data into a table
6) Select – this command is used to show selected data from the table.
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "adarsh";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"].
"<br>";
}
} else {
echo "0 results";
}
mysqli_close($conn);
?>
Output: