Web Designs & Languages
Web Designs & Languages
PHP
What is PHP?
PHP
PHP: Hypertext
Preprocessor
Similar to
C++
Preproces
sor...
The purpose of a
web server is
precisely to send
HTML to the
browser!
PHP
Apache/IIS
HTML
What is MySQL?
PHP + MySQL
PHP
Why PHP?
Where to Start?
Note:
Comments in PHP
PHP Variables
PHP Variables
Operators (1)
PHP has all the usual operators as part
of its Syntax:
Arithmetic Operators
Negate, Add, Subtract, Multiply, Divide, Modulus,
Increment (Pre & Post), Decrement (Pre & Post)
Assignment Operators
$a += $b, $a -= $b, $a *= $b,
$a /= $b, $a %= $b
Operators (2)
Logical Operators
And (1), And (2), Or (1), Or (2), Exclusive Or, Not
Comparison Operators
Equal, Identical (Equal and of the same Type), Not Equal
(1), Not Equal (2), Not Identical (Not Equal or not of the
same Type), Less Than, Greater Than, Less Than Or
Equal To, Greater Than Or Equal To
$a == $b,
<> $b, $a
$b, $a <=
Conditional
Statements
Example
<html>
<body>
<?php
$d=date("D");
if ($d=="Fri")
echo "Have a nice weekend!";
else echo "Have a nice day!";
?>
</body>
</html>
Example:
Example
PHP Arrays:
What is an array?
Numeric Arrays
Example 2
In this example we assign the ID key manually:
$names[0] = "Peter";
$names[1] = "Quagmire";
$names[2] = "Joe";
Numeric Arrays
<?php
$names[0] = "Peter";
$names[1] = "Quagmire";
$names[2] = "Joe";
echo $names[1] . " and " . $names[2] .
" are ". $names[0] . "'s neighbors";
?>
The code above will output:
Quagmire and Joe are Peter's neighbors
Associative Arrays
An associative array, each ID key
is associated with a value.
When storing data about specific
named values, a numerical array
is not always the best way to do
it.
With associative arrays we can
use the values as keys and assign
values to them.
Associative Arrays
Example 1
In this example we use an array to assign ages
to the different persons:
$ages = array("Peter"=>32, "Quagmire"=>30,
"Joe"=>34);
Example 2
This example is the same as example 1, but
shows a different way of creating the array:
$ages['Peter'] = "32";
$ages['Quagmire'] = "30";
$ages['Joe'] = "34";
Associative Arrays
PHP Looping
Very often when you write code, you want the same
block of code to run a number of times. You can use
looping statements in your code to perform this.
In PHP we have the following looping statements:
while - loops through a block of code if and as long as a
specified condition is true
do...while - loops through a block of code once, and then
repeats the loop as long as a special condition is true
for - loops through a block of code a specified number of
times
foreach - loops through a block of code for each element
in an array
Example
The following example demonstrates a loop that will continue to
run as long as the variable i is less than, or equal to 5. i will
increase by 1 each time the loop runs:
<html>
<body>
<?php
$i=1;
while($i<=5)
{
echo "The number is " . $i . "<br />";
$i++;
}
?>
</body>
</html>
The do...while
Statement
Example
<html>
<body>
<?php
$i=0;
do
{
$i++;
echo "The number is " . $i . "<br />";
}
while ($i<5);
?>
</body>
</html>
Example
<html>
<body>
<?php
for ($i=1; $i<=5; $i++)
{
echo "Hello World!<br />";
}
?>
</body>
</html>
Example
<html>
<body>
<?php
$arr=array("one", "two", "three");
foreach ($arr as $value)
{
echo "Value: " . $value . "<br />";
}
?>
</body>
</html>