220 likes | 311 Views
Overview of Server-side Scripting. http://www.flickr.com/photos/torkildr/3462607995/. Welcome to server-side scripting. Special commands mixed into HTML The server executes these commands when the page is loaded. There are many server-side options. PHP – Personal Home Pages
E N D
Overview of Server-side Scripting http://www.flickr.com/photos/torkildr/3462607995/
Welcome to server-side scripting • Special commands mixed into HTML • The server executes these commands when the page is loaded.
There are many server-side options. • PHP – Personal Home Pages • Very easy to get started and what we'll use • See "Setting up PHP" doc for info on how to start • Other available options • JSP – Java Server Pages • ASP.NET – Microsoft's server-side platform • Ruby – very popular, fairly easy to get started • … and more options every year
The PHP tag Hi, my name is <b><?php echo "Bob" ?></b>
Variables and strings <?php $x = 40; echo 'The meaning of life is '.($x+2).'<br>'; echo "But it is not $x"; ?> Variables start with dollar signs. Variables in double-quote strings are evaluated (but not in single-quote strings). Period concatenates.
Conditionals and comparisons <?php $i = 40; if ($i.'' == '40' && $i.'' !== 40) echo 'looks nice'; else echo 'kinda weird'; ?> Conditionals, conversions, and comparisons are like JavaScript.
Loops <?php for ($i = 0; $i < 100; $i++) { if ($i % 3 == 0) echo "$i<BR>"; } ?> Loops are also like JavaScript and other C-like languages.
Arrays <?php $ranking = array( 1 => "OSU", 2 => "UO" ); echo 'The top school in Oregon is '.$ranking[1] ?> You can also just define $arr = array('value0', 'value1') to create an array indexed from 0.
Associative arrays <?php $students = array( "bob" => "smith", "ricky" => "roller" ); echo 'The last name of ricky is '.$students["ricky"] ?> Keys can be strings, ints, whatever. But string, float, and boolean keys are auto-converted to ints whenever possible. Internally, actually, all arrays are associative.
Iterating over an array <?php $ranking = array("zero", "one", "two"); for ($i = 0; $i < count($ranking); $i++) echo $i.':'.$ranking[$i]."<BR>"; ?> The count() function returns the number of elements. Note that this includes element at position 0!!! Another handy function is sort(), for sorting arrays.
Iterating over an associative array <?php $ranking = array("zero", "one", "two"); foreach (array_keys($ranking) as $i) echo $i.':'.$ranking[$i]."<BR>"; ?> Internally, all arrays are associative. Here is how you can loop over the keys of an associative array. FYI, PHP also supports true objects, but we don't use 'em a lot.
Iterating over an array of arrays <?php $mydata = array( array("label" => "Google", "url" => "http://www.google.com"), array("label" => "Oregon State", "url" => "http://www.oregonstate.edu"), array("label" => "New York Times", "url" => "http://www.nytimes.com"), array("label" => "Reddit", "url" => "http://www.reddit.com") ); // print_r($mydata); print("<ul>"); for ($i = 0; $i < count($mydata); $i++) { $item = $mydata[$i]; $label = $item["label"]; $url = $item["url"]; print("<li><a href='" . htmlspecialchars($url) . "'>" . htmlspecialchars($label) . "</a>"); } print("</ul>"); ?>
htmlspecialchars <?php echo '<input value="'.htmlspecialchars('<"&').'">'; ?> This function escapes characters that have special meaning in HTML, such as those shown above. This helps to protect security and proper behavior.
Checking if an array value is set <?php $test = array("OSU" => "Beavs", "UO" => "Ducks"); echo "UW exists? "; echo (array_key_exists("UW", $test) ? "yes" : "no"); ?> Another handy function for debugging is var_dump(), which prints an array's contents.
Functions <?php function bestFriends($idx) { $tmp = array('Karen', 'Kelly', 'Brad'); return $tmp[$idx]; } echo "My best friend is ".bestFriends(0); ?> You can assign functions like pointers to variables. Like this… $fn = bestFriends; echo "My best friend is ".$fn(0);
Accepting values from the browser <form> <input name="nm"><input type="submit"> </form> <?php if (array_key_exists("nm", $_REQUEST)) echo 'submitted:'.htmlspecialchars($_REQUEST["nm"]); ?> The $_GET and $_POST arrays contain values sent by GET or POST. The $_REQUEST array merges these into one.
Very handy for structuring your site <?php include("_header.php");?> This will cause the entire contents of the specified file ("_header.php") to get inserted into your php at this point. For example, you could include a stylesheet.
Let's walk through a site skeleton Browse at http://web.engr.oregonstate.edu/~scaffidc/courses/cs290/skeleton1 Download from http://web.engr.oregonstate.edu/~scaffidc/courses/cs290/lectures/site_skeleton1.zip Good about this skeleton: Shows how to do very basic PHP tasks and a simple site structure Bad about this skeleton: No database integration; same page title on every page
Activity • Create a file on the server called "_header.php" that outputs some HTML, e.g., <style>body { font-family: sans-serif }</style> • Create a second file that does a PHP include <?php include("_header.php") ?>