Table of Contents
Unit1 Orientation and First Step ..................................................................................................1
Definition: ...............................................................................................................................1
1.1 PHP’s Place in the web world ............................................................................................1
1.2 What's So Great About PHP? .............................................................................................3
1.3 PHP in Action ....................................................................................................................3
1.4 Basic Rules of PHP Programs. ...........................................................................................5
1.4.1 Start and End Tags: .....................................................................................................5
1.4.2 Whitespace and Case-Sensitivity .................................................................................6
1.4.3 Comments in PHP .......................................................................................................7
Unit1 Orientation and First Step
Definition:
PHP is an acronym for "PHP: Hypertext Preprocessor"
PHP is a widely-used, open source scripting language
PHP scripts are executed on the server
PHP is free to download and use
PHP supports many database ( MYSQL, oracle, Informix, etc. )
1.1 PHP’s Place in the web world
PHP is a programming language that’s used mostly for building websites. Instead of a PHP
program running on a desktop computer for the use of one person, it typically runs on a web server
and is accessed by lots of people using web browsers on their own computers. This section explains
how PHP fits into the interaction between a web browser and a web server.
When you sit down at your computer and pull up a web page using a browser such as Safari or
Firefox, you cause a little conversation to happen over the Internet between your computer and
another computer. This conversation, and how it makes a web page appear on your screen, is
illustrated in Figure 1.
Here’s what’s happening in the numbered steps of the diagram:
1. You type www.example.com/catalog.html into your browser’s location bar.
2. The browser sends a message over the Internet to the computer named www.example.com asking
for the /catalog.html page.
3. Apache HTTP Server, a program running on the www.example.com computer, gets the message
and reads the catalog.html file from its disk drive.
4. Apache sends the contents of the file back to your computer over the Internet as a response to the
browser’s request.
5. Your browser displays the page on your screen, following the instructions of the HTML tags in
the page
1
Figure 1 Client and server communication without PHP
Every time a browser asks for http://www.example.com/catalog.html, the web server sends back
the contents of the same catalog.html file. The only time the response from the web server changes
is if someone edits the file on the server.
When PHP is involved, however, the server does more work for its half of the
conversation. Figure 2 shows what happens when a web browser asks for a page that is generated
by PHP.
Figure 2. Client and server communication with PHP
2
Here’s what’s happening in the numbered steps of the PHP-enabled conversation:
1. You type www.example.com/catalog/yak.php into your browser’s location bar.
2. Your browser sends a message over the Internet to the computer named www.example.com asking
for the /catalog/yak.php page.
3. Apache HTTP Server, a program running on the www.example.com computer, gets the message
and asks the PHP engine, another program running on the www.example.com computer, “What
does /catalog/yak.php look like?”
4. The PHP engine reads the file yak.php from the disk drive.
5. The PHP engine runs the commands in yak.php, possibly exchanging data with a database program
such as MySQL.
6. The PHP engine takes the yak.php program output and sends it back to Apache HTTP Server as
an answer to “What does /catalog/yak.php look like?”
7. Apache HTTP Server sends the page contents it got from the PHP engine back to your computer
over the Internet in response to your browser’s request.
8. Your browser displays the page on your screen, following the instructions of the HTML tags in
the page.
1.2 What's So Great About PHP?
PHP runs on various platforms (Windows, Linux, Unix, Mac OS X, etc.)
PHP is compatible with almost all servers used today (Apache, IIS, etc.)
PHP supports a wide range of databases
PHP is free. Download it from the official PHP resource: www.php.net
PHP is easy to learn and runs efficiently on the server side
PHP Is Widely Used
1.3 PHP in Action
When given a program to run, the PHP engine pays attention only to the parts of the program
between PHP start and end tags. Whatever’s outside those tags is printed with no modification.
This makes it easy to embed small bits of PHP in pages that mostly contain HTML. The PHP
engine runs the commands between <?php (the PHP start tag) and ?> (the PHP end tag). PHP
pages typically live in files whose names end in .php. Example 1-1 shows a page with one PHP
command.
3
Example 1-1. Hello, World!
<html>
<head><title>PHP says hello</title></head>
<body>
<b>
<?php
print "Hello, World!";
?>
</b>
</body>
</html>
The output of Example 1-1 is:
<html>
<head><title>PHP says hello</title></head>
<body>
<b>
Hello, World!</b>
</body>
</html>
In your web browser, this looks like Figure 1-3.
4
Figure 1-3. Saying hello with PHP
1.4 Basic Rules of PHP Programs.
1.4.1 Start and End Tags:
<?php as the PHP start Tag and ?> as the PHP end tag. The PHP interpreted ignores anything
outside of those tags. Text before the start tag and after the end tag is printed with no interference
from the PHP interpreter. A PHP program can have multiple start and end pairs.
Example 1-8. Multiple start and end tags
Five plus five is:
<?php print 5 + 5; ?>
<p>
Four plus four is:
<?php
print 4 + 4;
?>
<p>
<img src="vacation.jpg" alt="My Vacation" />
5
The PHP source code inside each set of <?php ?> tags is processed by the PHP engine, and the
rest of the page is printed as is. Example 1-8 prints:
Five plus five is:
10<p>
Four plus four is:
8<p>
<img src="vacation.jpg" alt="My Vacation" />
1.4.2 Whitespace and Case-Sensitivity
PHP is insensitive of whitespace. This includes all type of spaces that are invisible on the screen
including tabs, spaces, and carriage return. Even one space is equal to any number of spaces or
carriage return. This means that php will ignore all the spaces or tabs in a single row or carriage
return in multiple rows. Unless a semi-colon is encountered, PHP treats multiple lines as a single
command.
The spacing in Examples 1-9 and 1-10 is bad. Instead, format your code as in Example 1-11.
Example 1-9.
<?php print "Hello"; print " World!"; ?>
Example 1-10.
<?php
print "Hello";
print " World!";
6
?>
Example 1-11. This PHP is just right
<?php
print "Hello";
print " World!";
?>
1.4.3 Comments in PHP
A comment in PHP code is a line that is not executed as a part of the program. Its only purpose is
to be read by someone who is looking at the code.
Comments can be used to:
Let others understand your code
Remind yourself of what you did - Most programmers have experienced coming back to
their own work a year or two later and having to re-figure out what they did. Comments
can remind you of what you were thinking when you wrote the code
PHP supports several ways of commenting:
Single Line Comment: As the name suggests these are single line or short relevant
explanations that one can add in code. To add this, we need to begin the line with ( // ) or
(#).
Example:
<?php
//this is a single line comment
echo “hello world”;
#this is also single line comment
?>
Multi-line comment: these are used to accommodate multiple lines with a single tag and
can be extended to many line as required by user. To add this, we need to begin and end
line with (/*……..*/)
Example:
7
<?php
/* this is multi line comment
In PHP variables are written by adding a $ sign at the beginning */
$a=”hello”;
Echo $a;
?>