PPT

28
Web Database Programming Week 2 Introduction to PHP (1)

description

 

Transcript of PPT

Page 1: PPT

Web Database ProgrammingWeek 2

Introduction to PHP (1)

Page 2: PPT

04/08/23 Intro to PhP (1)

Today’s Plan

• Learn to use our lab environment

• Introducing PHP the language– How it is related to HTML

• Learn the basic syntax of PHP

Page 3: PPT

04/08/23 Intro to PhP (1)

SISPLAB

• Domain name: *.sisplab.albany.edu• Behind a firewall• Need to pass through a proxy server to

access

Internet Firewall

Proxy

trantor

mingus

Others…

SISPLAB subnetwork

Our server:Web, MySQL& PHP

Page 4: PPT

04/08/23 Intro to PhP (1)

Access SISPLAB Servers

• SSH to proxy.sisplab.albany.edu– With your regular UAlbany NetID & password

• From proxy, Telnet to trantor – With your regular Ualbany NetID, but…– With the initial password: test

• Don’t forget to change it!

• You can use FTP to transfer file to your UAlbany home directory, then to trantor

• Details see: http://www.albany.edu/~hy973732/SISPLAB-access.html

Page 6: PPT

04/08/23 Intro to PhP (1)

Introduction to PHP

• PHP – Hypertext Preprocessor• A general purpose script language• Mainly used to generate dynamic HTML

content• Usually implemented as a module or a

plug-in to Web servers• The most popular add-on (45%) of the

most popular Web server – Apache (75%)

Page 7: PPT

04/08/23 Intro to PhP (1)

PHP Code is Embodied within HTML Code

• Tags: <?php the php code ?>• White space (space, tab, line feed, carriage

return) is all the same and equivalent to one space

• The tag gets replaced by the text that it generates– <?php echo “Here we are.”; ?>– <?php print “Here we are.”; ?>

• PhP code consists of statements– You can have as many statements as you want– Every statement ends with a semi-colon

Page 8: PPT

04/08/23 Intro to PhP (1)

Execution of a PHP Request

Browser

WWWServer

ServerDisk

PHP Plugin

2. Fetch PHP Doc and pass to PHP Server Plugin

Database

3. Interpret PHP commands

4. Produce HTML

5. Return HTML doc to browser

1. PHP Request

Page 9: PPT

04/08/23 Intro to PhP (1)

A Simple PHP page

<html><head><title>Page 1</title></head><body><?php print (“My first PhP Script.”);

?>

</body>

Page 10: PPT

04/08/23 Intro to PhP (1)

Using phpinfo()

• Use phpinfo() to see the characteristics of your PhP server<html><head> <title>PhP info</title></head><body><?php phpinfo();?></body>

Page 11: PPT

04/08/23 Intro to PhP (1)

PHP - Comments

• Single line comments (everything on the line, past the comment marker, is ignored)

// this is a single line comment

## as is this• Multi-line comments (everything between the

comment markers is ignored)

/* this is a comment thatextends over multiple lines

*/

Page 12: PPT

04/08/23 Intro to PhP (1)

Building Blocks – Functions

• Functions denote actions that can be taken (on your behalf) by php

• Exampleprint(“This is some text”);

• Functions can be parts of an expression• Functions have one or more arguments (the

part inside the parentheses)• The values of the arguments are past to the

functions that do something with them (“side effects”)

• A Function may return a value (example shortly)

Page 13: PPT

04/08/23 Intro to PhP (1)

Building Blocks - Variables

• Variables– Name starts with begins with letter or underscore,

always proceeded by $– Other characters can be letters, digits or

underscore

• Variables store values that change over time• New values are computed in expressions

(shortly)• The values can be of a certain type: Integer,

Double, String, Boolean, Object, Array

Page 14: PPT

04/08/23 Intro to PhP (1)

The assignment operator

• An operator is a symbol that represents a primitive action in PHP

• The assignment operator takes two operands: a valuable, on the left, and some expression or variable, on the right.

• It takes the value computed by the expression or variable on the right and puts that value into the variable on the left

• If the variable on the left appears on the right, its old value is used in the expression to compute the new value

Page 15: PPT

04/08/23 Intro to PhP (1)

Changing the type

• Change the type of a variable with the PhP function settype()settype(variable,type);settype($zipcode,’string’);

• The function settype() has the side effect of changing the datatype of the value in the variable

• You can get the type of a value or variable using gettype() – returns the typegettype(variable);print(gettype($zipcode));

Page 16: PPT

04/08/23 Intro to PhP (1)

Change the type with a cast

• You cast a value into a new type by using the construct of the type name in parenthesis

e. g.$zipcode = (string)

$inputZipcode;

Page 17: PPT

04/08/23 Intro to PhP (1)

Operator

• An expression is a combination of operators and operands that results in a value

• A operand is one of the parts of the expression that the operator operators on

$count + 10

Expressions and Operators

Operand

Operand

Page 18: PPT

04/08/23 Intro to PhP (1)

Operators

• Assignment (=) – returns a value equal to the value that was assigned: $a = ($b = 5);

• Arithmetic: + - * \ %• Concatination: “now”.“one String”• Comparison: == != > >= < <=

• Logical: || or xor && and !

Page 19: PPT

04/08/23 Intro to PhP (1)

Operator Precedence

• (cast)• * \ %• + -• < <= > >=• == !=• &&• ||• = • and• xor• or

Can be changed by using parentheses

e.g.

2*5+6

2*(5+6)

Page 20: PPT

04/08/23 Intro to PhP (1)

Constants

• You indicate that a label is to represent a constant by:define(‘CONSTANT_NAME’,value) ordefine(‘CONSTANT_NAME’,value,true)

• When the third parameter is present and true then the parameter label can be used in either upper or lower case

e. g. define(‘PI’,3.1412,true)

defines a constant PI and another pi that both have the value 3.1412

Page 21: PPT

04/08/23 Intro to PhP (1)

Predefined Constants

__FILE__ is the file that the script is contained in

__LINE__ is the line number that is currently being processed in that script

PHP_VERSION is the version of the PHP that is running

Page 22: PPT

04/08/23 Intro to PhP (1)

Statements

• Function calls: print(“hello”);• Assignment statements: $first = “first one”;

• If/Then/Else statement:if (expression){ true_part }

else { false_part }

• While statement: while (expression){ repeated_part }

• For statement: for (init; test; update){ repeated_part }

Page 23: PPT

04/08/23 Intro to PhP (1)

If/Then/Else

• If/Then/Else statement:if (expression){ true_part }

else { false_part }

• Used to conditional execute part of a PhP script

• The else part is optional• Example:if ($num == 1) { $end = “st”; }else if ($num == 2 or $num ==3) { $end = “ed”; }else { $end = “th”; }print (((string)$num).$end);

Page 24: PPT

04/08/23 Intro to PhP (1)

While

• While statement: while (expression){ repeated_part }

• Loop, that is repeat all the enclosed PhP statements, while the condition is true

• Example:$count = 10;while ($count > 0){ print(“the count is $count

<br>”); $count = $count – 1;

}

Page 25: PPT

04/08/23 Intro to PhP (1)

For

• For statement: for (init; test; update){ repeated_part }

• Execute the init statement, then test the condition; then repeat the loop

• In the loop, as long as the condition is true, execute the statements in the body; then execute the update statement; then test the condition

• Example: for ($n = 1; $n < 10; $n = $n + 1){ print (“the count is $n <br>”);}

Page 26: PPT

04/08/23 Intro to PhP (1)

Building Blocks

• Functions–Statements

• Expressions–Operators–Variables–Constants

Page 27: PPT

04/08/23 Intro to PhP (1)

References

• Our textbook

• PHP.Nethttp://www.php.net/

• PHP.net simple tutorialhttp://www.php.net/tut.php

Page 28: PPT

04/08/23 Intro to PhP (1)

Homework

• Don’t forget

• Find it on the schedule page of course Web site http://www.albany.edu/~hy973732/courses/IIST535/schedule.html