Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 Web Programming Introduction to PHP COM427 1

2 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

3 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

4 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

5 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

6 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

7 PHP Advantages Advantages of Using PHP to enhance Web pages: – Easy to use. – Open source. – Multiple platform. COM427 7

8 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

9 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 ? Yes MOT? Yes Reg Date: 1 st Jan 2010 https://www.gov.uk/check-vehicle-tax Car Database

10 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 COM42710

11 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

12 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

13 Install Apache, PHP & MySQL Download XAMPP http://sourceforge.net/projects/xampp Installs Apache/PHP/MySQL (plus PERL) Cross platform (Mac/Windows/Linux) COM427 13

14 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/b00XXXXXX Click 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

15 Using MySQL – http://localhost/phpMyAdmin MAC Lab Login (MySQL) Login: labuser Password: macimd15 Windows Lab login (MySQL) Login: labuser Password: Labuser1 COM42715

16 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

17 Working with HTML Use tags <?php // opening php tag ?>// closing php tag Save the file with.php extension COM427 17

18 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

19 Print Statement Syntax The print statement syntax: COM427 19

20 Echo Statement Syntax The echo statement syntax: echo "the message to print"; Echo almost identical to print COM427 20

21 If Use Improper Syntax Suppose you use the wrong syntax: 1. <?php 2. print "A simple initial script; 3. ?> COM427 21 Missing inverted commas

22 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

23 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. 2. 3. HTML With PHP Embedded 4. 5. Welcome To My Page 6.<?php 7. print " Using PHP gets easier as you keep practising it "; 8.?> 9. (PHP code alone will run fine since system will construct an html output for browser automatically) COM427 23

24 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 be output: print " "; – The above statement would output: COM427 24

25 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

26 Using Comments with PHP Scripts Comment Syntax - Use // <?php // This is a comment ?> Can place on Same line as a statement: <?php print "A simple initial script"; //Output a line ?> COM427 26

27 Example Script with Comments (prog2.php) 1. 2. Generating HTML From PHP 3. Generating HTML From PHP 4. <?php 5. // 6. // Example script to output HTML tags 7. // 8. print "Using PHP has some advantages: "; 9. print " Speed Ease of Use Functionality "; //Output bullet list 10. print " "; 11. ?> COM427 27

28 Alternative Comment Syntax PHP allows a couple of additional ways to create comments. <?php phpinfo(); # This is a built-in function ?> Multiple line comments. <?php /* A script that gets information about the PHP version being used. */ phpinfo(); ?> COM427 28

29 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

30 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

31 Resources www.w3schools.com/php/ www.php.net/manual/ COM427 31


Download ppt "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."

Similar presentations


Ads by Google