|
Web Design ![]() This website demonstrates using wikis as teaching and learning tool. The course instructor is happy to share the teaching materials here with those who find it readable. |
Tutorial /
Server-side Processing: Introduction to PHP ProgrammingOverview: This tutorial introduces PHP programming as the basic of server-side processing for Web development. ![]() The Environment
![]() Activity 1: Install XAMPP as a Web server in your PC
Activity 2: Test a simple PHP program running in your Web server
<html>
<head></head>
<body>
Agent: So who do you think you are, anyhow?
<br />
<?php
// print output
echo 'Neo: I am Neo, but my people call me The One.';
?>
</body>
</html>
Q1) What your Web browser will receive from the server-side program?
Q2) Can we use a Web browser to get the source code of the server-side program?
PHP Syntax
<?php
... PHP code ...
?>
<html>
<head>
<title>My First PHP Page</title>
</head>
<body>
<?php
echo "Hello World! ";
echo "Hello World! ";
echo "Hello World! ";
echo "Hello World! "; echo "Hello World! ";
?>
</body>
</html>
PHP variables
(Question: What is the major difference when it compares to other programming language you have learned?)
PHP operators
$my_var = 4;
$another_var = $my_var;
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus
== Equal To
!= Not Equal To
< Less Than
> Greater Than
<= Less Than or Equal To
>= Greater Than or Equal To
$a = 'This is';
$b = 'funny';
$c = $a.' '.$b;
PHP's arrays
$pizzaToppings = array('onion', 'tomato', 'cheese', 'anchovies', 'ham');
$fruits = array('red' => 'apple', 'yellow' => 'banana', 'purple' => 'plum');
$pasta[0] = 'spaghetti';
$pasta[1] = 'penne';
$pasta[2] = 'macaroni';
$menu['breakfast'] = 'bacon and eggs';
$menu['lunch'] = 'roast beef';
$menu['dinner'] = 'lasagna';
PHP's control statements
$name = "steven";
if ( $name == "steven" ) {
echo "Your name is Steven!<br />";
}
echo "Welcome to my homepage!";
if ( $number >= 3 ) {
echo "The if statement evaluated to true";
} else {
echo "The if statement evaluated to false";
}
for ( initialize a counter; conditional statement; increment a counter){
// do this code;
}
// Example
for ( $counter = 10; $counter <= 100; $counter += 10) {
// do something based on the counter;
}
$pizzaToppings = array('onion', 'tomato', 'cheese', 'anchovies', 'ham');
echo '<ol>';
foreach ($pizzaToppings as $topping) {
echo '<li>'.$topping.'</li>';
}
echo '</ol>';
Define and call functions
// define a function
function getCircumference($radius) {
$result = $radius*2*3.14;
return $result;
}
// call a function with an argument
$the_radius = 10;
$the_circumference = getCircumference($the_radius);
Activity 3: PHP For loop
<table border="1" cellpadding="5">
<tr><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td></tr>
<tr><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td></tr>
<tr><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td></tr>
<tr><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td></tr>
<tr><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td></tr>
<tr><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td></tr>
<tr><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td></tr>
<tr><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td></tr>
<tr><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td><td>X</td></tr>
</table>
Q3) Submit your PHP program for this activity.
Activity 4: PHP Control Statements
Hint 1: The following PHP code is useful for you; it outputs all the prime numbers between 1 and 100.
<?php
for ($i = 1; $i <= 100; $i++)
{
if($i % 2 != 1) continue;
$d = 3;
$x = sqrt($i);
while ($i % $d != 0 && $d < $x) $d += 2;
if((($i % $d == 0 && $i != $d) * 1) == 0) echo $i.' ';
}
?>
Hint 2: You can use the HTML tag <strong> and </strong> to bold a number.
Q4) Submit your PHP program for this activity.
References:Submission
|