Presentation is loading. Please wait.

Presentation is loading. Please wait.

School of Computing and Information Systems CS 371 Web Application Programming PHP - Basics Serving up web pages.

Similar presentations


Presentation on theme: "School of Computing and Information Systems CS 371 Web Application Programming PHP - Basics Serving up web pages."— Presentation transcript:

1 School of Computing and Information Systems CS 371 Web Application Programming PHP - Basics Serving up web pages

2 School of Computing and Information Systems CS 371 Web Application Programming Server vs Client side Traditional static web pages Internet clie nt server request is made server receives request sends html file client receives and renders html

3 School of Computing and Information Systems CS 371 Web Application Programming Server vs Client side Client side processing Internet clie nt server Client clicks on button or element that has event associated with it associated function modifies web page

4 School of Computing and Information Systems CS 371 Web Application Programming Server vs Client side Server side scripting Internet clie nt server request is made server script processes request (database…) sends back html, xml or other data client receives and processes data

5 School of Computing and Information Systems CS 371 Web Application Programming Client vs. Server side server scripts web server must support language & features can save and retrieve data source code is hidden client scripts browser must support language (cross browser issues) executed by client does not require refresh

6 School of Computing and Information Systems CS 371 Web Application Programming Server side scripting history CGI (common gateway interface) since 1993 input was URL appendage: www.what.org?name=“me”&age=20 ?=“& characters are converted to hex www.what.org?name=“me”&age=20 programs (C, perl) read the data from stdin had to convert data back and assemble web server invokes program as a separate process

7 School of Computing and Information Systems CS 371 Web Application Programming Server side scripting - newer languages are compiled into web server – directly executed make access to input variables much easier have better integration with database examples: ASP, JSP, PHP, SMX, Lasso, Ruby, etc.

8 School of Computing and Information Systems CS 371 Web Application Programming PHP PHP: Hypertext Processor Ranked 3 rd on TIOBE index Free Software Foundation – no formal specifications syntax influenced by C and perl 1995 ver 2 - Rasmas Lerdorf created for maint. web pages several updates later, 2004 ver 5, oop, consistent database support, etc.

9 School of Computing and Information Systems CS 371 Web Application Programming PHP basics files have php extensions but embed HTML acts as a filter – inputs mix of code and HTML, outputs all HTML called: putting URL directly into address of browser <form action=“myProg.php” … code is embedded using

10 School of Computing and Information Systems CS 371 Web Application Programming PHP more basics like c statements end in ; comments, assignments, operators, control statements, function calls like perl loosely typed variables (also start with $) associative array whitespace echo is stdout

11 School of Computing and Information Systems CS 371 Web Application Programming Variables identifiers – any length, letters, digits, _ and $ use “=“ for assignment operator types: integer, double, string, boolean, array and object declarations do not use type: $i=0; constants: define(‘MAX’,100);

12 School of Computing and Information Systems CS 371 Web Application Programming Variable Scope local, global and superglobal all variables are global within the scope they are defined in all variables are local in functions unless specified with global keyword superglobals are a set of variables always available ($_POST(‘name’))

13 School of Computing and Information Systems CS 371 Web Application Programming Operators arithmetic: +,-, *, / and our old friend % string concatenation $fname. $lname assignment: =, +=, -=, *=, /=, %= and.= ++ and - - are both pre and post fix reference: $b = &$a // $a and $b point to same variable (no pointer arithmetic) comparison: ==, !=, <>,, =, === logical: !, &&, ||, xor Who uses this?

14 School of Computing and Information Systems CS 371 Web Application Programming Program Structure similar to C, Java: if statements switch statements while, do while and for loops foreach can be used to iterate through collection foreach($myArray as $value) foreach($myArray as $key => $value)

15 School of Computing and Information Systems CS 371 Web Application Programming Functions do not need to declare return type optional parameters – assign in def functions not case sensitive pass-by-ref: use & before parameter use require() and include() to import code function f($parm1, $parm2, $parm3=1, $parm4=‘a’){ return $parm1 + $parm2 * $parm3 – strlen($parm4); } echo( f(-4, 6) ); echo( f( 3, 4, 2, ‘what’) );

16 School of Computing and Information Systems CS 371 Web Application Programming Arrays arrays are associative (more like hash maps than arrays) subscripts can be integers or strings declarations: $arr = array(1,2,3,4); $arr = array( 4 => ‘first’, ‘x’ => ‘second’); $arr[3]=‘something’; //creates $arr add element: $arr[]=‘new val’; $value) { unset($array[$i]); //delete item } print_r($array); $array[] = 6; print_r($array); $array = array_values($array); $array[] = 7; print_r($array); ?> Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 ) Array ( ) Array ( [5] => 6 ) Array ( [0] => 6 [1] => 7 )

17 School of Computing and Information Systems CS 371 Web Application Programming Arrays (cont) multidimensional $persons=array(array(‘al’,21),array(‘flo’,22) ); $persons[0][1] //value would be 21 many helpful array functions sort, count, sizeOf, each(), current, reset(), end(), next(), pos()

18 School of Computing and Information Systems CS 371 Web Application Programming File Processing like c fopen, fclose, fwrite, fgets, feof, etc. also can use lock functions easier way: $lines = file(“pathName”); creates an array with an element for each line

19 School of Computing and Information Systems CS 371 Web Application Programming Strings can be delimited by single quotes (only recognizes \\ and \’) double quotes (recognizes more escape seq.) heredoc can use printf, strtoupper, ucfirst… explode(‘,’,$record) //creates array like split in java substr, strcmp, strlen, etc.

20 School of Computing and Information Systems CS 371 Web Application Programming Classes and Objects members can be private, public or protected constructors and destructors classes can extend other classes use class::method for class methods name = $newName; } public function displayName() { echo $this->name; } } ?> $s = new Student(‘melvin’); $s->displayName();


Download ppt "School of Computing and Information Systems CS 371 Web Application Programming PHP - Basics Serving up web pages."

Similar presentations


Ads by Google