Unformatted text preview:

Introduction to phpPHPMost of this is from the PHPmanual online at:http://www.php.net/manual/What we'll cover• A short history of php• Parsing• Variables• Arrays• Operators• Functions• Control Structures• External Data FilesBackground• PHP is server side scripting system– PHP stands for "PHP: HypertextPreprocessor"– Syntax based on Perl, Java, and C– Very good for creating dynamic content– Powerful, but somewhat risky!– If you want to focus on one system fordynamic content, this is a good one tochooseHistory• Started as a Perl hack in 1994 by RasmusLerdorf (to handle his resume), developed toPHP/FI 2.0• By 1997 up to PHP 3.0 with a new parserengine by Zeev Suraski and Andi Gutmans• Version 5.2.4 is current version, rewritten byZend (www.zend.com) to include a number offeatures, such as an object model• Current is version 5• php is one of the premier examples of whatan open source project can beAbout Zend• A Commercial Enterprise• Zend provides Zend engine for PHP for free• They provide other products and services fora fee– Server side caching and other optimizations– Encoding in Zend's intermediate format to protectsource code– IDE-a developer's package with tools to make lifeeasier– Support and training services• Zend's web site is a great resourcePHP 5 Architecture• Zend engine as parser (Andi Gutmans and ZeevSuraski)• SAPI is a web server abstraction layer• PHP components now self contained (ODBC, Java,LDAP, etc.)• This structure is a good general design for software(compare to OSI model, and middlewareapplications)image from http://www.zend.com/zend/art/intro.phpPHP Scripts• Typically file ends in .php--this is set by theweb server configuration• Separated in files with the <?php ?> tag• php commands can make up an entire file, orcan be contained in html--this is a choice….• Program lines end in ";" or you get an error• Server recognizes embedded script andexecutes• Result is passed to browser, source isn'tvisible<P><?php $myvar = "Hello World!"; echo $myvar;?></P>Parsing• We've talk about how the browser can read atext file and process it, that's a basic parsingmethod• Parsing involves acting on relevant portionsof a file and ignoring others• Browsers parse web pages as they load• Web servers with server side technologieslike php parse web pages as they are beingpassed out to the browser• Parsing does represent work, so there is acostTwo Ways• You can embed sections of php insidehtml:• Or you can call html from php:<BODY><P><?php $myvar = "Hello World!"; echo $myvar;</BODY><?phpecho "<html><head><title>Howdy</title>…?>What do we know already?• Much of what we learned aboutjavascript holds true in php (but not all!),and other languages as well$name = "bil";echo "Howdy, my name is $name";echo "What will $name be in this line?"; echo 'What will $name be in this line?';echo 'What's wrong with this line?';if ($name == "bil") { // Hey, what's this? echo "got a match!"; }Variables• Typed by context (but one can force type),so it's loose• Begin with "$" (unlike javascript!)• Assigned by value–$foo = "Bob"; $bar = $foo;• Assigned by reference, this links vars–$bar = &$foo;• Some are preassigned, server and env vars– For example, there are PHP vars, eg.PHP_SELF, HTTP_GET_VARS00phpinfo()• The phpinfo() function shows the phpenvironment• Use this to read system and servervariables, setting stored in php.ini,versions, and modules• Notice that many of these data are inarrays• This is the first script you should write…00_phpinfo.00_phpinfo.phpphpVariable Variables• Using the value of a variable as thename of a second variable)$a = "hello";$$a = "world";• Thus:echo "$a ${$a}";• Is the same as: echo "$a $hello";• But $$a echoes as "$hello"….00_hello_world.00_hello_world.phpphpOperators••Arithmetic (+, -, *, /, %) and String (.)Arithmetic (+, -, *, /, %) and String (.)••Assignment (=) and combined assignmentAssignment (=) and combined assignment$a = 3;$a += 5; // sets $a to 8;$b = "Hello ";$b .= "There!"; // sets $b to "Hello There!";••Bitwise (&, |, ^, ~, <<, >>)Bitwise (&, |, ^, ~, <<, >>)–$a ^ $b(Xor: Bits that are set in $a or $b but notboth are set.)–~ $a (Not: Bits that are set in $a are not set,and vice versa.)••Comparison (==, ===, !=, !==, <, >, <=, >=)Comparison (==, ===, !=, !==, <, >, <=, >=)Coercion• Just like javascript, php is loosely typed• Coercion occurs the same way• If you concatenate a number and string,the number becomesa string17_coercion.17_coercion.phpphpOperators: The Movie••Error Control (@)Error Control (@)––When this precedes a command, errors generated areWhen this precedes a command, errors generated areignored (allows custom messages)ignored (allows custom messages)••Execution (` is similar to the Execution (` is similar to the shell_execshell_exec()()function)function)––You canYou can pass apass a string to the shell for execution:string to the shell for execution:$output = $output = `ls `ls -al`;-al`;$output = shell_exec($output = shell_exec("ls "ls -al");-al");––This is one reason to be careful about user set variables!This is one reason to be careful about user set variables!••Incrementing/DecrementingIncrementing/Decrementing++$a (Increments by one, then returns $a.)++$a (Increments by one, then returns $a.)$a++ (Returns $a, then increments $a by one.)$a++ (Returns $a, then increments $a by one.)--$a--$a (Decrements $a by one, then returns $a.) (Decrements $a by one, then returns $a.)$a--$a-- (Returns $a, then decrements $a by one.) (Returns $a, then decrements $a by one.)Son of the Valley of Operators• Logical$a and $b And True if both $a and $b are true.$a or $b Or True if either $a or $b is true.$a xor $b Xor True if either $a or $b is true,but not both.! $a Not True if $a is not true.$a && $b And True if both $a and $b are true.$a || $b Or True if either $a or $b is true.• The two ands and ors have differentprecedence rules, "and" and "or" arelower precedence than "&&" and "||"• Use parentheses to resolve precedenceproblems or just to be clearerControl Structures• Wide Variety available– if, else, elseif– while, do-while– for, foreach– break, continue, switch– require, include, require_once,include_onceControl Structures• Mostly parallel to what we've coveredalready in javascript• if, elseif, else, while, for, foreach, breakand


View Full Document

UNC-Chapel Hill INLS 672 - Introduction to php

Download Introduction to php
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Introduction to php and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Introduction to php 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?