12/6/2011
Computer Science Dept
PHP INTRODUCTION Lecture 5
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 Machine PHP supports many databases (MySQL, Informix, Oracle, Sybase, Solid, PostgreSQL, Generic ODBC, etc.) PHP is an open source software
12/6/2011
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
Required Software
Download PHP
Download PHP for free here: http://www.php.net/downloads.php
Download MySQL Database
Download MySQL for free here: http://www.mysql.com/downloads/index.html
Download Apache Server
Download Apache for free here: http://httpd.apache.org/download.cgi
You need to configure all these software to be able to communicate with each other
12/6/2011
Php Installation
There some of the already configured software which contain php, mysql, apache and the database administration tool (php myadmin). Some of them are
Easyphp Xamp Wamp etc
Just download and install
Client (browser) requests a page on a Web server Web server executes the program The Web server program accesses (for instance) a database on the Web server machine
12/6/2011
Basic PHP Syntax
A PHP scripting block always starts with <?php and ends with ?>. A PHP scripting block can be placed anywhere in the document.
Example
<html> <body> <?php echo "Hello World"; ?> </body> </html>
12/6/2011
Code Embeddings and Comments The following example illustrates three ways of embedding code in a PHP document embed.php: <html><head><title>Embeddings</title></head> <body> <h2>Tree forms for embedding PHP in HTML</h2> <ol> <? echo "<li>The simple form</li>"; ?> <?php echo "<li>A slightly longer form</li>"; ?> <script language="PHP"> // Comments in PHP code is not sent to the browser echo "<li>The somewhat longer form</li>"; </script> </ol> </body> </html>
Comments in PHP
In PHP, we use // to make a single-line comment or /* and */ to make a large comment block. <html> <body> <?php //This is a single comment /* This is a commentblock */ ?> </body> </html>
12/6/2011
Variables in PHP
Variables are used for storing a values, like text strings, numbers or arrays. When a variable is declared, it can be used over and over again in your script. All variables in PHP start with a $ sign symbol.
Naming Rules for Variables
A variable name must start with a letter or an underscore "_" A variable name can only contain alpha-numeric characters and underscores (a-z, A-Z, 0-9, and _ ) A variable name should not contain spaces. If a variable name is more than one word, it should be separated with an underscore ($my_string), or with capitalization ($myString)
12/6/2011
Example of variables
<?php $txt="Hello World!"; $x=16; ?>
<html><head><title>Home page</title></head> <body> <h2> <? $name = "Martin Elsman"; echo "Home page for "; echo $name; ?> </h2> <hr /> This page is maintained by <? echo $name ?> </body> </html>
12/6/2011
Computation with Numbers
There are two types of numbers in PHP: 1. Integers: 3, 9, 0, -1, ... 2. Doubles (double precision): 3.0, 9.2, 0.13, 23.2, ... The usual number operations (e.g., +,-,*, and /) can be used in computations.
Example dollars.php:
<html><head><title>Dollars</title></head> <body> <h2>Dollars</h2> <? $rate = 8.43; $kroner = 1000.0; $dollars = ($kroner - 20.0) / $rate; echo "For $kroner you receive $$dollars"; ?> </body> </html>
12/6/2011
Conditional Statements in PHP
If-statements are used for executing different code depending on some condition The general format: if ( condition1 ) { statement1 } elseif ( condition2 ) { statement2 } else { statement3 }
Example account.php:
<html><head><title>Account</title></head> <body> <? $dollars = 8; if ( $dollars == 1 ) { echo "You have 1 Dollar on you account"; // if-branch } else { echo "You have $dollars Dollars on your account"; // else-branch } ?> </body> </html>
12/6/2011
Comparison Operators and their Precedence
Logical Operators and their Precedence
10
12/6/2011
PHP Loops
In Php we have different loops which are used when we execute some of the information which are repetitive
For-loop While loop For each loop ect
For-loops
A for-loop takes the following form: for ( initialization ; condition ; incrementation ) { statement ; } It works as follows: (1) Evaluate initialization (2) Evaluate condition; if 0 (FALSE) continue after the loop (3) Evaluate statement (loop body) (4) Evaluate incrementation
11
12/6/2011
Loops in PHP
How can we output the text "I love Web programming" 20 times? Bad solution: echo "I love Web programming<br />"; ...18 times... echo "I love Web programming<br />";
love.php:
Better solution: use a while-loop for repetition $counter = 20; while ( $counter >= 1 ) { echo "I love Web programming<br />"; $counter = $counter - 1; } The statement echo is executed 20 times, with $counter = 20, 19, . . . , 1 It is important that the content of the variable $counter (i.e., a number) decreases for each execution of the loop-body.
12
12/6/2011
PHP Functions
Function is a block of code which is executed when called PHP function syntax function functionName() { code to be executed; }
Example
<html> <body> <?php function writeName() { echo "Kai Jim Refsnes"; } echo "My name is "; writeName(); ?> </body> </html>
13
12/6/2011
PHP Form Handling
Forms are used to collect user inputs After collection of users inputs, the data submitted must be processed Processing depends if the data have to be submitted into database or displayed The PHP $_GET and $_POST variables are used to retrieve information from the form
PHP Form Processing
$_GET Variable
This is a predefined variable which is used to collect values in a form with method=get Information sent with GET method is visible to everyone because it will be displayed in browser address bar This method has a limit on the amount of information to send
14
12/6/2011
$_GET method
<form action=proces.php method=get> Reg :<input type=text name=reg /> Course: <input type=text name=course /> <input type=submit /> </form> When a user click Submit Button, the URL sent to the browser will look like this www.ifm.ac.tz/students.php?reg=1233&&course=BIT
PHP Form Processing
$_POST Variable
This predefined variable is used to collect values from a form with method=post The information sent with this method is invisible to others No limit on the amount of information to send However there is 8mb max size for POST by default which can be changed in php.ini file
15
12/6/2011
$_POST Method
<form action=proces.php method=post> Reg :<input type=text name=reg /> Course: <input type=text name=course /> <input type=submit /> </form> When a user click Submit Button, the URL sent to the browser will look like this www.ifm.ac.tz/students.php
PHP form Processing
<form action=proces.php method=post> Reg :<input type=text name=reg /> Course: <input type=text name=course /> <input type=submit /> </form>
16
12/6/2011
Print Out the data from the form
<php echo My Registration number is .$_POST[regno]; echo <br />; echo My Course is . $POST[course]; ?> OutPut My Registration number is 123 MY Course is BIT
Form Validation
User Inputs should be validated to remove unwanted information This can be done in two ways
Browser validation Server Validation
Browser validation is faster and reduces server load Server validation should be considered if the user inputs are to be inserted into database
17
12/6/2011
END
18