You are on page 1of 34

ISB42503 INTERNET PROGRAMMING

INTRODUCTION TO PHP

Topic to discussed
1. 2. 3. 4. 5. 6. 7. 8. 9. Basic Syntax Sending Data to the Web Browser Understanding PHP, HTML and White Space Writing Comments What are Variables? About Strings About Numbers About Constants Single vs. Double Quotation Marks

Static and dynamic web page


Static Pages
A static websites is created using HTML pages which have been written in a fixed way to instruct the browser how to render the page. (What you see is what you get). Static websites are used to display fixed information which most often does not need to be updated on a regular basis

Dynamic Pages

As the web evolved, sites became more complex and were expected to display dynamic information which could not have been hard coded in HTML:
search results number of visitors user login message boards

Dynamic Web Sites


Dynamic Web Sites are flexible, accurately describes as applications than merely sites:
Respond to different parameters (for example, the time of day or the version of the visitors Web browser Have a memory allowing user registration & login Involve HTML forms, so that people can perform searches, provide feedback, etc Often have interfaces where administrators can manage the sites content Easier to maintain, upgrade and build open

What is PHP?
PHP stands for PHP: Hypertext Preprocessor PHP is a server-side scripting language, like ASP PHP scripts are executed on the server PHP supports many databases (MySQL, Informix, Oracle, Sybase, Solid, PostgreSQL, Generic ODBC, etc.) PHP is an open source software (OSS) PHP is free to download and use

What is PHP?
PHP: Hypertext Preprocessor
An HTML embedded scripting language Use to develop dynamic web site
Better, faster and easier to learn Designed to do something only after event occurs. e.g. when user submit forms

A server-side language, the code resides on a host computer that serves Web pages to the requesting visitors (client).
URL request client HTML

server
HTML PHP Script request

What is MySQL?
DBMS for relational databases (RDBMS) A database being a collection of interrelated data, for text, numbers or binary files. Open source database

URL request client server HTML HTML PHP Script request Query Data MySQL

What is a PHP File?


PHP files may contain text, HTML tags and scripts PHP files are returned to the browser as plain HTML PHP files have a file extension of ".php", ".php3", or ".phtml"

Why PHP?
PHP runs on different platforms (Windows, Linux, Unix, etc.) PHP is compatible with almost all servers used today (Apache, IIS, etc.) PHP is FREE to download from the official PHP resource: www.php.net PHP is easy to learn and runs efficiently on the server side

Development Tools
Web server application Apache or IIS PHP MySQL Web browser mozilla, internet explorer Text editor, PHP capable applicationMacromedia Dreamweaver,IDE

Basic syntax
PHP is HTML embedded scripting language PHP code is enclosed with PHP tags written in formal or informal style
<?php . ?>
formal (XML style)

<? . ?>

informal

Intermingle PHP and HTML code within the same script <html> <title>Basic PHP </title> <? //comment write php code here ?> </html>

Basic syntax
To place PHP code within HTML document, use
<?php ?>

Save the file as .php Run the file in web browser

http://localhost/filename.php

Sending data to web browser


Language constructs in PHP to display message
echo() print ()

For example, <?php echo This was done using PHP; ?> Will display: This was done using PHP

PHP, HTML & White Space


What you are doing with PHP is creating the HTML source of a Web Page
view source in Web browser

PHP is generating the HTML source (in web browser)

PHP, HTML & White Space


There are 3 areas where you can affect spacing :
in your PHP scripts In HTML source In the rendered Web page

PHP is generally white space insensitive, meaning that you can space out your code to make your scripts more legible.

To create white space


To alter the spacing of the finished Web page, use the HTML tags : To alter the spacing of the HTML source created with PHP:
Use echo() or print() over the course of several lines Use the newline character (\n) within the double quotation marks. <br /> (line break) <p></p> (paragraph).

Writing comments in PHP


Viewable in the source but not in the browser window PHP supports three comment types:
# This is a comment
Right after the initial PHP tag a.k.a introductory block

// This is also a comment


In one line

/* This is a longer comment that spans two lines */

Variables
PHP syntactical rules for creating variables:
1.
2.

3.
4. 5.

A variables name (identifier) must start with a dollar sign ($), for example $name Can contain combination of strings, numbers & underscore ($my_report1) First character after dollar sign ($) must be either a letter or an underscore (it cannot be a number) Variable names in PHP are case-sensitive. Variables can be assigned using the equal sign (=) to print out PHPs predefined variables

/*
$_SERVER refers to mass of server-related information, like the name of the script being run ($_SERVER['PHP_SELF']), the Web browser and operating system of the user accessing the script ($_SERVER['HTTP_USER_AGENT']) and the IP address of the user accessing the script ($_SERVER['REMOTE_ADDR']) */

String
A quoted chunk of letters, numbers, spaces, punctuation .. Example strings hello software 1000 12 January, 2006 String variable assign a string value to valid variable name $today =16 July, 2007; To print out echo $today; echo Today is $today; Concatenation string Addition of strings using period (.). $day=12; $month=January; $year =2006; $today = $day . . $month . . $year Use it extensively when building database queries in later chapters

Variables: Strings
A string is a quoted chunk of letters, numbers, spaces, punctuations, etc. E.g.
Ali In my mind 1000 9 july 1978

Strings are case sensetive so $Welcome_Text is not the same as $welcome_text When assigning numbers to strings you do not need to include the quotes so: $user_id = 987 would be allowed.

Outputting Variables
To display a variable on the screen uses exactly the same code as to display text but in a slightly different form. The following code would display your welcome text: <? $welcome_text = "Hello and welcome to my website."; print($welcome_text); ?> As you can see, the only major difference is that you do not need the quotation marks if you are printing a variable. String variables are created and their values sent to the Web browser in this script

Concatenating strings
$city = Ipoh; $state = Perak
$address = $city . $state ; Will display: IpohPerak To improve: $address = $city . , . $state ;
Add a comma and a space

About Numbers
Functions round() $j = 3.14; $k = round( $j); number_format() $p =20980; $g=number_format($p); $g=number_format($p,2);

Using numbers
Valid number-type variables can be like: 8, 3.14, 109080808, -4.524 To format the number into thousands and round it to two decimal places: $total = number_format ($total, 2);

Numbers
$n = 3.14; $n = round ($n); // 3 rounds a decimal to the nearest integer $n = 3.142857; $n = round ($n, 3); // 3.143 rounds to specified number of decimal places $n = 20943; $n = number_format ($n); // 20, 943 grouped into thousands using commas $n = 20943; $n = number_format ($n, 2); // 20, 943.00 set a specified number of decimal points

Arithmetic operators
+ addition - subtraction * multiplication / division % modular ++ increment -- decrement

Constants
Specific data type Retain initial value throughout script Cannot change once it has been set Use define() define (AGE, value); Print constant echo Hello, . AGE; OR echo Hello,, AGE;

Example of script using constants

Single vs. Double Quotation Marks

Single quote -> values treated literally Double quote -> interpolated Example
$var =Hello; echo var equal to $var; echo var equal to $var; echo \$var is equal to $var;

var equal to hello

var equal to $var

$var is equal to hello

Single vs. Double Quotation Marks

=> replace variables name with its value and a special characters code (\$) with its represented value => display exactly what you type, except for the escaped single quote (\) and the escape backslash(\\).

Single vs Double Quotation Marks


Using double quotation marks: echo "You are purchasing <b>$quantity</b> widget(s) at a cost of <b>\$$price</b> each. With tax, the total comes to <b>\$$total</b>.\n"; Using single quotation marks echo 'You are purchasing <b>$quantity</b> widget(s) at a cost of <b>\$$price</b> each. With tax, the total comes to <b>\$$total</b>.\n'; The script to demonstrate the difference between using single and double quotation marks

Single vs Double Quotation Marks

cont

Exercises
Give an example of static web site that you know. Discuss what are the advantages and disadvantages of static website.

You might also like