Lab Session 9
Lab Session 9
Department of CS and SE
Objective
On completion of this lab, students will be able:
Introduction to PHP
PHP started out as a small open source project that evolved as more and more people found out how
useful it was. Rasmus Lerdorf unleashed the first version of PHP way back in 1994.PHP is a recursive
acronym for "PHP: Hypertext Preprocessor”. PHP is a server side scripting language that is embedded
in HTML. It is used to manage dynamic content, databases, session tracking, even build entire e-
commerce sites. It is integrated with a number of popular databases, including MySQL, PostgreSQL,
Oracle, Sybase, Informix, and Microsoft SQL Server.
PHP is pleasingly zippy in its execution, especially when compiled as an Apache module on the UNIX
side. The MySQL server, once started, executes even very complex queries with huge result sets in record
setting time.
PHP supports a large number of major protocols such as POP3, IMAP, and LDAP. PHP4 added support
for Java and distributed object architectures (COM and CORBA), making n-tier development a
possibility for the first time. PHP is forgiving: PHP language tries to be as forgiving as possible. PHP
Syntax is C-Like. Common uses of PHP.
PHP performs system functions, i.e. from files on a system it can create, open, read, write, and close
them. 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 add, delete, and modify elements within your database through PHP.
Access cookies variables and set cookies. Using PHP, you can restrict users to access some pages of your
website. It can encrypt data.
Setup
In order to develop and run PHP Web pages three vital components need to be installed on your
computer system.
Web Server − PHP will work with virtually all Web Server software, including Microsoft's Internet
Information Server (IIS) but then most often used is freely available Apache Server. Download Apache
for free here − https://httpd.apache.org/download.cgi
Web Engineering
Department of CS and SE
Database − PHP will work with virtually all database software, including Oracle and Sybase but most
commonly used is freely available MySQL database. Download MySQL for free here −
https://www.mysql.com/downloads/
PHP Parser − In order to process PHP script instructions a parser must be installed to generate HTML
output that can be sent to the Web Browser. This tutorial will guide you how to install PHP parser on
your computer.
Example 1
<!DOCTYPE html>
<html>
<body>
<?php
echo "Hello World!";
?>
</body>
</html>
Example 2
<?php
$txt = "Hello world!";
$x = 5;
$y = 10.5;
?>
<! DOCTYPE html>
<html>
<body>
<? Php
$txt = "W3Schools.com";
echo "I love " . $txt . "!";
?> </body>
</html>
<!DOCTYPE html>
<html>
<body>
<?php
echo "<h2>PHP is Fun!</h2>";
echo "Hello world!<br>";
echo "I'm about to learn PHP!<br>";
echo "This ", "string ", "was ", "made ", "with multiple parameters.";
?> </body>
</html>
PHP String
<?php
$x = "Hello world!";
$y = 'Hello world!'; echo $x;
echo "<br>";
echo $y;
?>
Web Engineering
Department of CS and SE
Lab Task
Note: Use proper naming conventions and also add comments.
1. Write a PHP script to get the PHP version and configuration information. (Hint: Use phpinfo()
)
2. $var = 'PHP Tutorial'. Put this variable into the title section and in the h3 tag
within an HTMLdocument and produce the following output.
Sample Output:
“PHP Tutorial PHP, an acronym for Hypertext Preprocessor, is a widely-used
open source general- purpose scripting language. It is a cross-platform, HTML
embedded server-side scripting language and is especially suited for web
development.”
3. Write a PHP function to test whether a number is greater than 30, 20 or 10 using
ternary operator.Take the number in a variable.
4. Write a script using one variable “$whatsit” to print the following to the browser
Value is string.
Value is double.
Value is boolean.
Value is integer. Value is NULL.
(Hint: Use gettype() builtin function to get the type of variable).
5. Write a PHP program to swap values of two integer variables without using any third variable.
6. Write a PHP program to print out the multiplication table upto 5*5 Output:
2345
4 6 8 10
6 9 12 15
8 12 16 20
10 15 20 25
12 18 24 30