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

Post on 04-Jan-2016

218 views 0 download

transcript

IST210 1

Intro to PHP

IST210 2

Review: HTML & Tags

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

<body>

Hello World!

</body>

</html>

Get file: helloworld.htmlFrom folder zuz22Return to user

User Web Server

IST210 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

IST210 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

IST210 6

Dynamic Page Example: Show Datehttp://my.up.ist.psu.edu/zuz22/date.php

<html><body><?php echo "Today is: ".date("m/d/y") ?></body></html>

<html>

<body>

Today is: 01/16/14

</body>

</html>

IST210 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

<html><body><?php echo "Today is: ".date("m/d/y") ?></body></html>

IST210 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

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

http://my.up.ist.psu.edu/zuz22/date.php

IST210 10

PHP and HTML

HTML PHP

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 side Runs on the server side

IST210 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

IST210 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

<?php … ?> (<? … ?>)

<script language=“php”> … </script>

<html><body><?php echo "Today is: ".date("m/d/y") ?></body></html>

IST210 13

echo

echo means “print it to html”Double quotes pair “” define a stringSemicolon ; means the end of a sentence

<html> <body>

<?php echo "Hello World!";?>

</body></html>

IST210 14

echo

<?phpecho "Hello World!";

?>

HTML is not a strict language.It is OK to only have PHP part.

IST210 15

echo: HTML tags

<html> <body>

<?php echo "<h1>Hello World!<h1>";?>

</body></html>

IST210 16

Exercise 1: echo

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

<html> <body>

<?php echo "<h1>Hello World!<h1>";?>

</body></html>

IST210 17

echo: Multiple PHP segments

<html><body>

This is HTML part. <br>Hello world in HTML. <br><?php

echo "Hello World in PHP!<br>";?>Back to HTML again! <br><?php

echo "Hello World in PHP again!";?>

</body></html>

IST210 18

echo

• echo — Output one or more strings– String is defined in a pair of " "– String concatenate using .

<html><body>

<?phpecho "Welcome "."<br>"."Hello World";

?></body></html>

IST210 19

echo

echo "Welcome "."<br>"."Hello World";

echo "Welcome <br> Hello World";

echo "Welcome";echo "<br>";echo "Hello World";

IST210 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

IST210 21

Variables

<html><body>

<?php$x = 1; //integer$y = 2.2; //float$z = "hello"; //stringecho "x is $x<br>"; echo "x is ".$x."<br>"; echo "y is $y<br>";echo "z is $z";

?></body>

</html>

1. Open your helloworld.php2. Input following codes3. 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

IST210 22

Variables: Debug

<html><body>

<?php$name = "John";echo "My name is $Name.";

?></body>

</html>

Name is not shown. Where is the bug?

IST210 23

Variables: Output Quotes

<html><body>

<?php$x = 1; //integer$y = 2.2; //float$z = "hello"; //stringecho "z is \"$z\"";

?></body>

</html>

In PHP, you can use \ to escape quotes.

quotes on hello

IST210 24

Variables: HTML Tags

<html><body>

<?php$x = "Penn State";echo "I love <b>$x</b>";

?></body>

</html>

Penn State in bold