Download presentation
Presentation is loading. Please wait.
1
Lab Homework #3 PHP Overview (based on http://www.its.monash.edu/staff/web/slideshows/basic_php/index.html by Lisa Wise) http://www.its.monash.edu/staff/web/slideshows/basic_php/index.html CS 183 4/20/2010
2
Lab Homework #3 More CSS practice ◦ Add CSS to your HTML page from homework #0 ◦ Make it look nice! ◦ Get creative with available CSS styles ◦ No restrictions, but expect usage of a few elements that we discussed in class (e.g., class, ID, contextual selector, inheritance, …) CSS file should be external and included in head using tag (same as in previous lab) Due Date: 4/21 – EOD Submission: ◦ URL to your file via email ◦ Send to Ryan (randonia+cs183@ucsc.edu)randonia+cs183@ucsc.edu
3
PHP 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
4
Scripting vs. Programming A script is interpreted line by line every time it is run A true programming language is compiled from its human readable form (source code) into a machine readable form (binary code) which is delivered to the user as a program. Variables in scripting languages are type-less whereas variables in programs need to be declared as a particular type and have memory allocated to them. PHP requires programming skills PHP web sites should be developed within a software engineering framework
5
Competitors Perl Microsoft Active Server Pages (ASP) Java Server Pages (JSP) Allaire Cold Fusion
6
PHP Code Examples <?php $name = "Lisa"; $date = date ("d-m-Y", time()); ?> Hello World Hello World It's and all is well. <?php echo " Hello ".$name.". \n"; ?>
7
Perl Coding Example #!/usr/local/bin/perl print "Content-type: text/html \n\n"; $date = `/usr/local/bin/date`; $name = "Lisa"; print " "; print " Hello World "; print " \n "; print " Hello World "; print " It\'s $date and all is well "; print " Hello $name "; print " ";
8
JSP Coding Example <%@ page language="java" contentType="text/html" %> <%! String name = "Lisa" %> Hello World Hello World It's and all is well. Hello.
9
PHP Strengths High performance - see benchmarks at http://www.zend.com http://www.zend.com Interfaces to different database systems Low cost SourceForge has PHPTriad (Apache, PHP and MySQL) for WindowsPHPTriad Ease of learning and use Portability
10
Basics of PHP PHP files end with.php other places use.php3.phtml.php4 as well PHP code is contained within tags Canonical: Short-open: HTML script tags: We recommend canonical tags so as not to confuse with xml tags
11
Included Files Files can be inserted into other files using include or require These files can have any name and be anywhere on the filesystem so long as the file trying to include them has appropriate rights CAVEAT: ◦ if these files are not called blah.php, and they are fetched independently by a browser, they will be rendered in plaintext rather than passed to the PHP interpreter - not good if they contain username/passwords and things like that
12
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
13
Variables Scopes Scope refers to where within a script or program a variable has meaning or a value Mostly script variables are available to you anywhere within your script. Note that variables inside functions are local to that function and a function cannot access script variables outside the function even if they are in the same file. The modifiers global and static allow function variables to be accessed outside the function or to hold their value between function calls respectively.
14
Output Most things in PHP execute silently You need to explicitly ask PHP to generate output Echo is not a function and cannot return a value echo " This is a paragraph. "; Print is a function and returns a value ◦ 1 = success ◦ 0 = failure print (" This is a paragraph too. "); Use echo or print statements and View Source for debugging your code
15
Variable Types Strings Numbers integers doublesintegersdoubles Booleans ◦ TRUE / FALSE Arrays Objects
16
Boolean Unlike PHP3, PHP4 has a boolean type if (TRUE) print ("This will always print"); A number is FALSE if it exactly equals 0 otherwise it is TRUE A string is FALSE if it is empty (has zero characters) or is "0" otherwise it is TRUE An array or object is FALSE if it contains no other values and is TRUE otherwise
17
Strings Dot operator for concatenation (joining) ◦ “test”. “file” singly quoted read in and store literally double quoted ◦ certain sequences beginning with \ are replaced with special characters + \n \t \r \$ \" \\ ◦ Variable names are replaced with string representations of their values (variable interpolation) $name = "Praveen"; print "$name said Hello World to the crowd of people."; No limit on string length
18
String Functions boolean strcmp ($str1, $str2) boolean strcasecmp ($str1, $str2) boolean strstr ($haystack, $needle) ◦ case sensitive ◦ Returns the portion of string, or FALSE if needle is not found boolean stristr ($haystack, $needle) int strlen($str) string substr ($str, $start_pos, $len)
19
String Functions (cont) string chop ($str) ◦ Alias of rtrim() string ltrim ($str) string trim ($str) string str_replace ($old_txt, $new_txt, $text) string substr_replace ($old_txt, $new_txt, $text)
20
String Functions (cont) strtolower($str) strtoupper($str) ucfirst($str) ucwords($str) these last two don’t correct inappropriate upper case to lower case
21
String Parsers string strtok($str, $delimiter) $token = strtok ($str, $delimiter) while ($token) { print ($token." "); $token = strtok ($delimiter); } array explode ($delimiter, $str) ◦ Returns an array of strings, each of which is a substring of string formed by splitting it on boundaries formed by the string delimiter. boolean ereg ($reg_exp, $str, [,$array]) array split ($reg_exp, $str [, $num]) ◦ Splits a string into array by regular expression (deprecated)
22
Screening user input/output addslashes($str) ◦ Returns a string with backslashes before characters that need to be quoted in database queries etc. stripslashes($str) magic_quotes_gpc($str) ◦ Affects HTTP Request data (GET, POST, and COOKIE). Cannot be set at runtime, and defaults to on in PHP magic_quotes_runtime($query) ◦ If enabled, most functions that return data from an external source, including databases and text files, will have quotes escaped with a backslash. escapeshellcmd($str) ◦ escapeshellcmd() escapes any characters in a string that might be used to trick a shell command into executing arbitrary commands. strip_tags($str) ◦ This function tries to return a string with all NUL bytes, HTML and PHP tags stripped from a given str htmlspecialchars($str) ◦ Certain characters have special significance in HTML, and should be represented by HTML entities if they are to preserve their meanings. htmlentities($str) ◦ all characters which have HTML character entity equivalents are translated into these entities. nl2br($str) ◦ Returns string with ' ' or ' ' inserted before all newlines.
23
Math Functions + - / * % ++ -- += -= *= = is set to = = is equivalent to = = = is identical to
24
Math Functions (Cont) $low_int = floor ($double) $high_int = ceil ($double) $nearest_int = round ($double) (nearest even number if exactly.5) $positive = abs ($number) $min = min ($n1, $n2 …, $nn) $max = max ($n1, $n2 …, $nn)
25
Control and Flow if (expr1) { } elseif (expr2) { } else { } while (cond) { } do { } while (cond) switch ($var) case a { } case b { } for ($i = 0; $i < expr; $i ++) { } foreach (array_expr as $value) { } foreach (array_expr as $key=>$value) { } break [1] continue
26
Next Topics … Arrays Forms Include / Require Coding style Classes Web-enabled Databases Session management Building code libraries
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.