Lab Homework #3 PHP Overview (based on by Lisa Wise) .
-
date post
19-Dec-2015 -
Category
Documents
-
view
215 -
download
1
Transcript of Lab Homework #3 PHP Overview (based on by Lisa Wise) .
Lab Homework #3PHP Overview(based on http://www.its.monash.edu/staff/web/slideshows/basic_php/index.html by Lisa Wise)
CS 1834/20/2010
Lab Homework #3More 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 – EODSubmission:
◦ URL to your file via email◦ Send to Ryan ([email protected])
PHPPHP 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. ProgrammingA script is interpreted line by line every time it
is runA 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 skillsPHP web sites should be developed within a
software engineering framework
CompetitorsPerlMicrosoft Active Server Pages (AS
P)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/perlprint "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 StrengthsHigh performance
- see benchmarks at http://www.zend.com
Interfaces to different database systems
Low costSourceForge has PHPTriad (Apache,
PHP and MySQL) for WindowsEase of learning and usePortability
Basics of PHPPHP files end with .phpother places use .php3 .phtml .php4
as wellPHP code is contained within tagsCanonical: <?php ?>Short-open: <? ?>HTML script tags: <script
language="php"> </script>We recommend canonical tags so as
not to confuse with xml tags
Included FilesFiles can be inserted into other files using
include or requireThese 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
VariablesAll 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 variablesVariables have no intrinsic type
other than the type of their current value
Can have variable variables $$variable
Variables ScopesScope refers to where within a script or
program a variable has meaning or a valueMostly 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.
OutputMost things in PHP execute silentlyYou need to explicitly ask PHP to generate
outputEcho 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 = failure
print ("<p>This is a paragraph too.</p>");Use echo or print statements and View
Source for debugging your code
Variable TypesStringsNumbers
integers doubles
Booleans◦TRUE / FALSE
ArraysObjects
BooleanUnlike PHP3, PHP4 has a boolean
typeif (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
StringsDot operator for concatenation (joining)
◦ “test” . “file”
singly quoted read in and store literallydouble 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 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)
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 Parsersstring 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.
boolean ereg ($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 (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