Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to PHP. PHP PHP is the Hypertext Pre-processor –Script language –Embedded into HTML –Runs as Apache module –Can use DB (MySQL, Oracle, Microsoft.

Similar presentations


Presentation on theme: "Introduction to PHP. PHP PHP is the Hypertext Pre-processor –Script language –Embedded into HTML –Runs as Apache module –Can use DB (MySQL, Oracle, Microsoft."— Presentation transcript:

1 Introduction to PHP

2 PHP PHP is the Hypertext Pre-processor –Script language –Embedded into HTML –Runs as Apache module –Can use DB (MySQL, Oracle, Microsoft SQL, PostgreSQL) –Rich features: XML, PDF, IMAP, LDAP

3 PHP Origins Rasmus LerdorfRasmus Lerdorf (born Greenland, ed Canada) PHP originally abbreviates for ‘Personal Home Pages’, now ‘PHP Hypertext Processor’ Other key developers: Zeev Surashi and Andi Gutmans (Israel) Open Source PHPPHP version 4.4.3 current at UWE Due to upgrade to PHP 5

4 Scripting languages A scripting language is: –often evolved not designed –cross-platform since interpreter is easy to port –designed to support a specific task – PHP -> Web support –un-typed variables (but values are typed) –implicit variable declaration –implicit type conversion –stored only as script files –compiled on demand –may run on the server (PHP) or the client (Javascript)

5 PHP details Procedural language –Compare with Javascript which is event-driven C-like syntax - { } ; Extensive Function Library Good Web-server integration –Script embedded in HTML –Easy access to form data and output of HTML pages Not fully object-oriented –Java is fully object oriented – all functions have to be in a class –In PHP, classes are additional but quite simple to use

6 PHP and HTML HTML-embedded –PHP scripts are essentially HTML pages with the occasional section of PHP script. – PHP script is enclosed in the tag pair:

7 Free format - white space is ignored Statements are terminated by semi-colon ; Statements grouped by { … } Comments begin with // or a set of comments /* */ Assignment is ‘=’ $a=6 Relational operators are, == ( not a single equal) Control structures include if (cond) {..} else { }, while (cond) {.. }, for(startcond; increment; endcond) { } C-like language

8 Arrays are accessed with [ ] : $x[4] is the 5th element of the array $x – indexes start at 0 Associative Arrays (hash array in Perl, dictionary in Java) are accessed in the same way: $y[“fred”] Functions are called with the name followed by arguments in a fixed order enclosed in ( ) : substr(“fred”,0,2) Case sensitive - $fred is a different variable to $FRED

9 Function library Basic tasks –String Handling –Mathematics – random numbers, trig functions.. –Regular Expressions –Date and time handling –File Input and Output And more specific functions for- –Database interaction – MySQL, Oracle, Postgres, Sybase, MSSQL.. –Encryption –Text translation –Image creation –XML

10 String Handling String literals (constants) enclosed in double quotes “ ” or single quotes ‘ ’ Within “”, variables are replaced by their value: – called variable interpolation. “My name is $name, I think” Within single quoted strings, interpolation doesn’t occur Strings are concatenated (joined end to end) with the dot operator “key”.”board” == “keyboard”

11 Standard functions exist: strlen(), substr() etc Values of other types can be easily converted to and from strings – numbers implicitly converted to strings in a string context. Regular expressions be used for complex pattern matching.

12 Learning PHP Start with just the basics, installing a script to output an HTML page Understand how PHP supports interaction with the Browser or other clients Understand how PHP supports integration with databases – Postgress

13 Basic PHP Syntax

14 Datatypes String –'My name is Taro Keio.' –"My name is $name." Boolean –true –false Integer –100 –0x1c Floating point Array –array("tokyo", "hanoi", "london") –array("japan" => "tokyo", "vietnam" => "hanoi", "england" => "london") –$a[2] –$a["vietnam"]

15 Variables in PHP All variables in PHP start with a $ sign symbol. Variables may contain strings, numbers, or arrays.

16 Variable Naming Rules A variable name must start with a letter or an underscore "_" A variable name can only contain alpha-numeric characters and underscores (a-Z, 0-9, and _ ) A variable name should not contain spaces. If a variable name should be more than one word, it should be separated with underscore ($my_string), or with capitalization ($myString)

17 Comments in PHP <?php //This is a comment /*This is a comment block*/ ?>

18 The If...Else Statement Syntax if (condition) code to be executed if condition is true; else code to be executed if condition is false;

19 Example <?php $d=date("D"); if ($d=="Fri") echo "Have a nice weekend!"; else echo "Have a nice day!"; ?>

20 The Switch Statement Syntax switch (expression) { case label1: code to be executed if expression = label1; break; case label2: code to be executed if expression = label2; break; default: code to be executed if expression is different from both label1 and label2; }

21 Looping while - loops through a block of code if and as long as a specified condition is true do...while - loops through a block of code once, and then repeats the loop as long as a special condition is true for - loops through a block of code a specified number of times foreach - loops through a block of code for each element in an array

22 The while Statement Syntax while (condition) code to be executed;

23 The do...while Statement Syntax Do { code to be executed; } while (condition);

24 The for Statement Syntax for (initialization; condition; increment) { code to be executed; }

25 The foreach Statement Syntax foreach (array as value) { code to be executed; }

26 PHP Function <?php function writeMyName() { echo "Kai Jim Refsnes"; } writeMyName(); ?>

27 PHP Form Handling Name: Age:

28 welcome.php Welcome. You are years old.


Download ppt "Introduction to PHP. PHP PHP is the Hypertext Pre-processor –Script language –Embedded into HTML –Runs as Apache module –Can use DB (MySQL, Oracle, Microsoft."

Similar presentations


Ads by Google