Download presentation
Presentation is loading. Please wait.
Published byLaureen Cobb Modified over 8 years ago
1
269200 Web Programming Language Week 5 Dr. Ken Cosh Introducing PHP 1
2
Introduction PHP : Hypertext Preprocessor Server-side script language Supported by Apache/IIS on various platform (MS/Linux/Mac OS) Opensource (PHP License v3.01) 2
3
Introduction – cont. Extension:.php File content Html code PHP code 3 HTML
4
Installation LAMP (or MAMP, or WAMP…) Testing Create a file and name it “test.php” The content is “ ” 4
5
Outline PHP Structure Comments, Syntax, Variables Operators Conditionals Looping 5
6
Basic Syntax Script blockings start with Can be anywhere in the code It can be Inside the block are html-code, php-code, or text Comment the code by // or /* */ as in C 6
7
Basic Syntax Put the script in test.php <?php echo “hello, world”; ?> 7
8
Variables One type, just variables Can store numbers, strings, or array Conversion between type is automatically done. Variables begin with $, e.g. $var1 = 1; or $var2 = “alice”; No declaration is required. 8
9
Variables Naming rules Begins with letter or _ (after $) Contains only A-Z, a-z, 0-9, and _ 9
10
Variables - Array 10 Numeric $cars=array("Saab","Volvo","BMW","Toyota"); $cars[0]="Saab"; $cars[1]="Volvo"; $cars[2]="BMW"; $cars[3]="Toyota";
11
Variables - Array 11 Associative $ages = array("Peter"=>32, "Quagmire"=>30, "Joe"=>34); Or $ages['Peter'] = "32"; $ages['Quagmire'] = "30"; $ages['Joe'] = "34"; Example, <?php $ages['Peter'] = "32"; $ages['Quagmire'] = "30"; $ages['Joe'] = "34"; echo "Peter is ". $ages['Peter']. " years old."; ?>
12
Strings Functions Concatenate:. Length: strlen() Search/Position: strpos(text, pattern) More information: http://www.w3schools.com/php/php_ref_string.asp http://www.w3schools.com/php/php_ref_string.asp 12
13
Strings Put the script in test2.php <?php $h1 = “hello”; $h2 = “world”; echo $h1.”, “.$h2.” ”; echo “h1 has: “.strlen($h1).” letters in it ”; ?> 13
14
Operators - Arithmetic 14
15
Operators - Assignment 15
16
Operators - Comparison 16
17
Operators - Logical 17
18
Control Flow if/while/do…while/for All the same as C. for each Syntax foreach ($array as $value) { code to be executed; } 18
19
Control Flow Example <?php $x=array("one","two","three"); foreach ($x as $value) { echo $value. " "; } ?> 19
20
Form Put the script in test_form.php Name: Age: 20
21
Form Put the script in welcome.php Welcome ! You are years old. 21
22
Form Get, put the script in test_form.php Name: Age: http://localhost?fname=Peter&age=37 22
23
Form Get Visibility Though, it can be bookmarked Not suitable for large variables (>2000 characters) 23
24
Assignment Form Validation A continuation from the previous Javascript exercise. This time validate the form using PHP. Use the form to send data to a new page, and use the new page to display an appropriate message, e.g.; "Password must have 8 characters" "Age must be >18 ″ “All data correct” 24
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.