180 likes | 313 Views
COP 3813 Intro to Internet Computing. Prof. Roy Levow PHP. PHP. P HP: H ypertext P rocessor Server-side scripting Script embedded in html Script runs on server Allows secure access to resources on server – database, file, etc. Hides program code from users But, more network traffic
E N D
COP 3813Intro to Internet Computing Prof. Roy Levow PHP
PHP • PHP: Hypertext Processor • Server-side scripting • Script embedded in html • Script runs on server • Allows secure access to resources on server – database, file, etc. • Hides program code from users • But, more network traffic • Standard extension is .php
Basic Format <html> …<body> <?php echo "Hello World"; ?> </body> </html>
PHP File Form • Script is embedded in HTML • Bounded by <?php and ?> • Output replaces script code in page sent to client
PHP Service • Must be installed on server • Enabled in web server • Versions for • Apache and IIS • No many platforms • Primary site for PHP www.php.net
Free PHP Services • A number of sites offer limited free PHP services • The following site lists several http://www.0php.com/free_PHP_hosting.php
Your PHP Work • You need a place to work with PHP • You may do any of the following provided that the hosting site is publicly accessible • Set up PHP on your own server • Subscribe to a free hosting site • Use any other hosting site
Language Resources • Online PHP Manual http://www.php.net/manual/en/ • Tutorials • http://www.php.net/links.php#tutorials • http://www.w3schools.com • Google for many others
Variables • Names begin with $ followed by string of letters and digits • Case sensitive • No declarations • Type determined by value $num = 5; $myName = “Prof. Levow”
Named Constants • define(“ONE”, 1); • do not have $ • must be scalars
Types • int, integer • fload, double • string //enclosed in ‘ or “ • bool, Boolean //true or false • array //actually a map • object // for O-O programming • NULL //no value
Constants • Typical rules for numbers and strings • Usual \ escapes in strings • settype($var, “type”) • function to change type of a value • actually changes type of stored value • (type) expr • typical “C” cast of value • Example: Fig. 26.3, data.php
Operators • Comparable to C operators • Usual operators • arithmetic • comparison • logical • increment and decrement • op=
String Operators • . for concatenation • variable interpolation with “ “ • embedded variable reference is replaced by value • example: “My name is $name.” • numeric valued strings converted for arithmetic • 7 + “3” yields 10
Regular Expressions • Pattern matching in strings • ereg, eregi • (reg_exp, $search, $match) • Meta characters: * + ? ^ $ . • Character class [list] • a-z • :alpha:, :alnum:, :digit:, :space:, :lower:, :upper: • Repetition: {n}, {m,n}, {,n}
Environment • Server and Client Side • $_SERVER • $_ENV • $_GET • $_POST • $_COOKIE • $GLOBALS
Form Processing • Examples: • fig 26.13 form.html • fig 26.14 form.php • Login • Example: • fig 26.15 password.html • fig 26.16 password.php
PHP Examples • Cookies • fig 26.20 cookies.html • fig 26.21 readCookie.php • PHP and Database connectivity