Presentation is loading. Please wait.

Presentation is loading. Please wait.

IST 210: PHP Basics IST 210: Organization of Data IST2101.

Similar presentations


Presentation on theme: "IST 210: PHP Basics IST 210: Organization of Data IST2101."— Presentation transcript:

1 IST 210: PHP Basics IST 210: Organization of Data IST2101

2 Previous Classes Review IST2102 HTML Basics

3 How A Web Server Works IST2103 http://my.up.ist.psu.edu/zul17/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 zul17 Return to user 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 IST2104

5 Dynamic Web Contents Through scripts (e.g., PHP) –Programs that can generate dynamic HTML results Two types of scripts –Client side: running inside a browser –Server side: running inside a web server IST2105

6 Hello World in PHP IST2106 http://my.up.ist.psu.edu/zul17/helloworld.php <?php echo "Hello World!"; ?> Hello World!

7 Dynamic PHP Example: Show Date IST2107 http://my.up.ist.psu.edu/zul17/date.php Today is: 08/27/12

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 IST2108

9 PHP Must Work Through a Web Server Check the date.php file in the webpage Click the file in the folder 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!!! IST2109 http://my.up.ist.psu.edu/zul17/date.php

10 Differences Between PHP and HTML Which language is used for display format? –HTML Which language is programming language? –PHP Which runs on the client side (the system on which the page is being viewed) ? –HTML Which runs on the server side (the system from which the page comes)? –PHP IST21010

11 PHP and HTML HTML –is a language used to describe to a browser how to display text and other objects in a browser window –NOT a programming language –works on a client computer (the system on which the page is being viewed PHP –is a scripting language, and can be used to create HTML page –runs on the server (the system from which the page comes) –is a full-fledged programming language programming languages: C, C++, C#, JAVA, Python IST21011

12 About PHP Created in 1994 by Rasmu 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 IST21012

13 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 IST21013 ( ) …

14 PHP – Getting Started Step 1. Open a NotePad++ Step 2. Input codes Step 3. Save it to “helloworld.php” to your webspace Step 4. Open a web browser, and visit – http://my.up.ist.psu.edu/PSUI D/helloworld.php http://my.up.ist.psu.edu/PSUI D/helloworld.php IST21014 <?php echo "Hello World!"; ?> Try it

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

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

17 echo IST21017 <?php echo " Hello World! "; ?> Try it

18 echo: Exercise 1 IST21018 <?php echo " Hello World! "; ?> Think How to modify codes to get the output on the right ? Penn State in bold

19 echo: Exercise 2 IST21019 How to modify codes to get the output on the right ? A break after “Penn State” Red in color red <?php echo " Hello World! "; ?> Think

20 echo IST21020 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!"; ?> Try it Multiple PHP segments

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

22 echo IST21022 echo "Welcome "." "."Hello World"; echo "Welcome Hello World"; echo "Welcome"; echo " "; echo "Hello World";

23 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.phphttp://php.net/manual/en/language.types.php IST21023

24 Variables IST21024 <?php $x = 1; //integer $y = 2.2; //float $z = "hello"; //string echo "x is $x"; echo "x is ".$x; ?> 1.Create a test.php in your webspace 2.Input following codes 3.Visit through web browser http://my.up.ist.psu.edu/PSUID/test.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

25 Variables: Exercise 1 IST21025 <?php $x = 1; //integer $y = 2.2; //float $z = “hello”; //string echo "x is $x"; ?> Think How to modify codes to get the output on the right?

26 Variables: Exercise 2 IST21026 <?php $x = 1; //integer $y = 2.2; //float $z = “hello”; //string echo "z is $z"; ?> Think How to modify codes to get the output on the right (with quotes on hello)?

27 Variables: Exercise 3 IST21027 <?php $name = "John"; echo "My name is $Name."; ?> Debug Name is not shown. Where is the bug?

28 Variables: Exercise 4 IST21028 <?php $x = "Penn State"; echo "I love $x"; ?> Think How to make the string in bold?

29 Expressions Any legal combination of symbols that represents a value –Each programming language and application has its own rules for what is legal and illegal Every expression consists of at least one operand and can have one or more operators –Operands are values –Operators are symbols that represent particular actions Examples –5+3 –$x*$y –$x > $y –$x <> $y –$x == $y More: –http://www.w3schools.com/php/php_operators.asp IST21029 A==B: boolean result, true or false A=B: assign variable B’s value to variable A A very common mistake: if (x = 1) then …  This is always true!

30 Functions IST21030 1. Create a date.php in your webspace 2. Visit it through web browser Try it

31 Functions echo — Output one or more strings –String is defined in a pair of " " –String concatenate using. IST21031 <?php echo "A: What is the day today? "; echo "B: ".date("l, F d Y")." "; echo "A: Not Friday yet?! " ?> Try it

32 Functions IST21032 <?php <?php echo "Today is: ".date("m/d/y").” ”; echo “A random number in [1,10]”.rand(1,10).” ”; $name = "John"; echo "The length of string \" $name \" is ".strlen($name); ?> ?> More string functions http://php.net/manual/en/book.strings.php More math functions http://php.net/manual/en/ref.math.php

33 Learn more about PHP Reference websites –http://en.wikibooks.org/wiki/PHPhttp://en.wikibooks.org/wiki/PHP –http://php.net/manual/en/index.phphttp://php.net/manual/en/index.php Learn by yourself –http://www.lynda.com/PHP-training-tutorials/282-0.htmlhttp://www.lynda.com/PHP-training-tutorials/282-0.html Learn from experience –Try it yourself! –Try different ways to write the codes. –Learn from debugging Learn from examples –Search “php echo” on google –http://php.net/manual/en/function.echo.phphttp://php.net/manual/en/function.echo.php –Learn from the example IST21033


Download ppt "IST 210: PHP Basics IST 210: Organization of Data IST2101."

Similar presentations


Ads by Google