Introduction to
Server-Side
Development with
PHP
Chapter 11
Randy Connolly and Ricardo Hoar Fundamentals of Web Development
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.
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
• ASP (Active Server Pages) / ASP.NET
• JSP (Java Server Pages)
• Node.js
• Perl
• PHP
• Python
• Ruby on Rails
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
<?php tag and a matching closing ?>
Inside is code to execute, outside is HTML to echo
directly
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
Quick Tour of PHP
PHP Comments
• Single-line comments. Lines that begin with a #
• Multiline (block) comments. begin with a /* and
encompass everything that is encountered until a
closing */
• End-of-line comments. // to end of line
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
Quick Tour of PHP
Variables, Data Types, and Constants
• Variables in PHP are dynamically typed
• To declare a variable you must preface the variable
name with the dollar ($) symbol.
• Whenever you use that variable, you must also
include the $ symbol with it.
• Right to left assignment
$count = 42;
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
Quick Tour of PHP
Variables, Data Types, and Constants
• Boolean A logical true or false value
• Integer Whole numbers
• Float Decimal numbers
• String Letters
• Array A collection of data of any type (covered in
the next chapter)
• Object Instances of classes
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
for ($count=0; $count < 100; $count+=5)
{
echo $count;
}
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
Program Control
Alternate Syntax for Control Structure
<?php if ($userStatus == "loggedin") : ?>
<a href="account.php">Account</a>
<a href="logout.php">Logout</a>
<?php else : ?>
<a href="login.php">Login</a>
<a href="register.php">Register</a>
<?php endif; ?>
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
A Return Type Declaration explicitly defines a function’s return type
by adding a colon and the return type after the parameter list when
defining a function
function mustReturnString() : string {
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
Parameters are the mechanism by which values are
passed into functions
• To define a function with parameters, you must
decide
• how many parameters you want to pass in, and
• in what order they will be passed.
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");
}
echo getNiceTime(true); // this will print seconds
echo getNiceTime(false); // will not print seconds.
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
Functions
Parameters – default values
In PHP you can set parameter default values for any
parameter in a function. However, once you start
having default values, all subsequent parameters must
also have defaults.
function getNiceTime($showSeconds=true) {
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 – Passing Parameters by reference
By default, arguments passed to functions are passed
by value in PHP.
PHP also allows arguments to functions to be passed
by reference , which will allow a function to change
the contents of a passed variable
The mechanism in PHP to specify that a parameter is
passed by reference is to add an ampersand (&)
symbol next to the parameter name in the function
declaration
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
All variables defined within a function have function
scope , meaning that they are only accessible within
the function.
While variables defined in the main script are said to
have global scope , these global variables are not by
default, available within functions.
PHP does allow variables with global scope to be
accessed within a function using the global keyword,
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
ASP /ASP.NET global scope PHP core
built-in function handlers preforked
Common Gateway Java Server Pages (JSP) process
Interface (CGI) loosely typed Python
constant module Return-type
daemon multi-process declarations
data storage multi-threaded Ruby On Rails
data types opcodes SAPI
database overloading server-side includes
database management parameters (SSI)
system (DBMS) parameter default thread
dynamically typed values user-defined function
extension layer passed by reference virtual machine
fork passed by value web services
function Perl worker
function scope PHP Zend Engine
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.
Summary
Questions?
Randy Connolly and Ricardo Hoar Fundamentals of Web Development - 2nd Ed.