class03_php_conditional_statements

Class06
Conditional Statements
MIS 3501, Fall 2015
Jeremy Shafer
Department of MIS
Fox School of Business
Temple University
9/10/2015
© 2014, Mike Murach & Associates, Inc.
1
Course Overview
You
are
here.
MySQL
To do:
•Weeks 5 & 6
PDO
•Week 7
PHP
•Weeks 3 & 4





Organize your code with MVC
(week 8)
Debug your code (week 9)
Work with forms (week 10)
Use arrays (week 11 & 12)
Use sessions (week 13)
HTML &
CSS
•Weeks 1 & 2
2
Variables
Variables are named, containers, that hold data.
© 2014, Mike Murach & Associates, Inc.
3
Data Types
The six PHP data types
 integer
 double
 boolean
For starters, we’ll just think
about these four
 string
 array
 object
© 2014, Mike Murach & Associates, Inc.
Slide 4
Integer values (whole numbers)
15
-21
// an integer
// a negative integer
Double values (numbers with decimal positions)
21.5
-124.82
// a floating-point value
// a negative floating-point value
The two Boolean values
true
false
// equivalent to true, yes, or on
// equivalent to false, no, off, or 0
String values
'Ray Harris'
"Ray Harris"
''
null
//
//
//
//
a string with single quotes
a string with double quotes
an empty string
a NULL value
© 2014, Mike Murach & Associates, Inc.
Slide 5
Using the assignment operator (=)
as you declare a variable and give it a value
$count = 10;
$list_price = 9.50;
$first_name = 'Bob';
$first_name = "Bob";
$is_valid = false;
//
//
//
//
//
an integer literal
a double literal
a string literal
a string literal
a Boolean literal
$product_count = $count;
$price = $list_price;
$name = $first_name;
$is_new = $is_valid;
//
//
//
//
$product_count is 10
$price is 9.50
$name is "Bob"
$is_new is FALSE
© 2014, Mike Murach & Associates, Inc.
Slide 6
How assign string expressions
Use single quotes to improve PHP efficiency
$first_name = 'Bob';
$last_name = 'Roberts';
Assign NULL values and empty strings
$address2 = '';
$address2 = null;
// an empty string
// a NULL value
Use double quotes for variable substitution
$name = "Name: $first_name";
$name = "$first_name $last_name";
// Name: Bob
// Bob Roberts
Mix single and double quotes for special purposes
$last_name = "O'Brien";
$line = 'She said, "Hi."';
© 2014, Mike Murach & Associates, Inc.
// O'Brien
// She said, "Hi."
Slide 7
How to use the concatenation operator (.)
How to use the concatenation operator for simple joins
$first_name = 'Bob';
$last_name = 'Roberts';
$name = 'Name: ' . $first_name;
$name = $first_name . ' ' . $last_name;
// Name: Bob
// Bob Roberts
How to join a number to a string
$price = 19.99;
$price_string = 'Price: ' . $price;
© 2014, Mike Murach & Associates, Inc.
// Price: 19.99
Slide 8
Making Comparisons
The relational operators
Operator
Example
==
$last_name == "Harris"
$test_score == 10
!=
$first_name != "Ray"
$months != 0
<
$age < 18
<=
$investment <= 0
>
$test_score > 100
>=
$rate / 100 >= 0.1
The logical operators in order of precedence
Operator
Example
!
!is_numeric($age)
&&
$age > 17 && $score < 70
||
!is_numeric($rate) || $rate < 0
© 2014, Mike Murach & Associates, Inc.
Slide 9
Conditional Statements (simple)
An if statement with no other clauses
if ( $price <= 0 ) {
$message = 'Price must be greater than zero.';
}
An if statement with an else clause
if ( empty($first_name) ) {
$message = 'You must enter your first name.';
} else {
$message = 'Hello ' . $first_name.'!';
}
© 2014, Mike Murach & Associates, Inc.
Slide 10
Functions
A function for formatting numbers
number_format($number[, $decimals])
Statements that format numbers
$nf
$nf
$nf
$nf
=
=
=
=
number_format(12345);
number_format(12345, 2);
number_format(12345.674, 2);
number_format(12345.675, 2);
© 2014, Mike Murach & Associates, Inc.
//
//
//
//
12,345
12,345.00
12,345.67
12,345.68
Slide 11
Four functions for checking variable values
isset($var)
empty($var)
is_numeric($var)
filter_input($type,$var,$filter)
Discuss –
Based on your
reading of the
Function calls that check variable values textbook,
isset($name)
// TRUE if $name has been set what is the
// and is not NULL
difference
empty($name)
// TRUE if $name is empty
is_numeric($price)
// TRUE if $price is a number between
isset() and
filter_input(INPUT_POST, $price, FILTER_VALIDATE_FLOAT)
empty()?
//Returns the value of the variable on success, FALSE on failure, or
NULL if the second parameter is a variable that is notWhat
set. do they
do for us?
© 2014, Mike Murach & Associates, Inc.
Slide 12
Four functions for checking variable values
isset($var)
empty($var)
is_numeric($var)
filter_input($type,$var,$filter)
Very
Useful!
Function calls that check variable values
isset($name)
empty($name)
is_numeric($price)
//
//
//
//
TRUE if $name has been set
and is not NULL
TRUE if $name is empty
TRUE if $price is a number
filter_input(INPUT_POST, $price, FILTER_VALIDATE_FLOAT)
//Returns the value of the variable on success, FALSE on failure, or
NULL if the second parameter is a variable that is not set.
© 2014, Mike Murach & Associates, Inc.
Slide 13
<?php
//get data off the form
$description = filter_input(INPUT_POST,'product_description');
$price = filter_input(INPUT_POST, 'list_price',FILTER_VALIDATE_FLOAT);
$discount = filter_input(INPUT_POST, 'discount_percent',FILTER_VALIDATE_INT);
?>
Could be INPUT_POST
or INPUT_GET
© 2014, Mike Murach & Associates, Inc.
Slide 14
<?php
//get data off the form
$description = filter_input(INPUT_POST,'product_description');
$price = filter_input(INPUT_POST, 'list_price',FILTER_VALIDATE_FLOAT);
$discount = filter_input(INPUT_POST, 'discount_percent',FILTER_VALIDATE_INT);
?>
Corresponds to the name
of a form input tag
© 2014, Mike Murach & Associates, Inc.
Slide 15
<?php
//get data off the form
$description = filter_input(INPUT_POST,'product_description');
$price = filter_input(INPUT_POST, 'list_price',FILTER_VALIDATE_FLOAT);
$discount = filter_input(INPUT_POST, 'discount_percent',FILTER_VALIDATE_INT);
?>
Specifies the type of
validation.
See PHP 5 Predefined Filter Constants here:
http://www.w3schools.com/php/php_ref_filter.asp
© 2014, Mike Murach & Associates, Inc.
Slide 16
<?php
//get data off the form
$description = filter_input(INPUT_POST,'product_description');
$price = filter_input(INPUT_POST, 'list_price',FILTER_VALIDATE_FLOAT);
$discount = filter_input(INPUT_POST, 'discount_percent',FILTER_VALIDATE_INT);
?>
The value of $discount will be either:
1. The value of ‘discount_percent’ provided by the user
2. FALSE (because the value was not an integer)
3. NULL (if there was no ‘discount_percent’ in the form
post at all.)
© 2014, Mike Murach & Associates, Inc.
Slide 17
Built-in functions that pass control

include($path)

include_once($path)

require($path)

require_once($path)

exit([$status])

die([$status])
© 2014, Mike Murach & Associates, Inc.
Slide 18
The include function
include('index.php');
// index.php in the current
// directory
The require function
require('index.php');
// index.php in the current
// directory
The exit function
exit;
// parentheses are optional
exit();
exit('Unable to connect to DB.');
// passes a message to the browser
© 2014, Mike Murach & Associates, Inc.
Slide 19
The include function
include('index.php');
// index.php in the current
// directory
The require function
require('index.php');
// index.php in the current
// directory
The exit function
exit;
// parentheses are optional
exit();
exit('Unable to connect to DB.');
// passes a message to the browser
© 2014, Mike Murach & Associates, Inc.
Slide 20
How to pass control to another PHP file
in the current directory
if ($is_valid) {
include('process_data.php');
exit();
}
© 2014, Mike Murach & Associates, Inc.
Slide 21
Conditional Statements (fancy!)
An if statement with else if and else clauses
if ( empty($investment) ) {
$message = 'Investment is a required field.';
} else if ( !is_numeric($investment) ) {
$message = 'Investment must be a valid number.';
} else if ( $investment <= 0 ) {
$message = 'Investment must be greater than zero.';
} else {
$message = 'Investment is valid!';
}
© 2014, Mike Murach & Associates, Inc.
Slide 22
A compound conditional expression
if ( empty($investment) || !is_numeric($investment) ||
$investment <= 0 ) {
$message = 'Investment must be a valid number greater than
zero.';
}
A nested if statement
if ( empty($months) || !is_numeric($months)
|| $months <= 0 ) {
$message = 'Please enter a number of months > zero.';
} else {
$years = $months / 12;
if ( $years > 1 ) {
$message = 'A long-term investment.';
} else {
$message = 'A short-term investment.';
}
}
© 2014, Mike Murach & Associates, Inc.
Slide 23
Exercise
© 2014, Mike Murach & Associates, Inc.
Slide 24