Presentation is loading. Please wait.

Presentation is loading. Please wait.

IST 210: PHP BASICS IST 210: Organization of Data IST210 1.

Similar presentations


Presentation on theme: "IST 210: PHP BASICS IST 210: Organization of Data IST210 1."— Presentation transcript:

1 IST 210: PHP BASICS IST 210: Organization of Data IST210 1

2 Previous Classes Review IST210 2 HTML Basics

3 Place Your Page on Web Step 1. Go to your webspace folder Open any folder. On the left-hand side, expand Computer, click on “IST UP Webspace”. Step 2. Place your helloWorld.html in the webspace folder Step 3. Open a browser, visit my.up.ist.psu.edu/YourPSUID/helloWorld.html IST210 3 If I put helloWorld.html file on my desktop, does this work?

4 IST 210: PHP BASICS IST 210: Organization of Data IST210 4

5 How A Web Server Works IST210 5 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

6 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 IST210 6

7 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 IST210 7

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

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

10 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 IST210 10

11 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 IST210 11

12 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 IST210 12

13 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: Originally: Personal Home Page; Now: Hypertext Preprocessor PHP is used as the server-side programming language on 75% of all Web sites E.G. Facebook, Wikipedia IST210 13

14 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 IST210 14 ( ) …

15 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/PSUID/ helloworld.php http://my.up.ist.psu.edu/PSUID/ helloworld.php IST210 15 <?php echo "Hello World!"; ?> Try it

16 PHP Must Work Through a Web Server Find the helloworld.php file in your webspace Double Click the file in the folder and see what happens Now visit through your web browser: http://my.up.ist.psu.edu/PSUID/helloworld.php What happened? PHP must work thru a web server! Try “View Page Source” IST210 16

17 echo – deep dive. IST210 17 <?php echo "Hello World!"; ?> echo means “print it to html” Double quotes pair “” define a string Semicolon ; means the end of a sentence

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

19 echo IST210 19 <?php echo " Hello World! "; ?> Try it

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

21 echo: Exercise 1 Answer IST210 21 <?php echo "Welcome to Penn State "; ?> How to modify codes to get the output on the right ? Penn State in bold

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

23 echo: Exercise 2 Answer IST210 23 <?php echo "Welcome to Penn State "; echo "I like red "; ?> How to modify codes to get the output on the right ? A break after “Penn State” Red in color red

24 echo IST210 24 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

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

26 echo IST210 26 echo "Welcome "." "."Hello World"; echo "Welcome Hello World"; echo "Welcome"; echo " "; echo "Hello World";

27 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 IST210 27

28 Variables IST210 28 <?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

29 Variables: Exercise 1 IST210 29 <?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?

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

31 Variables: Exercise 2 IST210 31 <?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?

32 Variables: Exercise 2 Answer IST210 32 <?php $x = 1; //integer $y = 2.2; //float $z = "hello"; //string echo "z is \" $z \""; ?> How to modify codes to get the output on the right?

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

34 Variables: Exercise 3 Answer IST210 34 <?php $name = "John"; echo "My name is $name."; ?> Variable name is sensitive. Should be $name not $Name

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

36 Variables: Exercise 4 Answer IST210 36 <?php $x = "Penn State"; echo "I love $x "; ?>

37 Variables: Exercise 1 IST210 37 <?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?

38 Variables: Exercise 2 IST210 38 <?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)?

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

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

41 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 IST210 41 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!

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

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

44 Functions IST210 44 1. Create a date.php in your webspace 2. Visit it through web browser Try it String concatenation

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

46 Functions IST210 46 <?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

47 Learn more about PHP Reference websites http://en.wikibooks.org/wiki/PHP http://php.net/manual/en/index.php 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.php Learn from the example IST210 47

48 Reminder Assignment 2 due on Monday Sept 14 th 11:59PM! IST210 48


Download ppt "IST 210: PHP BASICS IST 210: Organization of Data IST210 1."

Similar presentations


Ads by Google