php - introduction week 5 dr. ken cosh introducing php 1

23
PHP - Introduction Week 5 Dr. Ken Cosh Introducing PHP 1

Upload: emery-preston

Post on 01-Jan-2016

220 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: PHP - Introduction Week 5 Dr. Ken Cosh Introducing PHP 1

PHP - IntroductionWeek 5

Dr. Ken Cosh

Introducing PHP

1

Page 2: PHP - Introduction Week 5 Dr. Ken Cosh Introducing PHP 1

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

Page 3: PHP - Introduction Week 5 Dr. Ken Cosh Introducing PHP 1

Introduction – cont.

• Extension: .php

• File content

• Html code

• PHP code

3

PHP

Request

HTML

Page 4: PHP - Introduction Week 5 Dr. Ken Cosh Introducing PHP 1

Installation

• LAMP (or MAMP, or WAMP…)

• Testing

• Create a file and named it “test.php”

• The content is “<$php phpinfo(); $>”

4

Page 5: PHP - Introduction Week 5 Dr. Ken Cosh Introducing PHP 1

Outline

• PHP Structure

• Comments, Syntax, Variables

• Operators

• Conditionals

• Looping

5

Page 6: PHP - Introduction Week 5 Dr. Ken Cosh Introducing PHP 1

Basic Syntax

• Script blockings start with <?php ?>

• 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

Page 7: PHP - Introduction Week 5 Dr. Ken Cosh Introducing PHP 1

Basic Syntax

• Put the script in test.php<html><body><?php

echo “hello, world”;

?></body></html>

7

Page 8: PHP - Introduction Week 5 Dr. Ken Cosh Introducing PHP 1

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

Page 9: PHP - Introduction Week 5 Dr. Ken Cosh Introducing PHP 1

Variables

• Naming rules

• Begins with letter or _ (after $)

• Contains only A-Z, a-z, 0-9, and _

9

Page 10: PHP - Introduction Week 5 Dr. Ken Cosh Introducing PHP 1

Variables - Array

10

Numeric $cars=array("Saab","Volvo","BMW","Toyota"); $cars[0]="Saab"; $cars[1]="Volvo"; $cars[2]="BMW"; $cars[3]="Toyota";

Page 11: PHP - Introduction Week 5 Dr. Ken Cosh Introducing PHP 1

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.";

?>

Page 12: PHP - Introduction Week 5 Dr. Ken Cosh Introducing PHP 1

Strings

• Functions• Concatenate: .

• Length: strlen()

• Search/Position: strpos(text, pattern)

• More information: http://www.w3schools.com/php/php_ref_string.asp

12

Page 13: PHP - Introduction Week 5 Dr. Ken Cosh Introducing PHP 1

Strings

• Put the script in test2.php<html><body><?php

$h1 = “hello”;$h2 = “world”; echo $h1.”, “.$h2.”<br/>”;echo “h1 has: “.strlen($h1).” letters in it <br/>”;

?></body></html>

13

Page 14: PHP - Introduction Week 5 Dr. Ken Cosh Introducing PHP 1

Operators - Arithmetic

14

Page 15: PHP - Introduction Week 5 Dr. Ken Cosh Introducing PHP 1

Operators - Assignment

15

Page 16: PHP - Introduction Week 5 Dr. Ken Cosh Introducing PHP 1

Operators - Comparison

16

Page 17: PHP - Introduction Week 5 Dr. Ken Cosh Introducing PHP 1

Operators - Logical

17

Page 18: PHP - Introduction Week 5 Dr. Ken Cosh Introducing PHP 1

Control Flow

• if/while/do…while/for• All the same as C.

• for each• Syntaxforeach ($array as $value) { code to be executed; }

18

Page 19: PHP - Introduction Week 5 Dr. Ken Cosh Introducing PHP 1

Control Flow

• Example<html><body>

<?php$x=array("one","two","three");foreach ($x as $value) { echo $value . "<br />"; }?>

</body></html>

19

Page 20: PHP - Introduction Week 5 Dr. Ken Cosh Introducing PHP 1

Form

• Put the script in test_form.php<html><body>

<form action="welcome.php" method="post">Name: <input type="text" name="fname" />Age: <input type="text" name="age" /><input type="submit" />

</form>

</body></html>

20

Page 21: PHP - Introduction Week 5 Dr. Ken Cosh Introducing PHP 1

Form

• Put the script in welcome.php<html><body>

Welcome <?php echo $_POST["fname"]; ?>!<br />You are <?php echo $_POST["age"]; ?> years old.

</body></html>

21

Page 22: PHP - Introduction Week 5 Dr. Ken Cosh Introducing PHP 1

Form

• Get, put the script in test_form.php<html><body>

<form action="welcome.php" method=“get">Name: <input type="text" name="fname" />Age: <input type="text" name="age" /><input type="submit" />

</form>

</body></html>

• http://localhost?fname=Peter&age=37

22

Page 23: PHP - Introduction Week 5 Dr. Ken Cosh Introducing PHP 1

Form

• Get• Visibility

• Though, it can be bookmarked

• Not suitable for large variables (>2000 characters)

23