Presentation is loading. Please wait.

Presentation is loading. Please wait.

Intro to PHP IST2101. Review: HTML & Tags 2IST210.

Similar presentations


Presentation on theme: "Intro to PHP IST2101. Review: HTML & Tags 2IST210."— Presentation transcript:

1 Intro to PHP IST2101

2 Review: HTML & Tags 2IST210

3 How A Web Server Works http://my.up.ist.psu.edu/zuz22/HelloWorld.html Find the IP address of the server my.up.ist.psu.edu Send the request to the server HelloWorld.html Hello World! Get file: helloworld.html From folder zuz22 Return to user 3IST210 User Web Server

4 Problems with HTML Static pages – Cannot provide dynamic contents based on user preferences and interest Users often need different things in different conditions – Google – Facebook – ANGEL, webmail 4IST210

5 Dynamic Web Contents Through PHP scripts – Programs that can generate dynamic HTML results Two types of scripts – Client side: running inside a browser HTML – Server side: running inside a web server PHP 5IST210

6 Dynamic Page Example: Show Date http://my.up.ist.psu.edu/zuz22/date.php Today is: 01/16/14 6IST210

7 Try it yourself Start Notepad or Notepad++ Create a PHP document to show the date Save as a php document, e.g. date.php Place your php document on IST web space Access it through URL: – http://my.up.ist.psu.edu/username/filename 7IST210

8 How It Works: More Details When a browser requests an HTML file, the HTTP server returns the file When a browser requests an PHP file – The web server passes the request to a PHP interpreter – The PHP interpreter reads the PHP file, line by line, and executes the scripts in the file – Finally, the executed result is returned to the web server, which delivers HTML contents to the browser 8IST210

9 PHP Must Work Through a Web Server Check the date.php file in the folder Right click the file in the folder, Open it with a web browser, and see what happens. Try “View Page Source” DON’T CLICK ON PHP PAGE DIRECTLY!!! PUT PHP PAGE ON WEBSPACE AND VISIT THROUGH WEB BROWSER!!! 9IST210 http://my.up.ist.psu.edu/zuz22/date.php

10 PHP and HTML HTMLPHP Used to how to display text and other objects in a browser window Used to dynamically create web pages written in HTML Runs on the client sideRuns on the server side IST21010

11 About PHP (Hypertext Preprocessor) Created in 1994 by Rasmus Lerdorf A simple set of Common Gateway Interface binaries written in the C programming language Originally used to track visits to his online resume “Personal Home Page tools” PHP is used as the server-side programming language on 75% of all Web sites – E.G. Facebook, Wikipedia IST21011

12 PHP – Getting started PHP code segments are mixed with HTML source Escaping from HTML (To tell PHP processor: this is PHP code) A PHP parser starts to execute codes when it encounters a start tag The parser does not process non-PHP code Two options indicating the embedded PHP codes 12IST210 ( ) …

13 echo IST21013 echo means “print it to html” Double quotes pair “” define a string Semicolon ; means the end of a sentence <?php echo "Hello World!"; ?>

14 echo IST21014 <?php echo "Hello World!"; ?> HTML is not a strict language. It is OK to only have PHP part.

15 echo: HTML tags IST21015 <?php echo " Hello World! "; ?>

16 Exercise 1: echo IST21016 Step 1: Open NotePad++ and create “helloworld.php” in your webspace Step 2: Using the codes above as reference, write codes to get the output on the right: “Penn State” in bold A break after “Penn State” “red” in color red <?php echo " Hello World! "; ?>

17 echo: Multiple PHP segments IST21017 This is HTML part. Hello world in HTML. <?php echo "Hello World in PHP! "; ?> Back to HTML again! <?php echo "Hello World in PHP again!"; ?>

18 echo echo — Output one or more strings – String is defined in a pair of " " – String concatenate using. IST21018 <?php echo "Welcome "." "."Hello World"; ?>

19 echo IST21019 echo "Welcome "." "."Hello World"; echo "Welcome Hello World"; echo "Welcome"; echo " "; echo "Hello World";

20 Variables Data types – Similar to C++ or Java Int, Float, Boolean, String Array (next class), Object (not covered in our class) Variables: a symbol or name that stands for a value – PHP variables start with $ – You can use variables without defining the type $x = 5; $pi = 3.14; $name = "George"; – Name your variables in meaningful ways $s = "matrix" vs. $servername = “matrix” – Case sensitive! More data types – http://php.net/manual/en/language.types.php http://php.net/manual/en/language.types.php IST21020

21 Variables IST21021 <?php $x = 1; //integer $y = 2.2; //float $z = "hello"; //string echo "x is $x "; echo "x is ".$x." "; echo "y is $y "; echo "z is $z"; ?> 1.Open your helloworld.php 2.Input following codes 3.Visit through web browser http://my.up.ist.psu.edu/PSUID/helloworld.php Try it $x can be put insides quotes or outside quotes. When $x is outside of quotes, remember to use. to concatenate two strings

22 Variables: Debug IST21022 <?php $name = "John"; echo "My name is $Name."; ?> Name is not shown. Where is the bug?

23 Variables: Output Quotes IST21023 <?php $x = 1; //integer $y = 2.2; //float $z = "hello"; //string echo "z is \"$z\""; ?> In PHP, you can use \ to escape quotes. quotes on hello

24 Variables: HTML Tags IST21024 <?php $x = "Penn State"; echo "I love $x "; ?> Penn State in bold


Download ppt "Intro to PHP IST2101. Review: HTML & Tags 2IST210."

Similar presentations


Ads by Google