Website Development with PHP and MySQL

coordinatedcapableSoftware and s/w Development

Nov 4, 2013 (3 years and 8 months ago)

72 views

Website Development with
PHP and MySQL

First Look at the Programming
Language

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 html

PHP is kept between start and end tags

The PHP interpreter, picks out the PHP code in the HTML

It then interprets the code

It then produces HTML

This looks very messy

The ideas are very simple though

It is easier than CGI languages like Perl that have to
write out the HTML


: Customer

browser

request service

access page

interpret

set data

present html

return html

get data

get data

database

scripting

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 underscore

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



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 = 2010;

echo (“Year is $y”);

echo (“Year is 2010”);

is equivalent to

<html>

<head>

</head>

<body>

<?php



$h = "Hello";


$w = "World";


$y = 2010;




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 code


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

Look at the manual to find out more

Links to the
manual

and other sources will be embedded in the slides

Some useful references directly into the manual follow

Control Structures


if

else

elseif

while

do..while

for

foreach

break

continue

switch

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 language

It is merged in with HTML

The interpreter produces HTML

It has a full range of variables and control structures

We shall later look at functions and objects