Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Web Technologies Practical's by Om Waman

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

Practical no.

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.

1. Creating and inserting element into array:

<?php

$season=array("summer","winter","spring","autumn");
echo "Season are: $season[0], $season[1], $season[2] and $season[3]";
?>

Output:

2. Accessing elements in array :

<?php
//declare array
$season = array ("Summer", "Winter", "Autumn", "Rainy");

//access array elements using foreach loop


foreach ($season as $element) {
echo "$element";
echo "</br>";
}
?>

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 &copy; 1999-" . date("Y") . " Practicalexam.com</p>";
?>

index.php

<html>
<body>

<div class="menu">
<?php include 'menu.php';?>
</div>

<h1>Welcome to my home page!</h1>


<p>Some text.</p>
<p>Some more text.</p>
<?php include 'footer.php';?>
</body>
</html>

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

Aim:- write a program in PHP to demonstrate string functions

<?php
$str1="PHP";
$str2=" Programming";
echo "Original : $str1 and $str2<br>";

$l1=strlen($str1);
$l2=strlen($str2);
echo "Length : $l1 and $l2<br>";

$count = str_word_count($str1 . $str2);


echo "Word count : $count <br>";

$add = $str1 . $str2;


echo "Addition of Strings : $add <br>";

$replace = str_replace("Programming", "Language", "PHP Programming");


echo "New String : $replace";

?>

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;
}
?>

<h2>User Registration Form</h2>


<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
Name: <input type="text" name="name">
<br><br>
E-mail: <input type="text" name="email">
<br><br>
phone: <input type="text" name="phone">
<br><br>
Gender:
<input type="radio" name="gender" value="male">Male
<input type="radio" name="gender" value="female">Female
<input type="radio" name="gender" value="other">Other
<br><br>
<input type="submit" name="submit" value="Submit">
</form>

<?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

1) Show Database - it is used to show database from MySQL host.

COMMAND: SHOW DATABASES;

2) Create database - It Is Use To Create A Database In MySQL

COMMAND: CREATE DATABASE ADARSH;


3) Use Database - this command is used to use the given database.
COMMAND: USE ADARSH;

4) Create Table: This command is use to create table in MySQL host.

COMMAND: create table students( ID INT(5) PRIMARY KEY, NAME VARCHAR(20));


5) INSERT - this command is use to insert data into table .

COMMAND: INSERT INTO STUDENTS(ROLL_NO, NAME) VALUES(1,"OMWAMAN");

6) Select – this command is used to show selected data from the table.

COMMAND: SELECT * FROM `student

7) Rename a table : this command is used to rename the table.

COMMAND: RENAME TABLE student TO computer_science;


8) Delete data from table: this command is used to delete selected data from the table.

COMMAND: DELETE FROM students WHERE id=3;

9) Delete a table: this command is used delete the table.

COMMAND: drop TABLE student;


Practical: no. 10
Aim:- Write a program in PHP to Connecting to MySQL and selecting the Database Executing Simple
Queries, and Retrieving Query Results.

<?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());
}

$sql = "SELECT id, firstname, lastname FROM students";


$result = mysqli_query($conn, $sql);

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:

You might also like