/
CMPT241 Web Programming CMPT241 Web Programming

CMPT241 Web Programming - PowerPoint Presentation

min-jolicoeur
min-jolicoeur . @min-jolicoeur
Follow
343 views
Uploaded On 2019-11-20

CMPT241 Web Programming - PPT Presentation

CMPT241 Web Programming Intro to PHP URLs and web servers Usually when you type a URL in your browser Your computer looks up the servers IP address using DNS Domain Name System Your browser connects to that IP address and requests the given file ID: 766085

server php print web php server web print int string age html type file code world variables browser java

Share:

Link:

Embed:

Download Presentation from below link

Download Presentation The PPT/PDF document "CMPT241 Web Programming" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

CMPT241 Web Programming Intro to PHP

URLs and web servers Usually when you type a URL in your browser: Your computer looks up the server's IP address using DNS (Domain Name System) Your browser connects to that IP address and requests the given fileThe web server software (e.g. Apache) grabs that file from the server's local file systemThe server sends back its contents to you 2 http://server/path/file

browser requests a .html file static content: server just sends that file browser requests a . php file dynamic content: server reads it, runs any script code insideIf two people visit http://www.facebook.com/home.php, they see two very different pages.

URLs and web servers (cont.) 4 Web/Application Server Apache, IIS, ... Database

Server-Side web programming Server-side pages are programs written using one of many web programming languages/frameworks examples: PHP, Java/JSP, Ruby, ASP.NET, Python, Perl 5

Server-Side web programming (cont.) Also called server side scripting :Dynamically edit, change or add any content to a Web pageRespond to user queries or data submitted from HTML formsAccess any data or databases and return the results to a browser Customize a Web page to make it more useful for individual usersProvide security since your server code cannot be viewed from a browser 6

Server-Side web programming (cont.) Web server: contains software that allows it to run server side programs sends back their output as responses to web requests 7

XAMPP Web server solution package Apache HTTP Server MySQL MariaDBPHP interpreters...

“Homework” Download and run XAMPP on your laptop

What is PHP? PHP stands for "PHP Hypertext Preprocessor" Server-side scripting language Used to make web pages dynamic:provide different content depending on contextinterface with other services: database, file, etc.authenticate usersprocess form information PHP code can be embedded in HTML code http://en.wikipedia.org/wiki/Rasmus_Lerdorf10

Facts about PHP Rather than compiled, PHP source code is translated and executed dynamically, or interpreted , by the web server.PHP has more relaxed syntax than Java and C++fewer and looser data typesvariables don’t need to be declaredMore of a Procedural programming language The key construct is the function rather than the class

Lifecycle of a PHP web request 12 Hello world! User’s computer Server computer Hello.php

Why PHP? Free and open source Compatible Supported by most popular web serversUsed by 80% of the websites whose server-side language is known.Simplelots of build-in functionality, familiar syntaxhttp://www.php.net/ 13

Hello World! 14 <? php print "Hello, world!"; ?> PHP Hello world! output The following contents could go into a file  hello.php : a block or file of PHP code begins with  <?php and ends with ?>PHP statements, function declarations, etc. appear between these endpoints

Viewing PHP output 15 Hello world! Test the PHP page in the localhost

Step 1. Start Apache in XAMPP

Step 2. Put the PHP file in xampp / htdocs/

Step 3. In your browser, type http://localhost/filename.php

Step 4. Upload the php page to Turing using FileZilla turing.manhattan.edu/~ID/filename.php

PHP syntax template 20 Contents of a . php file between <?php and ?> are executed as PHP code All other contents are output as pure HTMLWe can switch back and forth between HTML and PHP "modes” (mode-switchers ) HTML content <? php PHP code ?> HTML content <? phpPHP code?>HTML content ... PHP

<!DOCTYPE html> <html> <head> <title>My First PHP Page</title> </head> <body> <p> <? php print "Hello, world!"; ?> <p> </body> </html> PHP

Console output: print/echo 22 print "Hello, World!\n"; print "Escape \"chars\" are the SAME as in Java!\n"; print "You can haveline breaks in a string."; print 'A string can use "single-quotes". It\'s cool!'; PHP Hello world! Escape "chars" are the SAME as in Java! You can have line breaks in a string. A string can use "single-quotes". It's cool! outputprint "text"; PHP

Comments 23 # single-line comment // single-line comment /* multi-line comment*/ PHP like Java, but # is also allowed a lot of PHP code uses # comments instead of //

Variables 24 $ user_name = “mundruid78"; $age = 16;$ drinking_age = $age + 5; $ this_class_rocks = TRUE; PHP $name = expression; PHPnames are case sensitiveseparate multiple words with _names always begin with $, on both declaration and usagealways implicitly declared by assignment (type is not written)a loosely typed language (like JavaScript or Python)

Variables 25 $ user_name = “mundruid78"; $age = 16;$ drinking_age = $age + 5; $ this_class_rocks = TRUE; PHP $name = expression; PHPA variable without assigned value will have a default value of 0, empty string, or empty array.A variable can change type as the program is running.

Variables 26 basic types: int , float, boolean , string, array, object, NULL test type of variable with is_type functions, e.g. is_string gettype function returns a variable's type as a string

Expression Result gettype (2.71) “double” gettype(42)“ int ” gettype (“42”) “string” is_string (“hello”) TRUEis_int(3.14)FALSE

Variables 28 PHP converts between types automatically in many cases: string → int auto-conversion on + "1" + 1 == 2 int → float auto-conversion on / 3 / 2 == 1.5 type-cast with (type): $age = (int) "21";

Expression Result ( int ) 2.71 2(int) “2.71” 2 (float) “2.71” 2.71 ( int ) “ billybob”0(int) 3/21.5 (int) (3/2)1

Arithmetic operators 30 + - * / % . ++ -- = += -= *= /= %= .= many operators auto-convert types: 5 + "7" is 12

int and float Types31 int for integers and float for realsdivision between two int values can produce a float $a = 7 / 2; # float: 3.5 $b = ( int ) $a; # int : 3 $c = round($a); # float: 4.0 $d = "123"; # string: "123"$e = ( int) $d; # int: 123 PHP

Math operations 32 $a = 3; $b = 4; $c = sqrt( pow ($a, 2) + pow ($b, 2)); PHP abs ceilcos floor log(n,base)log10 max min pow rand(min,max) round sin sqrt tan math functions (no need to include/import) Google “ php ” + function M_PI M_E M_LN2 3.14159265.. e, 2.7182818.. loge2, 0.693147.. math constants

Expression Result round(3.5) 4 min(17, 9) 9 abs(ceil(-3.2)) 3 max(7.8, 2, 5, round(7.6)) 8 sqrt (pow(3,2) + pow(4,2)) 5

bool (Boolean) type 34 $ feels_like_summer = FALSE;$ php_is_great = TRUE; $ student_count = 7; $nonzero = (bool) $ student_count ; PHP the following values are considered to be FALSE (all others are TRUE):0 and 0.0 "", "0", and NULL (includes unset variables)arrays with 0 elementsFALSE prints as an empty string (no output); TRUE prints as a 1

String Type 35 zero-based indexing using bracket notationthere is no char type; each letter is itself a Stringstring concatenation operator is . (period), not + 5 + "2 turtle doves" === 75 . "2 turtle doves" === "52 turtle doves"can be specified with "" or '' $ favorite_food = "Ethiopian"; print $ favorite_food [2]; $ favorite_food = $ favorite_food . " cuisine";print $favorite_food; PHP

Expression Result 1 + “2” 3 1 + “3 french hens” 4 1. “2” “12” 1 + “not a number” 1 + 3 + “5” + 7 + 9 25 1. 3. “5”. 7. 9“13579” 1 + 3. “5”. 7 + 9(1 + 3). “5”. (7 + 9)

Expression Result 1 + “2” 3 1 + “3 french hens” 4 1. “2” “12” 1 + “not a number” 1 1 + 3 + “5” + 7 + 9 25 1. 3. “5”. 7. 9“13579” 1 + 3. “5”. 7 + 9466(1 + 3). “5”. (7 + 9)“4516”

String Functions 38 $name = "Stefanie Hatcher"; $length = strlen($name); $ cmp = strcmp ($name, "Brian Le"); $index = strpos ($name, "e"); $last = substr ($name, 9, 5); $name = strtoupper($name); PHP

String Functions (cont.) 39 Name Java Equivalent strlen length strpos indexOf substr substring strtolower , strtoupper toLowerCase, toUpperCasetrimtrimstrcmpcompareTo

Expression Result str_replace (“be”, “B”, “to be or not to be”) “to B or not to B” trim (“ hulk smash! “) “hulk smash!” strrev (“ strtolower (“BOOYAH!”)) “!hayoob” ord(“A”)65 chr(66)“B”sprintf(“%5s %08d”, “hi”, 90210)“ hi 00090210”

Interpreted Strings 41 $age = 16; print "You are " . $age . " years old.\n"; print "You are $age years old.\n"; # You are 16 years old. PHP strings inside " " are interpreted variables that appear inside them will have their values inserted into the string strings inside ' ' are not interpreted: print ' You are $age years old.\n '; # You are $age years old. \n PHP

Interpreted Strings (cont.) 42 print "Today is your $ ageth birthday.\n"; # $ageth not found print "Today is your {$age} th birthday.\n"; PHP if necessary to avoid ambiguity, can enclose variable in {}

The variables are not preserved on the web server. Save data into someplace such as a file

Interpreted Strings (cont.) 44 $name = “Bill"; $name = NULL; if ( isset($name)) { print "This line isn't going to be reached.\n"; } PHP a variable is NULL if it has not been set to any value (undefined variables) it has been assigned the constant NULL it has been deleted using the unset functioncan test if a variable is NULL using the isset functionNULL prints as an empty string (no output)

if/else statement 45 if (condition) { statements; } elseif (condition) { statements; } else { statements; } PHP NOTE: although elseif keyword is much more common, else if  is also supported

Operators == != ignoring types === !== considering types 42 == “42” TRUE42 == 42.0 TRUE42 === “42” FALSE> < >= <=&& || !

if ($a == 5) { echo “a equals 5”; } elseif ($a == 6) { echo “a equals 6”; } else { echo “a is neither 5 nor 6”; } PHP

for loop (same as Java) 48 for (initialization; condition; update) { statements; } PHP for ($i = 0; $i < 10; $i++) { print "$i squared is " . $i * $i . ".\n"; } PHP

while loop (same as Java) 49 while (condition) { statements; } PHP do { statements; } while (condition); PHP break  and  continue keywords also behave as in Javawhile loops are used less often than for and foreach in PHP

if ($a == 5) : echo “a equals 5”; elseif ($a == 6) : echo “a equals 6”;else : echo “a is neither 5 nor 6”; endif ; for ($i = 0; $i < 10; $i++) : print "$i squared is " . $i * $i . ".\n"; endfor ; PHP

PHP exercise 1 Loops are very useful in creating lists and tables. In this PHP exercise, you will use a loop to create a list of equations for squares. Using a for loop, write a script that will send to the browser a list of squares for the numbers 1-12. Use the format, "1 * 1 = 1", and be sure to include code to print each formula on a different line. 51

PHP exercise 2 HTML tables involve a lot of repetitive coding - a perfect place to use for loops. You can do even more if you nest the for loops. In this PHP exercise, use two for loops, one nested inside another. Create the following multiplication table: 52 1 2 3 4 5 6 7 2 4 6 8 10 12 14 3 6 9 12 15 18 21 4 8 12 16 20 24 28 5 10 15 20 25 30 35 6 12 18 24 30 36 42 7 14 21 28 35 42 49

Errors in PHP Syntax errors print “hello world; Runtime errorsprint 1/0;Displayed as part of the HTML output