Unformatted text preview:

Introduction to phpPHPWhat we'll coverBackgroundHistoryAbout ZendPHP 5 ArchitecturePHP ScriptsParsingTwo WaysWhat do we know already?Variablesphpinfo()Variable VariablesOperatorsCoercionOperators: The MovieSon of the Valley of OperatorsControl StructuresSlide 20SwitchNesting FilesExample: A Dynamic TableArraysArraysText versus KeysWalking Arrays05_arrays.phpMultidimensional ArraysGetting Data into arraysThe SurfaceUsing External DataStandard data filesSlide 34Useful string functions06_more_arrays.phpHow it works06a_more_arrays.phpAlternative syntaxSourcesIntroduction to phpIntroduction to phpPHPPHPMost of this is from the PHP manual online at: http://www.php.net/manual/What we'll coverWhat we'll cover•A short history of php•Parsing•Variables•Arrays•Operators•Functions•Control Structures•External Data FilesBackgroundBackground•PHP is server side scripting system•PHP stands for "PHP: Hypertext Preprocessor"•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 for dynamic content, this is a good one to chooseHistoryHistory•Started as a Perl hack in 1994 by Rasmus Lerdorf (to handle his resume), developed to PHP/FI 2.0•By 1997 up to PHP 3.0 with a new parser engine by Zeev Suraski and Andi Gutmans•Version 5.2.4 is current version, rewritten by Zend (www.zend.com) to include a number of features, such as an object model•Current is version 5•php is one of the premier examples of what an open source project can beAbout ZendAbout Zend•A Commercial Enterprise•Zend provides Zend engine for PHP for free•They provide other products and services for a fee•Server side caching and other optimizations•Encoding in Zend's intermediate format to protect source code•IDE-a developer's package with tools to make life easier•Support and training services•Zend's web site is a great resourcePHP 5 ArchitecturePHP 5 Architecture•Zend engine as parser (Andi Gutmans and Zeev Suraski)•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 middleware applications)image from http://www.zend.com/zend/art/intro.phpPHP ScriptsPHP Scripts•Typically file ends in .php--this is set by the web server configuration •Separated in files with the <?php ?> tag •php commands can make up an entire file, or can be contained in html--this is a choice….•Program lines end in ";" or you get an error•Server recognizes embedded script and executes•Result is passed to browser, source isn't visible<P><?php $myvar = "Hello World!"; echo $myvar;?></P>ParsingParsing•We've talk about how the browser can read a text file and process it, that's a basic parsing method•Parsing involves acting on relevant portions of a file and ignoring others•Browsers parse web pages as they load•Web servers with server side technologies like php parse web pages as they are being passed out to the browser•Parsing does represent work, so there is a costTwo WaysTwo Ways•You can embed sections of php inside html:•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?What do we know already?•Much of what we learned about javascript 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!"; }VariablesVariables•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()phpinfo()•The phpinfo() function shows the php environment•Use this to read system and server variables, setting stored in php.ini, versions, and modules•Notice that many of these data are in arrays•This is the first script you should write…00_phpinfo.php00_phpinfo.phpVariable VariablesVariable Variables•Using the value of a variable as the name 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.php00_hello_world.phpOperatorsOperators•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 not both are set.) •~ $a (Not: Bits that are set in $a are not set, and vice versa.) •Comparison (==, ===, !=, !==, <, >, <=, >=)Comparison (==, ===, !=, !==, <, >, <=, >=)CoercionCoercion•Just like javascript, php is loosely typed•Coercion occurs the same way•If you concatenate a number and string, the number becomesa string17_coercion.php17_coercion.phpOperators: The MovieOperators: The Movie•Error Control (@)Error Control (@)•When this precedes a command, errors generated are ignored When this precedes a command, errors generated are ignored (allows custom messages)(allows custom messages)•Execution (` is similar to the shell_exec() Execution (` is similar to the shell_exec() function)function)•You can pass a string to the shell for execution:You can pass a string to the shell for execution:$output = `ls -al`;$output = `ls -al`;$output = shell_exec("ls -al");$output = shell_exec("ls -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 OperatorsSon of the Valley of Operators•Logical$a and


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?