|
Network Programming ![]() 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. |
Lecture /
Introduction to PHP ProgrammingA Network Programming Lecture by Steven Choy Overview: The Environment - Install XAMPP as a Web server in your PC - Test a simple PHP program running in your Web server - PHP Syntax - PHP variables - PHP operators - PHP's arrays - PHP's control statements - Define and call functions - Some PHP functions
The Environment
Activity 1: Install XAMPP as a Web server in your PC
Note: Another popular choice of software package for setting up Web server in Windows is WampServer. You are encouraged to try out both XAMPP and WamServer to see which one is suitable for you. 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>
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', 'pepperoni');
$fruits = array('red' => 'apple', 'yellow' => 'banana', 'purple' => 'plum', 'green' => 'grape');
$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', 'pepperoni');
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);
Some PHP functions
$numberedString = "1234567890";
$fivePos = strpos($numberedString, "5");
echo "The position of 5 in our string was $fivePos";
$rawstring = "Welcome Birmingham parents. Your replaceme is a pleasure to have!";
$malestr = str_replace("replaceme", "son", $rawstring);
$femalestr = str_replace("replaceme", "daughter", $rawstring);
echo "Son: ". $malestr . "<br />";
echo "Daughter: ". $femalestr;
$rawPhoneNumber = "800-555-5555";
$phoneChunks = explode("-", $rawPhoneNumber);
//Result: $phoneChunks[0] is 800, $phoneChunks[1] is 555, etc
$pieces = array("Hello", "World,", "I", "am", "Here!");
$gluedTogetherSpaces = implode(" ", $pieces);
$gluedTogetherDashes = implode("-", $pieces);
echo date();
echo date("m/d/y");
$tomorrow = mktime(0, 0, 0, date("m"), date("d")+1, date("y"));
echo "Tomorrow is ".date("m/d/y", $tomorrow);
Class Exercise
References:Thanks for ReadingIf you would rather like to have this lecture note in printed format, please click the print action link in the top right corner. If you find any problem in this lecture note, please feel free to tell Steven via steven@findaway.hk. |