Scripting Language Unit 3 To 6 Lab Report
Scripting Language Unit 3 To 6 Lab Report
1
TITLE:
A PHP program to show an echo method can take multiple parameters.
THEORY:
Display methods in PHP:
Echo and print are two display methods in PHP. Echo can take multiple parameters
while print can take only a single parameter. Also, echo doesn’t return anything while
print always returns 1. Since the echo method doesn’t return anything, it is a faster
method than print.
SOURCE CODE:
<?php
echo ("Hello World, How are you ?"); // single parameter
echo"<br>";
echo "Hello World, ", "How are you ?"; //two parameters
echo"<br>";
echo "Hello ", "World, ", "How are you ?"; //three parameters
?>
OUTPUT:
CONCLUSION:
Thus, it is concluded that the echo method can accept multiple parameters.
LAB 4.2
TITLE:
A PHP program to illustrate sorting of associative array.
THEORY:
Associative Array in PHP:-
Associative array is array with a key associated with each value of the array.
Syntax:
array(key1=>value1, key2=>value2,...............keyN=>valueN);
SOURCE CODE:
<?php
$age = array('Ram'=>45,'Hari'=>17,'Sita'=>28, 'Gita'=>52);
asort($age);
print_r($age);
echo'<br>';
arsort($age);
print_r($age);
echo'<br>';
ksort($age);
print_r($age);
echo'<br>';
krsort($age);
print_r($age);
?>
OUTPUT:
CONCLUSION:
Thus, it is concluded that
a) asort() method sorts associative array in ascending order w.r.t. value
b) arsort() method sorts associative array in descending order w.r.t. value
c) ksort() method sorts associative array in ascending order w.r.t. key
d) krsort() method sorts associative array in descending order w.r.t. key
LAB 4.3
TITLE:
A PHP program to show form data submitted using get method.is visible in URL.
THEORY:
IN PHP, get method is used to collect form data submitted to URL. Form data submitted
using the get method are available across all PHP pages in an application using $_GET
superglobal. Only a limited amount of data can be sent using get method and are visible
in the URL. So, data submitted with get method are not secured and can be bookmarked.
SOURCE CODE:
//forms.php
<html>
<body>
<form method = 'get' action = 'get.php'>
<label>Name</label>
<input type='text' name='name' ><br><br>
<label>Mobile</label>
<input type='text' name='mobile' ><br><br>
<input type = 'submit' value='submit'>
</form>
</body>
</html>
//get.php
<?php
print_r("Mobile Number of {$_GET['name']} is {$_GET['mobile']}");
?>
OUTPUT:
Input:
Output:
CONCLUSION:
Thus, it is concluded that form data submitted using get method are visible in URL.
LAB 4.4
TITLE:
A PHP program to submit form data using the post method.
THEORY:
In PHP, the post method is used to submit the form data to the webpage. Unlimited
amount of data can be sent, and it is not visible in the URL. So, form data submitted with
the post method is more secure and can’t be bookmarked. Both ASCII and binary data are
allowed to be submitted.
SOURCE CODE:
//form.php
<html>
<body>
<form method = 'post' action = 'postm.php'>
<label>Email</label>
<input type='email' name='name' ><br><br>
<label>Password</label>
<input type='password' name='mobile' ><br><br>
<input type = 'submit' value='submit'>
</form>
</body>
</html>
//post.php
<?php
print_r("Mobile Number of {$_POST['name']} is {$_POST['mobile']}");
?>
OUTPUT:
Input:
Output:
CONCLUSION:
Thus, It is concluded that form data can be submitted using the post method in PHP.
LAB 5.1
TITLE:
A program to illustrate including one PHP file into another PHP file.
THEORY:
In PHP, we can insert one PHP file into another one using two methods:
1) using include statement
2) Using require statement.
Main difference between these two is that the include statement produces a warning
message only and execution of the script goes on while require statement produces a fatal
error and stops the further execution of script.
Syntax:
include ‘filename’;
Or
require ‘filename’;
SOURCE CODE:
//main.php
<html>
<head>
<title>Including file in PHP</title>
</head>
<body>
<h1> Introduction to Nepal</h1>
</body>
<?php
require 'otherfile.php';
?>
</html>
//otherfile.php
<?php
echo" Nepal is a landlocked country. <br>";
echo "Nepal is land of Buddha";
?>
OUTPUT:
CONCLUSION:
Thus, inclusion of one PHP file into another PHP file can be done using the include
statement.
LAB 5.2
TITLE:
A PHP program to upload files to server.
THEORY:
To upload file in PHP, 2 things must be taken into consideration.
i) Form method must be of post type (i.e. method = ‘post’)
ii) Form needs enctype attribute which determines the content type the form
submits(i.e. enctype=multipart/form-data)
Superglobal $_FILES is used to access all files submitted using the post method.
SOURCE CODE:
//form1.php
<html>
<body>
<form method = 'post' action = 'post1.php' enctype='multipart/form-data'>
<input type = 'file' name = 'file1' > <br>
<input type = 'submit' value='upload'>
</form>
</body>
</html>
//post1.php
<?php
$n = $_FILES['file1']['name'];
$tn = $_FILES['file1']['tmp_name'];
if(move_uploaded_file($tn, $n))
{
echo "file uploaded successfully";
}
?>
OUTPUT:
Input:
Output:
CONCLUSION:
Thus, uploaded files in PHP can be accessed using superglobal $_FILES in PHP.
LAB 5.3
TITLE:
A PHP program to read the existing content of a text file followed by appending
additional content to it.
THEORY:
PHP provides following file handling functions
i. fopen(filename, mode)
ii. fclose(filename )
iii. fread($handle, no of bytes to be read from)
iv. fwrite($handle, string to be written into )
v. rename(oldfilename, newfilename)
vi. unlink(filename) :-deletes the file
vii. filesize(filename) :-returns the size of file in bytes
viii. file-exists(filename) :-returns true if mentioned file exists else false
SOURCE CODE:
<?php
$handle = fopen('xyz.txt', 'a+');
$contents = fread($handle, filesize('xyz.txt'));
echo "File content before append:- ".$contents;
echo'<br>';
fwrite($handle, 'I am fine');
echo"File contents after append:-";
echo file_get_contents('xyz.txt');
fclose($handle);
?>
OUTPUT:
CONCLUSION:
Thus, it is concluded that we can read and write external files using file handling
functions in PHP.
LAB 5.4
TITLE:
A PHP program to set and access session variables.
THEORY:
Small piece of information about the user which is stored in the server is called session.
Session values are assigned and accessed using superglobal $_SESSION. We have to
start a session using session_start() each time whether we want to create or access or
modify or delete session variables. .
SOURCE CODE:
//setsession.php
<?php
session_start();
$_SESSION['Name'] = 'Ujjwal';
$_SESSION['Roll'] = '32';
?>
//accessession.php
<?php
session_start();
echo "Roll number of {$_SESSION['Name']} is
{$_SESSION['Roll']}";
?>
OUTPUT:
On executing setsession.php then accessession.php
We got:
CONCLUSION:
Thus, Session variables can be set and accessed using superglobal $_SESSION in PHP.
LAB 6.1
TITLE:
A program to connect the MySQL database to the server using PHP.
THEORY:
There are two popular ways for connecting MySQL database to server namely
1) procedural method
mysqli_connect($servername, $username, $password, $dbname);
2) Object oriented method
new mysqli($servername, $username, $password, $dbname);
SOURCE CODE:
<?php
$servername = "localhost";
$username = "root";
$password = "";
// Creating connection
$conn = mysqli_connect($servername, $username, $password);
// Checking connection
if ($conn)
echo "Connection is successfully established";
else
die ("Connection is failed to be established");
?>
OUTPUT:
CONCLUSION:
Thus, MySQL databases can be successfully connected to the server using the
mysqli_connect() method in PHP.
LAB 6.2
TITLE:
A program to create a MySQL database using PHP.
THEORY:
MySQL database can be created using CREATE DATABASE statement as query in PHP
Syntax for Creating Database:
CREATE DATABASE database_name;
SOURCE CODE:
<?php
$servername = "localhost";
$username = "root";
$password = "";
// Creating connection
$conn = mysqli_connect($servername, $username, $password);
// Checking connection
if ($conn)
echo "Connection is successfully established";
else
echo "Connection is failed to be established";
?>
OUTPUT:
CONCLUSION:
Thus, MySQL databases can be created using the CREATE DATABASE statement in
PHP.
LAB 6.3
TITLE:
A program to create a MYSQL database table using PHP.
THEORY:
We use the MariaDB CREATE TABLE statement to create a new database table using
PHP.
Syntax for creating table:
CREATE TABLE [if not exists] table_name( column_1_definition,
column_2_definition,
..…………………..
………………………
column_n_definition.
table_constraints)
engine=storage_engine;
SOURCE CODE:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "SL";
// Creating connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Checking connection
if ($conn)
echo "Connection is successfully established !!";
else
die("Connection is failed to be established !!");
OUTPUT:
CONCLUSION:
Thus, MySQL databases can be created using CREATE TABLE statement in PHP.
LAB 6.4
TITLE:
A program to insert data into MySQL database table using PHP.
THEORY:
Values can be inserted into MySQL database table using the INSERT INTO statement in
PHP.
Syntax:
INSERT INTO table_name (column1, column2, column3, ...)
VALUES(value1, value2,..........);
SOURCE CODE:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "SL";
// Creating connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Checking connection
if ($conn)
echo "Connection is successfully established !!";
else
die("Connection is failed to be established !!");
if($result)
echo"<br> Values are inserted in table named fruits successfully !!";
else
echo"<br> Values can't be inserted into table named fruits !!";
?>
OUTPUT:
CONCLUSION:
Thus, values can be inserted into MySQL database table using INSERT INTO statement
in PHP.