You are on page 1of 12

PHP @(ABM SCHOOL NOTES)

Q1. What is server side scripting? How does it work?


Ans: Web browsers communicate with Web Servers using
the HyperText Transfer Protocol(HTTP). When you click a link on a web page,
submit a form, or run a search, an HTTP request is sent from your browser to
the target server.
The request includes a URL identifying the affected resource, a method that
defines the required action (for example to get, delete, or post the resource), and
may include additional information encoded in URL parameters (the field-value
pairs sent via a (Query string), as POST data (data sent by the HTTP POST
method), or in associated cookies.
Web servers wait for client request messages, process them when they arrive, and
reply to the web browser with an HTTP response message. The response
contains a status line indicating whether or not the request succeeded (e.g.
"HTTP/1.1 200 OK" for success).
The body of a successful response to a request would contain the requested
resource (e.g. a new HTML page, or an image, etc...), which could then be
displayed by the web browser.

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

Response from a client-side script is Response from a server-side script is


faster because the scripts are processed slower because the scripts are
on local computer processed remotely

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)

1. Write a PHP code to display "Welcome to PHP"

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

3. Write a PHP code to print the square and cube of 5

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

8. Write a PHP code to find the number is positive or negative


<html>
<body>
<?php
echo "<h1> Number is Positive or Negative </h1><br>";
$a = -10;
if ($a > 0)
{
echo "$a is the postive number";
}
else
echo "$a is the negative number";
?>
</body>
</html>

9. Write a PHP code to find the smallest among two numbers


<html>
<body>
<?php
echo "<h1>smallest Number </h1><br>";
$a = 10;
$b = 12;
if ($a < $b)
{
echo "$a is smallest number";
}
else
echo "$b is the smallest number";
?>
</body>
</html>
10. Write a PHP code to find the greatest among three numbers
<html>
<body>
<?php
echo "<h1> greatest Number </h1><br>";
$a = 10;
$b = 12;
$c = 23;
if ($a > $b && $a > $c)
{
echo "$a is greatest number";
}
else if($b > $c)
{
echo "$b is the greatest number";
}
else
{
echo "$c is greatest number";
}
?>
</body>
</html>

11. Write a code to display message using alert in PHP


<html>
<body>
<?php
echo '<script>alert("Welcome to PHP program")</script>';
?>
</body>
</html>

12. Write a code to display message using alert in PHP function


<html>
<body>
<?php
function function_alert($message) {
echo "<script>alert('$message');</script>";
}
function_alert("Welcome to PHP programming");
?>
</html>
</body>
13. Example of Case sensitive variables in PHP
<html>
<body>
<?php
$Roll = 67;
$roLL = 90;
print("Variable Roll is $Roll<br>");
print("Variable roLL is $roLL<br>");
?>
</body>
</html>

14. Print the days of a week using switch in PHP


<html>
<body>
<?php
echo "<h1> Days in a week </h1><br>";
$day = "Sunday";
switch($day)
{
case "Sunday":
echo "It's Sunday!";
break;
case "Monday":
echo "It's Monday!";
break;
case "Tuesday":
echo "It's Tuesday!";
break;
case "Wednesday":
echo "It's Wednesday!";
break;
case "Thursday":
echo "It's Thursday!";
break;
case "Friday":
echo "It's Friday!";
break;
case "Saturday":
echo "It's Saturday!";
break;
default:
echo "There are only 7 days in a week.";
}
?>
</body>
</html>
15. Program to check the connection with MySQL
<html>
<body>
<?php
$servername="localhost";
$username="root";
$password="";
$connection= mysqli_connect($servername,$username,$password);
if($connection->connect_error)
{
die("Connection failed");
}
else
{
echo "You are connected successfully";
}
?>
</body>
</html>
16. Create a database "registration" in MySQL using PHP
<html>
<body>
<?php
$servername="localhost";
$username="root";
$password="";
$conn=mysqli_connect($servername,$username,$password);
if($conn->connect_error)
{
die("Connection failed");
}
else
{
echo "It is connected successfully";
}
$sql="CREATE DATABASE registration";
$result = mysqli_query($conn, $sql);
if($result)
{
echo "Database is created successfully";
}
else
{
echo "Database is not created successfully";
}
?>
</body>
</html>
17. Create a table named "students" in the database "registration" with
following fields:
roll INT(5) PRIMARY KEY,
firstname VARCHAR(30) NOT NULL,
lastname VARCHAR(30) NOT NULL,
class INT(5) NOT NULL

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

You might also like