+ All Categories
Home > Documents > Website Development Introducing PHP The PHP scripting language Syntax derives from C, Java and Perl...

Website Development Introducing PHP The PHP scripting language Syntax derives from C, Java and Perl...

Date post: 20-Dec-2015
Category:
View: 236 times
Download: 0 times
Share this document with a friend
Popular Tags:
35
ebsite Development Introducing PHP
Transcript

Website Development

Introducing PHP

The PHP scripting language

Syntax derives from C, Java and PerlOpen SourceLinks 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

: 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

First PHP instruction

echo(“Mary had a little lamb”);

Mary had a little lamb

gets converted to

you end statements with a semi-colon

Embedding in HTML

<? php … ?>

<? … ?>

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

Code is placed between start and end tags:

<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

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

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

Results in the following HTML:

Variables

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

Examples

$customer$_1_of_the_many$go2work

PHP supports the following types:

array

floating-point numbers

integer

object

string

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

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

<?php

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

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

?>

</body></html>

Test

A useful property of strings

Variables are expanded in strings

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

echo (“Year is 2007”);

is equivalent to

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

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

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

?></body></html>

Test

A loop:

for (exp1; exp2; exp3) statement;

exp1

exp2 statement

exp3

true

false

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

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

<?php

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

};?>

</body></html>

Test

Auto-increment, auto-decrement

$i++

$i--

add one to $i

subtract one from $i

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

<?php

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

};?>

</body></html>

Test

Some clever stuff with loops

You can include html in the loop statement

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

};

<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

<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

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

<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

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

Chapter 11. Control Structures

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

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.

PHP Array Example

Test

PHP Form Handling

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

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:

File Modes

Example

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

Reading a File Line by Line

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

Test

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.

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


Recommended