web programming introduction to php com427 1. objectives to understand what php is and how a php...
TRANSCRIPT
Web Programming
Introduction to PHP
COM427 1
Objectives
• To understand what PHP is and how a PHP script works with a Web Browser and a Web Server
• To learn what software and components you need to get started with PHP
• To create and run a simple PHP script
COM427 2
Web servers
• Can be considered at two levels:1. The physical computer that stores web files and web application
programs.2. More precisely, the web server is a specialized piece of software,
running on the server computer, which• receives requests, via the Internet, for web pages or programs• retrieves the pages / executes the programs• returns the results over the Internet to the browser.
• Common web servers:– Apache– Microsoft Internet Information Services (IIS)
COM427 3
Web Application Program
• Carries out many dynamic tasks, such as the following: – Input a search term, search the WWW, and
return the results– Calculate and display the number of times that a
page has been viewed– Verify the input fields on a Web form– Save a Web form into a database– Display a special graph, or return the results of a
calculation based on data input from a form
COM427 4
What is PHP?
• Original of PHP (Rasmus Lerdorf) [born in Qeqertarsuaq, Greenland]
• Server side – e.g. form handling, database access– (as opposed to Javascript which is
mainly clientside)• (X)HTML-embedded scripting language
COM427 5
Basic Browser Web Server interaction
6
1. Browser in Client sents http request e.g. http://localhost/~labuser/test.php
2. Server receives request and activates php to interpret php program (test.php)
3. Server sends html output to browser in client for display
PHP Advantages
Advantages of Using PHP to enhance Web pages:
– Easy to use.– Open source.– Multiple platform.
COM427 7
Browser PHP MySQL Interaction
8
A third layer can be added if php makes a call to a database to supply data in response to web browser request
Browser/PHP/database example
9
Browser holds html form
Enter Details of Car
Reg Num
Make
Submit
PHP program- Receives values- Plugs them into database
query- Sends Request to database- Receives data and - Returns result as web page
Details of Car
Taxed ? YesMOT? YesReg Date: 1st Jan 2010
https://www.gov.uk/check-vehicle-tax
Car Database
PHP & Javascript
• Javascript– Client side– Good for form validation– Good for web page presentation– Poor for security– Some database access
• PHP– Server side– Better security– Good database access
COM427 10
Getting Started with PHP
To develop and publish PHP scripts all you need is:
– A Web server with PHP built into it– A client machine with a basic text editor and Internet
connection– FTP software (if webserver used) Use localhost in the development phase and then
upload to the server
COM427 11
Installation of Apache, PHP and MySQL
• In the lab: Apache server, PHP, MySQL installed
• At home: – Install Apache, PHP and MySQL separately, – Or install Apache, PHP, MySQL together using a
package, eg. WAMP or MAMP (for Windows or Mac)
– XAMPP installs very easily & is cross-platform (Windows or Mac)
COM427 12
Install Apache, PHP & MySQL• Download XAMPP
http://sourceforge.net/projects/xampp
Installs Apache/PHP/MySQL (plus PERL)
Cross platform (Mac/Windows/Linux)
COM427 13
Using Apache, PHP and MySQL• iMAC Lab
– Save your HTML file (e.g. test.html) or PHP file (e.g. myprog.php) under: Macintosh ID/Users/Labuser/Sites
– Run html or php file under Apache using: e.g. http://localhost/~labuser/test.html OR create subdirectory starting with b (e.g. student number
b00XXXXXX ) http://localhost/~labuser/b00XXXXXXClick on file in browser window
• Windows Lab/ Own Windows Machine[ Put files in C://xampp/htdocs e.g. hello.php URL to run file http://localhost/hello.php OR create subdirectory and click on browser list]
COM427
14
Using MySQL
– http://localhost/phpMyAdmin MAC Lab Login (MySQL)
Login: labuser Password: macimd15
Windows Lab login (MySQL)Login: labuserPassword: Labuser1
COM427 15
Creating a PHP Script File
You can use a number of different editors to create your PHP script files.
• TextWrangler (can add line numbers)• Save file with .php extension
[In Windows Lab can use Notepad++ to create PHP scripts]
COM427
16
Working with HTML
• Use tags <?php // opening php tag
?> // closing php tag
Save the file with .php extension
COM427 17
Accessing Your File Using a Browser
COM427 18
You should use this URL in the iMac LAB:
• for html files: http://localhost/~labuser/page1.html
• for php files: http://localhost/~labuser/ex1.php
Print Statement Syntax
• The print statement syntax:
COM427 19
print ( "Your message to print" );
Enclose messagein quotation
marks
Message to Output
End in asemi-colon
Parenthesis areoptional
Echo Statement Syntax
• The echo statement syntax:
echo "the message to print";
Echo almost identical to print
COM427 20
If Use Improper Syntax
• Suppose you use the wrong syntax: 1. <?php2. print "A simple initial script;
3. ?>
COM427 21
Missing inverted commas
A Little About PHP's Syntax
• Some PHP Syntax Issues: – Be careful to use quotation marks, parentheses, and brackets in
pairs. – Most PHP commands end with a semicolon (;).– Be careful of case.
– PHP ignores blank spaces.
COM427 22
Embedding PHP Statements Within HTML Documents
• One way to use PHP is to embed PHP scripts within HTML tags in an HTML document. (prog1.php)
1. <html>2. <head> 3. <title>HTML With PHP Embedded</title> </head>4. <body> 5. <font size=5 color="blue">Welcome To My Page</font>6. <?php7. print "<br /> Using PHP gets easier as you keep practising it<br />";8. ?>9. </body></html>
(PHP code alone will run fine since system will construct an html output for browser automatically)
COM427 23
Using Backslash (\) to Generate HTML Tags with print()
• Sometimes you want to output an HTML tag that also requires double quotation marks. – Use the backslash ("\") character to signal that the double
quotation marks themselves should beoutput:print "<font color=\"blue\">";
– The above statement would output: <font color="blue">
COM427 24
Using Comments with PHP Scripts
• Comments enable you to include descriptive text along with the PHP script.– Comment lines are ignored when the script
runs; they do not slow down the run-time.– Comments have two common uses.
• Describe the overall script purpose.• Describe particularly tricky script lines.
COM427 25
Using Comments with PHP Scripts
• Comment Syntax - Use //<?php// This is a comment?>
• Can place on Same line as a statement:<?phpprint "A simple initial script"; //Output a line?>
COM427 26
Example Script with Comments (prog2.php)
1. <html> <head>2. <title> Generating HTML From PHP</title> </head>3. <body> <h1> Generating HTML From PHP</h1>4. <?php5. //6. // Example script to output HTML tags7. //8. print "Using PHP has <i>some advantages:</i>";9. print "<ul><li>Speed</li><li>Ease of Use </li><li>Functionality
</li></ul>"; //Output bullet list10. print "</body></html>";11. ?>
COM427 27
Alternative Comment Syntax
• PHP allows a couple of additional ways to create comments.<?phpphpinfo(); # This is a built-in function?>
• Multiple line comments. <?php/*A script that gets information about thePHP version being used.*/phpinfo(); ?>
COM427 28
Summary
• You can embed a PHP script within an HTML document or run it as a stand-alone script.
• To begin working with PHP you need a Web server with built-in PHP, a client machine with a basic text editor
• PHP script process: write the PHP script, copy its file to the Web server, and access the file with a Web browser.
• Comments can be preceded by two forward slashes (//).
COM427 29
Contents to be covered (Wk 1-8)• Variables, Operations and Expressions• Control statements
– if statements– for & while loops
• Arrays – 1-dimensional – 2-dimensional (tables)
• Functions• Form handling• File handling
COM427 30
Resources
• www.w3schools.com/php/• www.php.net/manual/
COM427 31