1 / 53

CMPT241 Web Programming

Learn the basics of PHP web programming, including server-side scripting, dynamic content creation, and interacting with databases. Get started with XAMPP and explore the fundamentals of PHP syntax.

iknight
Download Presentation

CMPT241 Web Programming

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CMPT241 Web Programming Intro to PHP

  2. 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 file • The web server software (e.g. Apache) grabs that file from the server's local file system • The server sends back its contents to you http://server/path/file URLs and web servers

  3. 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 inside • If two people visit http://www.facebook.com/home.php, they see two very different pages.

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

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

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

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

  8. XAMPP • Web server solution package • Apache HTTP Server • MySQL MariaDB • PHP interpreters • ...

  9. “Homework” • Download and run XAMPP on your laptop

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

  11. 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 types • variables don’t need to be declared • More of a Procedural programming language • The key construct is the function rather than the class

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

  13. Free and open source • Compatible • Supported by most popular web servers • Used by 80% of the websites whose server-side language is known. • Simple • lots of build-in functionality, familiar syntax • http://www.php.net/ Why PHP?

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

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

  16. Step 1. Start Apache in XAMPP

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

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

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

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

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

  22. Console output: print/echo print "text"; PHP print "Hello, World!\n"; print "Escape \"chars\" are the SAME as in Java!\n"; print "You can have line 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! output

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

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

  25. Variables $name = expression;PHP $user_name = “mundruid78"; $age = 16; $drinking_age = $age + 5; $this_class_rocks = TRUE; PHP • A 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.

  26. basic types: int, float, boolean, string, array, object, NULL • test type of variable with is_typefunctions, e.g. is_string • gettypefunction returns a variable's type as a string Variables

  27. 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"; Variables

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

  29. int for integers and float for reals division between two int values can produce a float int and float Types $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: 123PHP

  30. $a = 3; $b = 4; $c = sqrt(pow($a, 2) + pow($b, 2)); PHP Math operations math functions (no need to include/import) Google “php” + function math constants

  31. $feels_like_summer = FALSE; $php_is_great = TRUE; $student_count = 7; $nonzero = (bool) $student_count; PHP bool (Boolean) type • 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 elements • FALSE prints as an empty string (no output); TRUE prints as a 1

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

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

  34. String Functions (cont.)

  35. $age = 16; print "You are " . $age . " years old.\n"; print "You are $age years old.\n"; # You are 16 years old. PHP Interpreted Strings • 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

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

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

  38. $name = “Bill"; $name = NULL; if (isset($name)) { print "This line isn't going to be reached.\n"; } PHP Interpreted Strings (cont.) • 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 function • can test if a variable is NULL using the isset function • NULL prints as an empty string (no output)

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

  40. Operators • == != ignoring types • === !== considering types • 42 == “42” TRUE • 42 == 42.0 TRUE • 42 === “42” FALSE • > < >= <= • && || !

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

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

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

  44. 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

More Related