1 / 26

CS 183 4 /20/ 2010

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

luka
Download Presentation

CS 183 4 /20/ 2010

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lab Homework #3PHP Overview(based onhttp://www.its.monash.edu/staff/web/slideshows/basic_php/index.htmlby Lisa Wise) 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 <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)

  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()); ?> <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>

  7. 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>";

  8. 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>

  9. 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

  10. 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

  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 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

  15. Variable Types • Strings • Numbersintegersdoubles • Booleans • TRUE / FALSE • Arrays • Objects

  16. 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

  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 • 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)

  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."<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)

  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 '<br />' or '<br>' 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

More Related