Download presentation
Presentation is loading. Please wait.
Published byBlaise Boone Modified over 8 years ago
1
PHP
2
www.venturenext.com Outline What is PHP? What can PHP do? PHP Basics PHP and Forms Cookies and Sessions Database Connections Command Line PHP Other PHP capabilities Useful Links Slide # 2
3
www.venturenext.com What is PHP?? PHP: Hypertext Preprocessor PHP is a scripting language that allows you to create dynamic web pages You can embed PHP scripting within normal html coding PHP was designed primarily for the web PHP includes a comprehensive set of database access functions Slide # 3
4
www.venturenext.com What can PHP do? Server side scripting CGI Server module Command line scripting GUI applications Some of the protocols PHP speaks: LDAP, IMAP, SNMP, NNTP, POP3, HTTP, CORBA, XML(SAX/DOM), Perl regular expressions, Cybercash payment, CyberMUT, VeriSign Payflow Pro, and most relational databases Slide # 4
5
www.venturenext.com PHP Basics PHP echos everything to standard output until it comes across special tags:,, <? if ($expression) { ?> This is true. <? } else { ?> This is false. <? } ?> Slide # 5
6
www.venturenext.com PHP Basics (contd.) Comments: // /* */ # Variable Types boolean $foo = True; integer $a=1234; $a=0123; $a=0x1A; float $a=1.234; $a=1.2e3; $a=7E-10; string $name = 'MyName'; array $arr[key] = value; object Variables All variables begin with $ and can contain letters, digits and underscore (and no digit directly after the $) The value of a variable is the value of its most recent assignment Don’t need to declare variables Variables have no intrinsic type other than the type of their current value Can have variable variables $$variable Like a pointer variable type; best to avoid Slide # 6
7
www.venturenext.com Predefined Variables $GLOBALS $_SERVER $_GET $_POST $_COOKIE $_FILE $_REQUEST $_SESSION Slide # 7
8
www.venturenext.com Variable scope The scope of a variable is the context within which it is defined. Within user-defined functions a local function scope is introduced. Any variable used inside a function is by default limited to the local function scope. This is circumvented by using the “global” or “static” declaration: function Sum() { global $a, $b; $b = $a + $b; } Slide # 8
9
www.venturenext.com Control Strctures if else elseif while do..while for foreach break continue switch declare return require() include() require_once() include_once() Slide # 9
10
www.venturenext.com Functions Default argument values function foo ($arg_1="value", $arg_2=7.3,..., $arg_n) Variable-length argument lists func_num_args() func_get_arg() function foo ($arg_1, $arg_2,..., $arg_n) { echo "Example function.\n"; return $retval; } function square ($num) { return $num * $num; } echo square (4); // outputs '16' Slide # 10
11
www.venturenext.com Objects Slide # 11 <?php class Cart { var $items; // Items in our shopping cart function Cart() { $this->todays_date = date("Y-m-d"); $this->name = $GLOBALS['firstname']; /* etc... */ } // Add $num articles of $artnr to the cart function add_item ($artnr, $num) { $this->items[$artnr] += $num; } // Take $num articles of $artnr out of the cart function remove_item ($artnr, $num) { if ($this->items[$artnr] > $num) { $this->items[$artnr] -= $num; return true; } else { return false; } ?>
12
www.venturenext.com PHP and Forms Slide # 12 An example HTML form: Name: Email: Username: Beer: Warthog Guinness Stuttgarter
13
www.venturenext.com PHP and Forms (contd.) Variables accessible in array.php: $_POST[“personal”][“name”] $_POST[“personal”][“email”] $_POST[“beer”] $_POST[“username”] For more on HTML forms: http://www.w3schools.com/html/html_forms.asp?output=print Slide # 13
14
www.venturenext.com PHP and Forms (contd.) Passing variables from page to page: $username from array.php will be available for array2.php Array.php: ” Favorite Color: Slide # 14
15
www.venturenext.com A form processing example <? while($temp = each($_POST)) { echo "Variable $temp[‘key’] contains ” echo “ $temp[‘value’] "; } ?> Slide # 15
16
www.venturenext.com GET vs. POST GET Accessed through the $_GET variable Puts variables in the URL Doesn’t require user to click “reload” if they revisit a page with the “back” button Good for passing small amounts of data POST Accessed through the $_POST variable Passes variables in Content HTTP directive … not in URL If a user clicks the “back” button, the browser will ask the user to reload the page Good for passing large amounts of data, but may cause navigation problems Slide # 16
17
www.venturenext.com Cookies Cookies are useful for storing user info that should be retained from one page to the next. (Overcome the ‘stateless’ nature of the web) Cookies are written to the client’s hard drive. Problems: User can disable cookies in the browser Cookies may be viewed by other users Can only store 20 cookies; max 4KB. Some browsers may display incorrectly unless all options are set in setcookie() Slide # 17
18
www.venturenext.com Creating a Cookie setcookie(name,value,expiration); Eg, setcookie(“fruit”,”banana”,time()+3600); The cookies is called ‘fruit’ and has a value of ‘banana’; it will expire 1 hr from now. Cookie values are sent as part of the HTTP headers (transparent to user). No output should be sent to the browser (echo etc) until the cookie is set else cookie will not be set. Slide # 18
19
www.venturenext.com Cookie (contd.) Accessing A Cookie $_COOKIE contains the value of every current cookie <? foreach ($_COOKIE as $name =>$value) echo “ $name => $value”; ?> Deleting a Cookie Automatically deleted after expiration time You can manually delete by setting negative time setcookie(“username”,””,time()-3600); Slide # 19
20
www.venturenext.com Redirection Once login data is captured/validated then want to go to a new page. Header(“Location: URL”); header("Location: http://capb.dbi.udel.edu/mypage/main.php"); General technique: Site start page = login page Login page validates user and set cookies Redirect to new page New page uses cookie data to access DB info Slide # 20
21
www.venturenext.com Sessions Used to store state across pages To save a variable simply set it in the $_SESSION variable: $_SESSION[“username”] = $username; To access a saved variable, access the $_SESSION variable: $username = $_SESSION[“username”]; To remove a variable, unset the $_SESSION variable: unset($_SESSION[“username”]); http://www.php.net/manual/en/ref.session.php Slide # 21
22
www.venturenext.com Databases and PHP PHP can access many types of RDBMS’s MySQL, M$SQL, Oracle, Sybase, M$ Access, dBase, dbm, PostgreSQL Use database independent interfaces My inc.db.php script DB.php in PEAR http://pear.php.net/manual/en/core.db.php ADODB http://php.weblogs.com/ADODB Slide # 22
23
www.venturenext.com Command Line PHP What is this good for: Parsing files to put into a database Updating files with latest public version (cron job) Anything you’d use a shell script or perl script to do! Variables of use: $argc, $argv[] $stdin, $stdout, $stderr Slide # 23
24
www.venturenext.com Other PHP capabilities IMAP XML parsing FTP IRC Java LDAP Mail Ncurses OpenSSL Regular Expressions Compression PDF file generation Shockwave Flash COM and.NET objects Password strength tests CURL Ecommerce protocols Slide # 24
25
www.venturenext.com Useful Links PHP main site: http://www.php.net Zend’s PHP Intro: http://www.zend.com/zend/art/intro.php PHPBuilder: http://www.phpbuilder.com/ Slide # 25
26
www.venturenext.com Slide # 26 Thank You
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.