Professional Documents
Culture Documents
Q2 What is PHP?
Ans: PHP (recursive acronym for PHP: Hypertext Preprocessor) is a widely-used
open source general-purpose scripting language that is especially suited for web
development and can be embedded into HTML.
Example:
<!DOCTYPE html>
<html>
<head>
<title>Example</title>
</head>
<body>
<?php
echo "Hi, I'm a PHP script!";
?>
</body>
</html>
Common uses of PHP
> by using PHP, you can create, open, read, write and close files
> PHP can handle forms, i.e gather data from files, save data to a file, through
email you can send data, return data to the user.
>You can add, delete, and modify elements within your database through PHP
>Access cookies variables and set cookies
>It can encrypt data
Q3. Write the features of PHP? @(ABM SCHOOL NOTES)
Ans: Following are the features of PHP:
> PHP script is executed much faster than those scripts which are written in other
languages such as JSP and ASP.
> PHP source code and software are freely available on the web. All its
components are free to download and use.
> PHP has easily understandable syntax. Programmers are comfortable coding
with it.
>PHP code can be easily embedded within HTML tags and script.
>PHP is available for WINDOWS, MAC, LINUX& UNIX operating system.
>PHP supports all the leading databases such as MySQL, SQLite, ODBC, etc.
> PHP allows us to use a variable without declaring its datatype. It will be taken
automatically at the time of execution based on the type of data it contains on its
value.
>PHP is a secure language to develop the website. It consists of multiple layers
of security to prevent threads and malicious attacks.
Q4 Write difference between Client Side Scripting and Server Side
Scripting.
Ans: Following are the differences between Client Side scripting and Server Side
scripting:
Client Side Scripting Server Side Scripting
The Web Browser executes the client The Web Server executes the server
side scripting that resides at the user's side scripting that produces the page to
computer be sent to the browser
Client side scripting cannot be used to Server side scripting is used to connect
connect to the databases on the web to the databases that reside on the web
server server
Client side scripting cannot access the Server side scripting can access the
file system that resides at the web file system residing at the web server
server
Client side scripting is possible to be Server side scripting cannot be
blocked by the user blocked by the user
User can view source code from the User cannot view the source code from
web browser the web browser
Examples of Client side scripting Examples of Server side scripting
languages are: JavaScript, VB script languages are: PHP, JSP, ASP, Ruby,
etc Perl etc
PHP codes @(ABM SCHOOL NOTES)
<html>
<body>
<?php
echo "Welcome to PHP";
?>
</body>
</html>
2. Write a PHP code to display first name, last name, class and college.
<html>
<body>
<?php
$fname="Birat ";
$lname=" Gautam";
$roll=12;
$college="Uniglobe HSS";
echo"My name is:".$fname,$lname;
?>
</body>
</html>
<html>
<body>
<?php
echo "Square and Cube of a number <br>";
$num = 5;
$sq = $num * $num;
$cu = $num * $num * $num;
echo "Square is $sq and Cube is $cu";
?>
</body>
</html>
4. Write a PHP code to find the area and perimeter of rectangle
<html>
<body>
<?php
echo "Area and perimeter of square <br>";
$n= 5;
$area = $n * $n;
$peri = 4 * $n;
echo "Area is $area and Perimeter is $peri";
?>
</body>
</html>
5. Write a PHP code to print the sum and product of two numbers
<html>
<body>
<?php
echo "Sum and product of numbers <br>";
$a = 5;
$b = 6;
$sum = $a + $b;
$pdt = $a * $b;
echo "Sum is $sum and Product is $pdt";
?>
</body>
</html>
6. Write a PHP code to find the number is divisible by 5 or not
<html>
<body>
<?php
echo "<h1> Divisible by 5 or not </h1><br>";
$a = 15;
if ($a % 5==0)
{
echo "$a is divisible by 5";
}
else
echo "$a is not divisible by 5";
?>
</body>
</html>
7. Write a PHP code to find the number is odd or even
<html>
<body>
<?php
echo "<h1> Number is Odd or Even </h1><br>";
$a = 1;
if ($a % 2==0)
{
echo "$a is the even number";
}
else
echo "$a is the odd number";
?>
</body>
</html>
<html>
<body>
<?php
$servername="localhost";
$username="root";
$password="";
$dbname="registration";
$conn=mysqli_connect($servername,$username,$password, $dbname);
if($conn->connect_error)
{
die("Connection failed");
}
else
{
echo "It is connected successfully";
}
$sql="CREATE TABLE users (
userIdINT(5) PRIMARY KEY,
username VARCHAR(30) NOT NULL,
password VARCHAR(30) NOT NULL )";
$result = mysqli_query($conn, $sql);
if($result)
{
echo "Table is created successfully";
}
else
{
echo "Table is not created successfully";
}
?>
</body>
</html>
18. Write a PHP code to insert atleast two data in the table "students"
<html>
<body>
<?php
$servername="localhost";
$username="root";
$password="";
$dbname="register";
$connection=
mysqli_connect($servername,$username,$password,$dbname);
if($connection->connect_error)
{
die("Connection failed".mysqli_connect_error());
}
else
{
echo "You are connected successfully";
}
$sql="INSERT INTO users (rollno, firstname,lastname,class)
VALUES (1,'ram','gurung',12),(2,'shyam','Rai',11)";
$result = mysqli_query($connection, $sql);
if($result)
{
echo "Data is inserted successfully";
}
else
{
echo "Data is not inserted successfully";
}
?>
</body>
</html>
19. Write a PHP code to read the data and print it on the screen
<html>
<body>
<?php
//connect to the server and select database
$servername="localhost";
$username="root";
$password="";
$dbname="register";
$conn = mysqli_connect($servername,$username,$password,$dbname);
if($conn->connect_error)
{
die("Connection failed");
}
//query the database for user
echo "<b><center>Database Output</center></b><br><br>";
$sql = "SELECT * from users";
$result = mysqli_query($conn, $sql);
if(mysqli_num_rows > 0)
{
while($row = mysqli_fetch_assoc())
{
echo "<br>UserID: ".$row['userId']. "- Name: ".$row['username']. " -Password:
".$row['password']."<br>";
}
}
else
{
echo "0 results";
}
$conn->close();
?>
</body>
</html>
20. Write a PHP program to display ID, Roll, Name, Class and Section of
different students in a table format
<!DOCTYPE html>
<html lang="en">
<head>
<title>PHP display data</title>
</head>
<body>
<h2>Displaying Student data</h2>
<?php
$servername="localhost";
$username="root";
$password="";
$dbname="EXAM";
$conn = mysqli_connect($servername,$username,$password,$dbname) or
die("Connection failed");
$sql ="SELECT * from student";
$result=mysqli_query($conn,$sql);
if(mysqli_num_rows($result)>0){
echo
"<table><tr><th>ID</th><th>Name</th><th>Roll</th><th>Class</th><th>Se
ction</th></tr>";
while($row=mysqli_fetch_assoc($result)){
echo
"<tr><td>".$row['ID']."</td><td>".$row['sname']."</td><td>".$row['Roll']."</t
d><td>".$row['class']."</td><td>".$row['Section']."</td></tr>";
}
}
else
{
echo "0 result";
}
?>
</body>
</html>