260 likes | 408 Views
Lab Homework #3 PHP Overview ( based on http ://www.its.monash.edu/staff/web/slideshows/basic_php/ index.html by Lisa Wise). CS 183 4 /20/ 2010. 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
E N D
Lab Homework #3PHP Overview(based onhttp://www.its.monash.edu/staff/web/slideshows/basic_php/index.htmlby Lisa Wise) CS 183 4/20/2010
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 <link> 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)
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
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
Competitors • Perl • Microsoft Active Server Pages (ASP) • Java Server Pages (JSP) • Allaire Cold Fusion
PHP Code Examples <?php $name = "Lisa"; $date = date ("d-m-Y", time()); ?> <html> <head> <title>Hello World</title> </head> <body> <h1>Hello World</h1> <p>It's <?php echo $date; ?> and all is well. </p> <?php echo "<p>Hello ".$name.".</p>\n"; ?> </body> </html>
Perl Coding Example #!/usr/local/bin/perl print "Content-type: text/html \n\n"; $date = `/usr/local/bin/date`; $name = "Lisa"; print "<html>"; print "<head>"; print "<title>Hello World</title>"; print "</head>\n<body>"; print "<h1>Hello World</h1>"; print "<p>It\'s $date and all is well</p>"; print "<p>Hello $name</p>"; print "</body></html>";
JSP Coding Example <%@ page language="java" contentType="text/html" %> <%! String name = "Lisa" %> <html> <head> <title>Hello World</title> </head> <body> <h1>Hello World</h1> <p>It's <%= new java.util.Date().toString() %> and all is well. </p> <p> Hello <%= name %>.</p> </body> <html>
PHP Strengths • High performance - see benchmarks at http://www.zend.com • Interfaces to different database systems • Low cost • SourceForge has PHPTriad (Apache, PHP and MySQL) for Windows • Ease of learning and use • Portability
Basics of PHP • PHP files end with .php • other places use .php3 .phtml .php4 as well • PHP code is contained within tags • Canonical: <?php ?> • Short-open: <? ?> • HTML script tags: <script language="php"> </script> • We recommend canonical tags so as not to confuse with xml tags
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
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
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.
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 valueecho "<p>This is a paragraph.</p>"; • Print is a function and returns a value • 1 = success • 0 = failureprint ("<p>This is a paragraph too.</p>"); • Use echoor printstatements and View Source for debugging your code
Variable Types • Strings • Numbersintegersdoubles • Booleans • TRUE / FALSE • Arrays • Objects
Boolean • Unlike PHP3, PHP4 has a boolean type • if (TRUE) print ("This will always print"); • A number is FALSEif it exactly equals 0 otherwise it is TRUE • A string is FALSEif it is empty (has zero characters) or is "0" otherwise it is TRUE • An array or object is FALSEif it contains no other values and is TRUEotherwise
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
String Functions • booleanstrcmp ($str1, $str2) • booleanstrcasecmp ($str1, $str2) • booleanstrstr ($haystack, $needle) • case sensitive • Returns the portion of string, or FALSE if needle is not found • booleanstristr ($haystack, $needle) • intstrlen($str) • string substr ($str, $start_pos, $len)
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)
String Functions (cont) • strtolower($str) • strtoupper($str) • ucfirst($str) • ucwords($str) • these last two don’t correct inappropriate upper case to lower case
String Parsers • string strtok($str, $delimiter)$token = strtok ($str, $delimiter)while ($token){ print ($token."<br>"); $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. • booleanereg ($reg_exp, $str, [,$array]) • array split ($reg_exp, $str [, $num]) • Splits a string into array by regular expression (deprecated)
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 '<br />' or '<br>' inserted before all newlines.
Math Functions • + - / * % • ++ -- • += -= *= • = is set to • = = is equivalent to • = = = is identical to
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)
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
Next Topics … • Arrays • Forms • Include / Require • Coding style • Classes • Web-enabled Databases • Session management • Building code libraries