Chapter11 - IntroductionToServerSideDevelopmentwithPHP
Chapter11 - IntroductionToServerSideDevelopmentwithPHP
Server-Side
Development with
PHP
Chapter 11
3 Program
Control 4 Functions
5 Summary
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
Chapter 11
What Is Server-
1 Side
Development?
2 Quick Tour of
PHP
3 Program
Control 4 Functions
5 Summary
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
What Is Server-Side Development?
Comparing Client and Server Scripts
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
What Is Server-Side Development?
Server-Side Script Resources
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
What Is Server-Side Development?
Comparing Server-Side Technologies
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
What Is Server-Side Development?
Comparing Server-Side Technologies
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
What Is Server-Side Development?
Comparing Server-Side Technologies
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
Chapter 11
What Is Server-
1 Side
Development?
2 Quick Tour of
PHP
3 Program
Control 4 Functions
5 Summary
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
Quick Tour of PHP
PHP Tags
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
Quick Tour of PHP
PHP Comments
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
Quick Tour of PHP
Variables, Data Types, and Constants
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
Quick Tour of PHP
Variables, Data Types, and Constants
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
Quick Tour of PHP
Variables, Data Types, and Constants
Escaping Strings
• \n Line feed
• \t Horizontal tab
• \\ Backslash
• \$ Dollar sign
• \” Double quote
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
Quick Tour of PHP
Variables, Data Types, and Constants
• Use define()
• uppercase for constants is a programming
convention
• Then use the word without quotes (or $)
define("DATABASE_LOCAL", "localhost");
echo DATABASE_LOCAL;
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
Quick Tour of PHP
Writing to Output
echo()
echo ("hello");
Strings can easily be appended together using the
concatenate operator, which is the period (.) symbol.
$username = "Ricardo";
echo "Hello". $username; //outputs Hello Ricardo
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
Quick Tour of PHP
More concatenation examples
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
Quick Tour of PHP
printf
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
Chapter 11
What Is Server-
1 Side
Development?
2 Quick Tour of
PHP
3 Program
Control 4 Functions
5 Summary
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
Program Control
if ...else
// if statement
if ( $hourOfDay > 6 && $hourOfDay < 12) {
$greeting = "Good Morning";
}
else if ($hourOfDay == 12) { // optional else if
$greeting = "Good Noon Time";
}
else { // optional else branch
$greeting = "Good Afternoon or Evening";
}
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
Program Control
switch . . . case
switch ($artType) {
case "PT":
$output = "Painting";
break;
case "SC":
$output = "Sculpture";
break;
default:
$output = "Other";
}
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
Program Control
while and do . . . while
$count = 0;
while ($count < 10){
echo $count;
$count++;
}
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
Program Control
while and do . . . while
$count = 0;
do {
echo $count;
// this one increments the count by 2 each
time
$count = $count + 2;
} while ($count < 10);
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
Program Control
for
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
Program Control
Alternate Syntax for Control Structure
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
Program Control
Include Files
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
Program Control
Include Files
include "somefile.php";
include_once "somefile.php";
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
Chapter 11
What Is Server-
1 Side
Development?
2 Quick Tour of
PHP
3 Program
Control 4 Functions
5 Summary
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
Functions
Function Syntax
/**
* This function returns a nicely formatted string using the
* system time.
*/
function getNiceTime(){
return date("H:i:s");
}
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
Functions
Function Syntax – return type declaration
return "hello";
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
Functions
Calling a Function
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
Functions
Parameters
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
Functions
Parameters
function getNiceTime($showSeconds) {
if ($showSeconds==true)
return date("H:i:s");
else
return date("H:i");
}
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
Functions
Parameters – default values
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
Functions
Parameters – Passing Parameters by reference
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
Functions
Parameters – Passing Parameters by reference
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
Functions
Variable Scope within Functions
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
Chapter 11
What Is Server-
1 Side
Development?
2 Quick Tour of
PHP
3 Program
Control 4 Functions
5 Summary
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
Summary
Key Terms
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
Summary
Questions?
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.