Top Banner
ebsite Development Introducing PHP
35

Website Development Introducing PHP The PHP scripting language Syntax derives from C, Java and Perl Open Source Links to MySql database.

Dec 20, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Website Development Introducing PHP The PHP scripting language Syntax derives from C, Java and Perl Open Source Links to MySql database.

Website Development

Introducing PHP

Page 2: Website Development Introducing PHP The PHP scripting language Syntax derives from C, Java and Perl Open Source Links to MySql database.

The PHP scripting language

Syntax derives from C, Java and PerlOpen SourceLinks to MySql database

Page 3: Website Development Introducing PHP The PHP scripting language Syntax derives from C, Java and Perl Open Source Links to MySql database.

How PHP is written

PHP is mixed with htmlPHP is kept between start and end tagsThe PHP interpreter, picks out the PHP code in the HTMLIt then interprets the codeIt then produces HTML

This looks very messyThe ideas are very simple thoughIt is easier than CGI languages like Perl that have to write out the HTML

Page 4: Website Development Introducing PHP The PHP scripting language Syntax derives from C, Java and Perl Open Source Links to MySql database.

: Customer browser

request service

access page

interpretset data

present html

return html

get data

get data

databasescripting language

web server

How server side scripting works

Page 5: Website Development Introducing PHP The PHP scripting language Syntax derives from C, Java and Perl Open Source Links to MySql database.

First PHP instruction

echo(“Mary had a little lamb”);

Mary had a little lamb

gets converted to

you end statements with a semi-colon

Page 6: Website Development Introducing PHP The PHP scripting language Syntax derives from C, Java and Perl Open Source Links to MySql database.

Embedding in HTML

<? php … ?>

<? … ?>

<script language="php">; ...</script>

Code is placed between start and end tags:

Page 7: Website Development Introducing PHP The PHP scripting language Syntax derives from C, Java and Perl Open Source Links to MySql database.

<html><head></head><body>

<?php echo("you can escape into php like this, "); ?>

<? echo("or simply like this, "); ?>

<script language="php">; echo("or this");</script>

</body></html>

Test

Page 8: Website Development Introducing PHP The PHP scripting language Syntax derives from C, Java and Perl Open Source Links to MySql database.

<html><head></head><body>

you can escape into php like this, or simply like this, or this</body></html>

Results in the following HTML:

Page 9: Website Development Introducing PHP The PHP scripting language Syntax derives from C, Java and Perl Open Source Links to MySql database.

Variables

Start with a $Then a letter or underscoreThen any number of letters, underscores or numbers

Examples

$customer$_1_of_the_many$go2work

Page 10: Website Development Introducing PHP The PHP scripting language Syntax derives from C, Java and Perl Open Source Links to MySql database.

PHP supports the following types:

array

floating-point numbers

integer

object

string

Page 11: Website Development Introducing PHP The PHP scripting language Syntax derives from C, Java and Perl Open Source Links to MySql database.

Example Name Result

$a + $b Addition Sum of $a and $b.

$a - $b Subtraction Difference of $a and $b.

$a * $b Multiplication Product of $a and $b.

$a / $b Division Quotient of $a and $b.

$a % $b Modulus Remainder of $a divided by $b.

Table 10-1. Arithmetic Operators

Page 12: Website Development Introducing PHP The PHP scripting language Syntax derives from C, Java and Perl Open Source Links to MySql database.

<html><head></head><body>

<?php

$h = "Hello";$w = "World";

echo($h); echo ($w);

?>

</body></html>

Test

Page 13: Website Development Introducing PHP The PHP scripting language Syntax derives from C, Java and Perl Open Source Links to MySql database.

A useful property of strings

Variables are expanded in strings

$y = 2007;echo (“Year is $y”);

echo (“Year is 2007”);

is equivalent to

Page 14: Website Development Introducing PHP The PHP scripting language Syntax derives from C, Java and Perl Open Source Links to MySql database.

<html><head></head><body><?php

$h = "Hello";$w = "World";$y = 2007;

echo("This is another $h $w $y program");

?></body></html>

Test

Page 15: Website Development Introducing PHP The PHP scripting language Syntax derives from C, Java and Perl Open Source Links to MySql database.

A loop:

for (exp1; exp2; exp3) statement;

exp1

exp2 statement

exp3

true

false

the statement can be a compound statement surrounded by { and }

Page 16: Website Development Introducing PHP The PHP scripting language Syntax derives from C, Java and Perl Open Source Links to MySql database.

<html><head></head><body>

<?php

for ($i=1; $i<10; $i=$i+1) {echo("$i ");

};?>

</body></html>

Test

Page 17: Website Development Introducing PHP The PHP scripting language Syntax derives from C, Java and Perl Open Source Links to MySql database.

Auto-increment, auto-decrement

$i++

$i--

add one to $i

subtract one from $i

Page 18: Website Development Introducing PHP The PHP scripting language Syntax derives from C, Java and Perl Open Source Links to MySql database.

<html><head></head><body>

<?php

for ($i=1; $i<15; $i++) {echo("$i ");

};?>

</body></html>

Test

Page 19: Website Development Introducing PHP The PHP scripting language Syntax derives from C, Java and Perl Open Source Links to MySql database.

Some clever stuff with loops

You can include html in the loop statement

for (exp1; exp2; exp3) {php codehtml code…

};

Page 20: Website Development Introducing PHP The PHP scripting language Syntax derives from C, Java and Perl Open Source Links to MySql database.

<html><head></head><body>

<table border="1"> <tr> <td>A simple</td> </tr> <tr> <td>table</td> </tr></table>

</body>

</html>

HTML for creating a table

Page 21: Website Development Introducing PHP The PHP scripting language Syntax derives from C, Java and Perl Open Source Links to MySql database.

<html><head></head><body>

<table border="1"><?php for ($i = 1; $i <6; $i++) { ?> <tr> <td> <?php echo("cell $i"); ?> </td> </tr><?php }; ?></table>

</body>

</html>

escape back to html

Test

Page 22: Website Development Introducing PHP The PHP scripting language Syntax derives from C, Java and Perl Open Source Links to MySql database.

<html><head></head><body>

<table border="1"> <tr> <td> cell 1 </td> </tr> <tr> <td> cell 2 </td> </tr> <tr> <td> cell 3 </td> </tr> <tr> <td> cell 4 </td> </tr> <tr> <td> cell 5 </td> </tr></table>

</body>

</html>

Results in

<html><head></head><body>

<table border="1"><?php for ($i = 1; $i <6; $i++) { ?> <tr> <td> <?php echo("cell $i"); ?> </td> </tr><?php }; ?></table>

</body>

</html>

Page 23: Website Development Introducing PHP The PHP scripting language Syntax derives from C, Java and Perl Open Source Links to MySql database.

<html><head></head><body>

<table border="1"><?php for ($i = 1; $i <10; $i++) { ?> <tr>

<?php for($j = 1; $j < 4; $j++) { ?> <td> <?php echo("cell $i, $j"); ?> </td> <?php }; ?> </tr><?php }; ?></table>

</body>

</html>Test

Page 24: Website Development Introducing PHP The PHP scripting language Syntax derives from C, Java and Perl Open Source Links to MySql database.

From here on I will introduce constructs as neededLook at the manual to find out moreLinks to the manual and other sources will be embedded in the slidesSome useful references directly into the manual follow

Page 25: Website Development Introducing PHP The PHP scripting language Syntax derives from C, Java and Perl Open Source Links to MySql database.

Chapter 11. Control Structures

if else elseif while do..while for foreach break continue switch

Page 26: Website Development Introducing PHP The PHP scripting language Syntax derives from C, Java and Perl Open Source Links to MySql database.

Table 10-3. Comparison Operators

Example Name Result

$a == $b Equal True if $a is equal to $b.

$a != $b Not equal True if $a is not equal to $b.

$a < $b Less than True if $a is strictly less than $b.

$a > $b Greater than True if $a is strictly greater than $b.

$a <= $b Less than or equal to True if $a is less than or equal to $b.

$a >= $b Greater than or equal to True if $a is greater than or equal to $b.

Page 27: Website Development Introducing PHP The PHP scripting language Syntax derives from C, Java and Perl Open Source Links to MySql database.

PHP Array Example

Test

Page 28: Website Development Introducing PHP The PHP scripting language Syntax derives from C, Java and Perl Open Source Links to MySql database.

PHP Form Handling

Page 29: Website Development Introducing PHP The PHP scripting language Syntax derives from C, Java and Perl Open Source Links to MySql database.

What this example does

• The page above contains two input fields and a submit button.

• When the user fills in this form and clicks on the submit button, the form data is sent to the "welcome.php" file.

• The "welcome.php" file looks contains these lines:

Test

Page 30: Website Development Introducing PHP The PHP scripting language Syntax derives from C, Java and Perl Open Source Links to MySql database.

PHP File Handling

• The fopen() function is used to open files in PHP.

• The first parameter of this function contains the name of the file to be opened and the second parameter specifies in which mode the file should be opened:

Page 31: Website Development Introducing PHP The PHP scripting language Syntax derives from C, Java and Perl Open Source Links to MySql database.

File Modes

Page 32: Website Development Introducing PHP The PHP scripting language Syntax derives from C, Java and Perl Open Source Links to MySql database.

Example

• The following example generates a message if the fopen() function is unable to open the specified file:

Page 33: Website Development Introducing PHP The PHP scripting language Syntax derives from C, Java and Perl Open Source Links to MySql database.

Reading a File Line by Line

The example below reads a file line by line, until the end of file is reached:

Test

Page 34: Website Development Introducing PHP The PHP scripting language Syntax derives from C, Java and Perl Open Source Links to MySql database.

Writing to the File

So we could use the Post variable to write data entered via a form to a file. Then read it back line-by-line.

Basis for a simple guestbook.

Page 35: Website Development Introducing PHP The PHP scripting language Syntax derives from C, Java and Perl Open Source Links to MySql database.

Summary

PHP is a server side scripting languageIt is merged in with HTMLThe interpreter produces HTMLIt has a full range of variables and control structuresWe shall later look at functions and objects