190 likes | 336 Views
JavaScript. Document Node / Intro to Php. Document Tree. Check Document Tree. Dynamically Add node, Remove node. p arentNode -- Returns the parent node of an element childNodes [] ---Returns an array of child nodes for an element Create Node ---- createElement ( NameofTag )
E N D
JavaScript Document Node / Intro to Php
Dynamically Add node, Remove node • parentNode-- Returns the parent node of an element • childNodes[] ---Returns an array of child nodes for an element • Create Node ---- createElement(NameofTag) • Add Node --- appendChild(element) • Remove Node --- removeChild(element) • Insert Node --- insertBefore(newelement, oldelement) • Replace Child Node --- replaceChild(newelement, oldelement)
Example • Create an paragraph element • varnewnode = document.createElement(“p”); • Create new items and add it to the list. varmyElement = document.createElement("li"); myElement.innerHTML = "HI, hi"; varcontentlist =document.getElementById("displayContents"); contentlist.appendChild(myElement);
Php • Server Side Web Programming • A script file embedded directly into HTML documents • * unlike javascript, php is server side application • Php code is processed by server --- Client can not see the actual phpcodes • Php must be supported by the web server • Any file with php code block should be named with extension . php
The first php program • PHP script has start and end tags, most commonly used are <?php print(“welcome to php”); ?> • <?php ?> can be placed anywhere in HTML markup. • Each command line ends with ; • Comment : // , #, /* */ • Variable starts with a $ sign. • ** php is mostly case insensitive.
First PHP program • Client Side <html> <head> <title>php file</title> </head> <body> welcome to php </body> </html> • Sever side <html> <head> <title>php file</title> </head> <body> <?php print("welcome to php"); ?> </body> </html>
First PHP program pratice • Make sure XAMPP is running on your computer • Copy first.php file to xampp/htdocs/xampp/external • Change “$name = "Paul";” to your name • Open Firefox/chrome • Type in addressbar: localhost/xampp/external/first.php • View the page source
Php type • Php variables are loosely typed – can contain different types of data • Data Type • Four scalar type: integer/int, float/double/real, boolean, string • Two compound type: array, object • Special type: • Resource: hold a reference to external resource (information from database) • NULL: the varible has no value. (either not set or unset()). • **The type of variable is usually decided by php at runtime, not programmer.
Data Type example : gettype() and settype() <?php $testfloat = 1.2; $testint = 2; $testint1 = 1; $teststring = "This is string"; $testbool = FALSE; ?> testbool is of type <?php print(gettype($testbool)); ?> <br \> testint is of type <?php print(gettype($testint)); ?> <br \> Convertestint to double <?phpsettype($testint,"double"); ?> , now testint is of type <?php print(gettype($testint)); ?> <br \>
Output foo is 0 foo is 2 foo is 2.2 foo is 15 foo is 15.2 Type casting and automatical type conversion. <?php $foo = "0"; // $foo is string (ASCII 48) print("foo is $foo <br>"); $foo += 2; // $foo is now an integer (2) print("foo is $foo <br>"); $foo =(float) $foo +0.2; // $foo is now a float (3.3) print("foo is $foo <br>"); $foo = 5 + "10 Little Piggies"; print("foo is $foo <br>"); $foo = 5 + "10.2 Small Pigs"; print("foo is $foo <br>"); ?>
<?php $a = 1; $a = $a + 4; print("After addition: $a <br\>"); $a = $a - 4; print("After substraction: $a <br\>"); $a *=2; print("After multiplication: $a<br\>"); $a +="5 dollars"; print("add by a string:$a <br\>"); $a =$a/2; print("after division:$a <br\>"); ?> Arithmetic Operators • Addtion + • Subtraction – • Multiplication * • Division / • Shortcut • $a = $a+1; • $a += 1;
Array • Array is a collection of data, with the first index be zero. • You can use array() to construct an array $HotelReservation = array(“Dona May”, 2, 89.95, true); • You can initialize one by one $HotelReservation[] = “Dona May”; $HotelReservation[] = 2; $HotelReservation[2]=89.95;
Print Array • For structure • count($Array) will return the number of elements in Array for ( $i = 0; $i < count( $first ); $i++ ) print(“Element $ i is $first[$i]”); • For each structure foreach($first as $value) { print("<p> $value </p>"); }
Associative Array • The index is not numeric values $third[ "Amy" ] = 21; $third[ "Bob" ] = 18; $third[ "Carol" ] = 23; • Or you can use array function $third = array( "Amy" => 21, "Bob" => 18 , "Carol" => 23 );
Iterate through Associative Array • foreach foreach ($third as $key => $value) { print( "<p>$key is $third[$key]</p>" ); } • foreach – control statement that is specially designed for iterating through arrays • foreach ( $third as $element => $value ) • $third is the array • as is the keyword • $element will store the index of each element • $value will store the value of each element
Global Array • $GLOBALS • Contains a reference to every variable which is currently available within the global scope of the script. • $_SERVER • Variables set by the web server or otherwise directly related to the execution environment of the current script. • $_GET • Variables provided to the script via URL query string. • $_POST • Variables provided to the script via HTTP POST. • $_ENV • Variables provided to the script via the environment. • $_COOKIE • Variables provided to the script via HTTP cookies. • * Check file env.php • Change the file to output the IP address of the remote computer. • http://www.php.net/manual/en/reserved.variables.php