Sei sulla pagina 1di 5

WEB ENGINEERING BSE-8

LAB # 11

SERVER-SIDE SCRIPTING

OBEJCTIVES:
 Introduction to PHP
 PHP Syntax
 PHP Variables
 PHP Datatypes
 PHP Echo/Print
 PHP Statements
 PHP Numbers
 PHP Strings

PHP, a Data Mover


PHP, the language that you use to write your programs, is a scripting language designed for use
on the Web. It has features to aid you in programming the tasks needed by dynamic Web
applications.

PHP stands for PHP: HyperText Preprocessor. In its early development by a guy named Rasmus
Lerdorf, it was called Personal Home Page tools. PHP is particularly strong in its ability to interact
with databases. It supports pretty much every database you’ve ever heard of.

PHP handles connecting to the database and communicating with it. You don’t need to know the
technical details for connecting to a database or for exchanging messages with it. You tell PHP the
name of the database and where it is, and PHP handles the details. It connects to the database,
passes your instructions to the database, and returns the database response to you.

How PHP works


PHP is an embedded scripting language when used in Web pages. This means that PHP code is
embedded in HTML code. You use HTML tags to enclose the PHP language that you embed in
your HTML file — the same way that you would use other HTML tags. You create and edit Web
pages containing PHP the same way that you create and edit regular HTML pages.
WEB ENGINEERING BSE-8

The PHP software works with the Web server. The Web server is the software that delivers Web
pages to the world. When you type a URL into your Web browser, you’re sending a message to
the Web server at that URL, asking it to send you an HTML file. The Web server responds by
sending the requested file.

Your browser reads the HTML file and displays the Web page. You also request the Web server
to send you a file when you click a link in a Web page. In addition, the Web server processes a
file when you click a Web page button that submits a form.

When PHP is installed, the Web server is configured to expect certain file extensions to contain
PHP language statements. Often the extension is .php or .phtml, but any extension can be used.
When the Web server gets a request for a file with the designated extension, it sends the HTML
statements as is, but PHP statements are processed by the PHP software before they’re sent to the
requester.

When PHP language statements are processed, only the output is sent by the Web server to the
Web browser. The PHP language statements are not included in the output sent to the browser,
so the PHP code is secure and transparent to the user.

Basic PHP Syntax


A PHP script can be placed anywhere in the document.

A PHP script starts with <?php and ends with ?>:


WEB ENGINEERING BSE-8

The default file extension for PHP files is ".php".

A PHP file normally contains HTML tags, and some PHP scripting code.

Below, we have an example of a simple PHP file, with a PHP script that uses a built-in PHP
function "echo" to output the text "Hello World!" on a web page:

Example # 1

<!DOCTYPE html>
<html>
<body>

<?php
echo "My first PHP script!";
?>

</body>
</html>

Example # 2
<!DOCTYPE html>
<html>
<body>

<h1>My first PHP page</h1>

<?php
echo "Hello World!";
?>

</body>
</html>
WEB ENGINEERING BSE-8

LAB TASKS

TASK # 1
How PHP scripts are executed using WAMP server? Note down the steps and take screen shot of
each step.

TASK # 2
Write a program that displays the current time in a specific location (use php built in function).

TASK # 3
Write a program that converts the given decimal number into binary, octal and hexadecimal (use
php built in function).

Task # 4
Execute the following script and observe the output:
<!DOCTYPE html>
<html>
<body>

<?php
// Positive numbers:
echo substr("Hello world",10)."<br>";
echo substr("Hello world",1)."<br>";
echo substr("Hello world",3)."<br>";
echo substr("Hello world",7)."<br>";
echo "<br>";

// Negative numbers:
echo substr("Hello world",-1)."<br>";
echo substr("Hello world",-10)."<br>";
echo substr("Hello world",-8)."<br>";
echo substr("Hello world",-4)."<br>";
?>

</body>
</html>
WEB ENGINEERING BSE-8

Task # 5
Write a script that gets the current month and prints one of the following responses, depending
on whether it's May or not:
It's May, so it's really hot.
Not May, so at least not in the peak of the heat.

Task # 6
Write a script that will print the following:

Create the 'abc' row with a while loop, the 'xyz' row with a do-while loop, and the last two sections
with for loops. Remember to include HTML and source code line breaks in your output.

Potrebbero piacerti anche