CMPT241 Web Programming Intro to PHP.

Slides:



Advertisements
Similar presentations
PHP I.
Advertisements

JavaScript FaaDoOEngineers.com FaaDoOEngineers.com.
Introduction to PHP MIS 3501, Fall 2014 Jeremy Shafer
1 CSC 551: Web Programming Spring 2004 client-side programming with JavaScript  scripts vs. programs  JavaScript vs. JScript vs. VBScript  common tasks.
Objectives Using functions to organize PHP code
Working with JavaScript. 2 Objectives Introducing JavaScript Inserting JavaScript into a Web Page File Writing Output to the Web Page Working with Variables.
Introduction to PHP (Part-1) Basharat Mahmood, Department of Computer Science,CIIT,Islamabad, Pakistan. 1.
PHP Server-side Programming. PHP  PHP stands for PHP: Hypertext Preprocessor  PHP is interpreted  PHP code is embedded into HTML code  interpreter.
Server side basics CS URLs and web servers  Usually when you type a URL in your browser:  Your computer looks up the server's IP address using.
Server side basics.
Server side basics CS URLs and web servers  Usually when you type a URL in your browser:  Your computer looks up the server's IP address using.
1 CS428 Web Engineering Lecture 18 Introduction (PHP - I)
PHP By Dr. Syed Noman Hasany. PHP PHP was originally created by Rasmus Lerdorf in PHP stands for PHP: Hypertext Preprocessor (a recursive acronym).
PHP Workshop ‹#› PHP: The Basics. PHP Workshop ‹#› What is it? PHP is a scripting language commonly used on web servers. –Stands for “PHP: Hypertext Preprocessor”
August Chapter 1 - Essential PHP spring into PHP 5 by Steven Holzner Slides were developed by Jack Davis College of Information Science and Technology.
ALBERT WAVERING BOBBY SENG. Week Whatever: PHP  Announcements/questions/complaints.
CSC 318 WEB APPLICATION DEVELOPMENT.  Introduction to Server Scripting language  Client VS Server  Introduction to PHP  PHP Files and Syntax  Function.
PHP Overview CS PHP PHP = PHP: Hypertext Preprocessor Server-side scripting language that may be embedded into HTML One goal is to get PHP files.
PHP == ‘ Hypertext Preprocessor ’ Open-source, server-side scripting language Used to generate dynamic web-pages PHP scripts reside between reserved PHP.
Introduction to PHP and Server Side Technology. Slide 2 PHP History Created in 1995 PHP 5.0 is the current version It’s been around since 2004.
CSE 154 LECTURE 6: EMBEDDED PHP. PHP syntax template HTML content HTML content HTML content... PHP any contents of.
PHP. Why should we learn web programming? No need write socket programming. - You can forget TCP/IP & OSI layers. - Web server handles socket tasks for.
INTERNET APPLICATION DEVELOPMENT For More visit:
NMED 3850 A Advanced Online Design January 26, 2010 V. Mahadevan.
School of Computing and Information Systems CS 371 Web Application Programming PHP - Basics Serving up web pages.
Nael Alian Introduction to PHP
IST 210: PHP BASICS IST 210: Organization of Data IST210 1.
CMPS 211 JavaScript Topic 1 JavaScript Syntax. 2Outline Goals and Objectives Goals and Objectives Chapter Headlines Chapter Headlines Introduction Introduction.
XP Tutorial 10New Perspectives on Creating Web Pages with HTML, XHTML, and XML 1 Working with JavaScript Creating a Programmable Web Page for North Pole.
November 2003Bent Thomsen - FIT 6-11 IT – som værktøj Bent Thomsen Institut for Datalogi Aalborg Universitet.
Tutorial 10 Programming with JavaScript
Creating Dynamic Web Pages Using PHP and MySQL CS 320.
Web Scripting [PHP] CIS166AE Wednesdays 6:00pm – 9:50pm Rob Loy.
Introduction to PHP “PHP is a server-side scripting language designed specifically for the Web. Within an HTML page, you can embed PHP code that will be.
Introduction to PHP A user navigates in her browser to a page that ends with a.php extension The request is sent to a web server, which directs the request.
Open Source Software Unit – 3 Presented By Mr. R.Aravindhan.
PHP - Basic Language Constructs CSCI 297 Scripting Languages - Day Two.
PHP Programming with MySQL Slide 4-1 CHAPTER 4 Functions and Control Structures.
Variables and ConstantstMyn1 Variables and Constants PHP stands for: ”PHP: Hypertext Preprocessor”, and it is a server-side programming language. Special.
PHP Teresa Worner. What is it? PHP: Hypertext Preprocessor server-side scripting language open source cross-platform compatible with almost all servers.php.php3.phtml.
PHP. What is PHP? PHP stands for PHP: Hypertext Preprocessor PHP is a server-side scripting language, like ASP PHP scripts are executed on the server.
Intro to PHP IST2101. Review: HTML & Tags 2IST210.
Introduction to PHP Advanced Database System Lab no.1.
What is PHP? PHP stands for PHP: Hypertext Preprocessor PHP is a server-side scripting language, like ASP PHP scripts are executed on the server PHP supports.
CSE 154 LECTURE 5: INTRO TO PHP. URLs and web servers usually when you type a URL in your browser: your computer looks up the.
JavaScript Scripting language What is Scripting ? A scripting language, script language, or extension language is a programming language.
Introduction to PHP.
David Lawrence 7/8/091Intro. to PHP -- David Lawrence.
IT ELECTIVE 2.  Web server Can refer to either the hardware (the computer) or the software (the computer application) that helps to deliver content that.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting PHP Basics.
8 th Semester, Batch 2009 Department Of Computer Science SSUET.
1 PHP Intro PHP Introduction After this lecture, you should be able to: Know the fundamental concepts of Web Scripting Languages in general, PHP in particular.
Dr. Abdullah Almutairi Spring PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages. PHP is a widely-used,
CSE 154 LECTURE 14: INTRO TO PHP. URLs and web servers usually when you type a URL in your browser: your computer looks up the.
PHP Tutorial. What is PHP PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages.
IST 210: PHP Basics IST 210: Organization of Data IST2101.
PHP. What is PHP? PHP Hypertext Processor – Dynamic web development – Scripting language – Can be procedural or OOP(preferred) – PHP code can be embedded.
First Steps in PHP Creating Very Simple PHP Scripts SoftUni Team Technical Trainers Software University
PHP using MySQL Database for Web Development (part II)
PHP for Server-Side Programming
Chapter 5 Scripting Language
Server-Side Application and Data Management IT IS 3105 (Spring 2010)
PHP Introduction.
PHP.
HYPERTEXT PREPROCESSOR BY : UMA KAKKAR
Tutorial 6 PHP & MySQL Li Xu
CMPT241 Web Programming Intro to PHP.
PHP an introduction.
SEEM 4540 Tutorial 4 Basic PHP based on w3Schools
Presentation transcript:

CMPT241 Web Programming Intro to PHP

Homework 5 Two parts Summary of what you have learned this week (paragraphs, lists) A web page that uses those techniques (CSS layout) You need to show/add something different in each homework.

Project 2 Due date extended to March 10 upload to the snoopy server upload to Moodle

Programming for Fun (PFF) When: Tuesday, March 10 3:30PM Where: RLC 107 Team based: one, two or three people per team Languages: C/C++ and Java IDEs: Visual Studio, Eclipse, NetBeans Event Schedule 3:30 – 5:30 pm – Contest 5:30 pm – Award Ceremony 5:30 pm – Pizza Party Interested? Register your team online or in RLC 203 (Find the registration list on the bulletin board) Contact Dr. Tina Tian for questions.

URLs and web servers http://server/path/file Usually when you type a URL in your browser: Your computer looks up the server's IP address using DNS 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

browser requests a .html file browser requests a .php 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.

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

URLs and web servers (cont.) http://www.facebook.com/home.php Some URLs actually specify programs that the web server should run, and then send their output back to you as the result: The above URL tells the server facebook.com to run the program home.php and send back its output

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

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

XAMPP Web server solution package Apache HTTP Server MySQL database PHP interpreters ...

What is PHP? 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

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

Lifecycle of a PHP web request Hello.php Hello world! it, then sends result across the network script produces output that becomes the response sent back User’s computer Server computer

Why PHP? Free and open source Compatible Simple Supported by most popular web servers There are more than 200 million websites using PHP. http://php.net/usage.php Simple lots of build-in functionality, familiar syntax http://www.php.net/ Interpreted, not compiled. Procedural, not OO. Not standalone. Relaxed syntax: fewer, looser data types

Hello World! The following contents could go into a file hello.php: print "Hello, world!"; ?> PHP Hello world! output block or file of PHP code begins with <?php and ends with ?> PHP statements, function declarations, etc. appear between these endpoints 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 Hello world! you can't view your .php page on your local hard drive; you'll either see nothing or see the PHP source code if you upload the file to a PHP-enabled web server, requesting the .php file will run the program and send you back its output Hello world!

2. Put PHP file in xampp/htdocs/ 1. Start Apache in XAMPP 2. Put PHP file in xampp/htdocs/ 3. In your browser, type http://localhost/filename.php default: index.php, if there is no index.html snoopy.rlc.manhattan.edu/~Your_ID/filename.php

PHP syntax template HTML content <?php PHP code ?> HTML content ... PHP Mode switchers 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)

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

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 some PHP programmers use the equivalent echo instead of print What is weird with this slide? \n has no effect, no line change 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

Comments like Java, but # is also allowed # 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 names are case sensitive separate multiple words with _ $name = expression; PHP $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) names are case sensitive; separate multiple words with _

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, 0.0, empty string, or empty array. A variable can change type as the program is running. names are case sensitive; separate multiple words with _

Variables 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 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 gettype(2.71) “double” gettype(42) “int” gettype(“42”) “string” is_string(“hello”) TRUE is_int(3.14) FALSE

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

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

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

Math operations math functions (no need to include/import) $b = 4; $c = sqrt(pow($a, 2) + pow($b, 2)); PHP math functions (no need to include/import) Google “php” + function abs ceil cos floor log(n,base) log10 max min pow rand(min,max) round sin sqrt tan break and continue keywords also behave as in Java math constants M_PI M_E M_LN2 3.14159265.. e, 2.7182818.. loge2, 0.693147..

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 $feels_like_summer = FALSE; $php_is_great = TRUE; $student_count = 7; $nonzero = (bool) $student_count; # TRUE 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 elements FALSE prints as an empty string (no output); TRUE prints as a 1 can cast to boolean using (bool) TRUE and FALSE keywords are case insensitive

String Type zero-based indexing using bracket notation $favorite_food = "Ethiopian"; print $favorite_food[2]; $favorite_food = $favorite_food . " cuisine"; print $favorite_food; PHP 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 ''

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 + 9 466 (1 + 3). “5”. (7 + 9) “4516”

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

String Functions (cont.) Name Java Equivalent strlen length strpos indexOf substr substring strtolower, strtoupper toLowerCase, toUpperCase trim strcmp compareTo CS380

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” htmlspeccialchars(“<p>Wow!</p>”) “<p>Wow!</p>” ord(“A”) 65 chr(66) “B” sprintf(“%5s %08d”, “hi”, 90210) “ hi 00090210”

Interpreted Strings strings inside " " are interpreted $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.) 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.) $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 function can test if a variable is NULL using the isset function NULL prints as an empty string (no output)

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

Operators == != ignoring types === !== considering types 42 == “42” TRUE 42 == 42.0 TRUE 42 === “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) 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) while (condition) { statements; } PHP do { statements; } while (condition); PHP break and continue keywords also behave as in Java break and continue keywords also behave as in Java while 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 In this PHP exercise, you will use a conditional statement to determine what gets printed to the browser. Write a script that gets the current month and prints one of the following responses, depending on whether it's August or not: It's August, so it's really hot. Not August, so at least not in the peak of the heat. Hint: the function to get the current month is 'date('F', time())' for the month's full name.

PHP exercise 2 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.

PHP exercise 3 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: 1 2 3 4 5 6 7 8 10 12 14 9 15 18 21 16 20 24 28 25 30 35 36 42 49

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

Homework 6 Due by the end of the week Project 2: links of validators .php file supporting css file idea: ASCII table Project 2: links of validators