Chapter 09
Chapter 09
Murach's PHP and MySQL, C9 © 2010, Mike Murach & Associates, Inc. Slide 1
Objectives
Applied
1. Use any of the functions and techniques that are presented in this
chapter to work with strings.
2. Use any of the functions and techniques that are presented in this
chapter to work with numbers.
Murach's PHP and MySQL, C9 © 2010, Mike Murach & Associates, Inc. Slide 2
Objectives (continued)
Knowledge
1. Describe the way variable substitution is used to assign a string to
a variable.
2. Describe the way PHP escape sequences can be used to insert
special characters into strings and how the htmlentities function
can be used to display special characters correctly in a browser.
3. Describe these terms as they apply to a PHP string: length,
substring, and position.
4. Describe the use of the PHP string functions that return string
lengths or substrings, search for or replace characters in a string,
modify a string, convert between strings and arrays, and compare
two strings.
Murach's PHP and MySQL, C9 © 2010, Mike Murach & Associates, Inc. Slide 3
Objectives (continued)
Knowledge (continued)
5. Describe the PHP is_infinite and is_finite functions, and describe
these PHP constants: PHP_INT_MAX, INF, and -INF.
6. Describe these PHP functions for working with numbers: max,
min, pow, round, sqrt, and mt_rand.
7. Describe the use of the sprintf function for formatting strings and
numbers.
8. Describe the use of type casting and the use of the intval and
floatval functions.
Murach's PHP and MySQL, C9 © 2010, Mike Murach & Associates, Inc. Slide 4
Assign strings with single quotes
$language = 'PHP';
$message = 'Welcome to ' . $language;
Murach's PHP and MySQL, C9 © 2010, Mike Murach & Associates, Inc. Slide 5
Within a heredoc, all new line characters, tabs,
and spaces are included as part of the string
except for the last new line character which
precedes the closing heredoc.
Murach's PHP and MySQL, C9 © 2010, Mike Murach & Associates, Inc. Slide 6
Assign a string with a nowdoc
$message = <<<'MESSAGE'
The nowdoc syntax also allows you to build multi-
line strings in PHP. However, no variable
substitution takes place inside the nowdoc string.
This is similar to single-quoted strings.
MESSAGE;
Murach's PHP and MySQL, C9 © 2010, Mike Murach & Associates, Inc. Slide 7
Escape sequences only used in some strings
\\ Use in all strings except nowdocs
\' Use in single-quoted strings
\" Use in double-quoted strings
Murach's PHP and MySQL, C9 © 2010, Mike Murach & Associates, Inc. Slide 8
Escape sequences with single quotes
$dir = 'C:\\xampp\\php';
$name = 'Mike\'s Music Store';
$quote = "He said, \"It costs \$12.\"";
$comment1 = "This is a\nmulti-line string.";
$comment2 = 'Not a\nmulti-line string.';
Murach's PHP and MySQL, C9 © 2010, Mike Murach & Associates, Inc. Slide 9
The escape sequences for octal and hexadecimal
values let you use any ASCII character in a string.
But, browsers don’t always display some
characters correctly.
The htmlentities function corrects that:
htmlentities($str [, $quotes])
Murach's PHP and MySQL, C9 © 2010, Mike Murach & Associates, Inc. Slide 10
A URL for a list of all PHP string functions
http://www.php.net/manual/en/ref.strings.php
Murach's PHP and MySQL, C9 © 2010, Mike Murach & Associates, Inc. Slide 11
Code that determines if a string is empty
if (empty($first_name)) {
$message = 'You must enter the first name.';
}
Murach's PHP and MySQL, C9 © 2010, Mike Murach & Associates, Inc. Slide 12
Code that formats a phone number in two ways
$phone = '5545556624';
$part1 = substr($phone, 0, 3);
$part2 = substr($phone, 3, 3);
$part3 = substr($phone, 6);
$format_1 = $part1 . '-' . $part2 . '-' . $part3;
$format_2 = '(' . $part1 . ') ' . $part2 . '-' . $part3;
Murach's PHP and MySQL, C9 © 2010, Mike Murach & Associates, Inc. Slide 13
Functions that search a string
strpos($str1, $str2[, $offset])
stripos($str1, $str2[, $offset])
strrpos($str1, $str2[, $offset])
strripos($str1, $str2[, $offset])
Murach's PHP and MySQL, C9 © 2010, Mike Murach & Associates, Inc. Slide 14
Code that splits a string into two substrings
$name = 'Ray Harris';
$i = strpos($name, ' ');
if ($i === false) {
$message = 'No spaces were found in the name.';
} else {
$first_name = substr($name, 0, $i);
$last_name = substr($name, $i+1);
}
Murach's PHP and MySQL, C9 © 2010, Mike Murach & Associates, Inc. Slide 15
Functions that replace part of a string
str_replace($str1, $new, $str2)
str_ireplace($str1, $new, $str2)
Murach's PHP and MySQL, C9 © 2010, Mike Murach & Associates, Inc. Slide 16
Functions that modify strings
ltrim($str)
rtrim($str)
trim($str)
str_pad($str, $len [, $pad[, $type]])
lcfirst($str)
ucfirst($str)
ucwords($str)
strtolower($str)
strtoupper($str)
strrev($str)
str_shuffle($str)
str_repeat($str, $i)
Murach's PHP and MySQL, C9 © 2010, Mike Murach & Associates, Inc. Slide 17
Code that trims and pads a string
$name = ' ray harris ';
$name = ltrim($name);
$name = rtrim($name);
$name = trim($name);
Murach's PHP and MySQL, C9 © 2010, Mike Murach & Associates, Inc. Slide 18
Code that changes the sequence of the characters
$name = strrev($name);
$name = str_shuffle($name);
Murach's PHP and MySQL, C9 © 2010, Mike Murach & Associates, Inc. Slide 19
Functions that convert strings and arrays
explode($sep, $str)
implode($sep, $sa)
Murach's PHP and MySQL, C9 © 2010, Mike Murach & Associates, Inc. Slide 20
Functions that convert between strings
and ASCII integer values
chr($value)
ord($string)
Murach's PHP and MySQL, C9 © 2010, Mike Murach & Associates, Inc. Slide 21
Functions that compare two strings
strcmp($str1, $str2)
strcasecmp($str1, $str2)
strnatcmp($str1, $str2)
strnatcasecmp($str1, $str2)
Murach's PHP and MySQL, C9 © 2010, Mike Murach & Associates, Inc. Slide 22
How to compare two strings
$result = strnatcasecmp($name_1, $name_2);
if ($result < 0) {
echo $name_1 . ' before ' . $name_2;
} else if ($result == 0) {
echo $name_1 . ' matches ' . $name_2;
} else {
echo $name_1 . ' after ' . $name_2;
}
Murach's PHP and MySQL, C9 © 2010, Mike Murach & Associates, Inc. Slide 23
How to assign a decimal value (base 10)
$number_1 = 42;
$number_2 = +72;
$number_3 = -13;
$number_4 = -(-39);
$number_5 = --39; // Error
Murach's PHP and MySQL, C9 © 2010, Mike Murach & Associates, Inc. Slide 24
How to assign an octal value (base 8)
$octal_1 = 0251; // Must begin with 0
$octal_2 = -0262;
Murach's PHP and MySQL, C9 © 2010, Mike Murach & Associates, Inc. Slide 25
How to assign floating-point values
Using normal notation
$float_1 = 3.5; // Must contain a decimal point
$float_2 = -6.0; // May be negative
$float_3 = .125; // Same as 0.125
$float_4 = 1.; // Same as 1.0
Murach's PHP and MySQL, C9 © 2010, Mike Murach & Associates, Inc. Slide 26
Two functions for working with infinity
is_infinite($value)
is_finite($value)
Murach's PHP and MySQL, C9 © 2010, Mike Murach & Associates, Inc. Slide 27
URL for a list of all PHP math functions
http://www.php.net/manual/en/ref.math.php
Murach's PHP and MySQL, C9 © 2010, Mike Murach & Associates, Inc. Slide 28
How to round a number
$subtotal = 15.99;
$tax_rate = 0.08;
$tax = round($subtotal * $tax_rate, 2);
Murach's PHP and MySQL, C9 © 2010, Mike Murach & Associates, Inc. Slide 29
How to calculate the distance between two points
$x1 = 5; $y1 = 4;
$x2 = 2; $y2 = 8;
$distance = sqrt(pow($x1 - $x2, 2) + pow($y1 - $y2, 2));
Murach's PHP and MySQL, C9 © 2010, Mike Murach & Associates, Inc. Slide 30
Functions that generate random numbers
getrandmax()
rand()
rand($lo, $hi)
mt_getrandmax()
mt_rand()
mt_rand($lo, $hi)
Murach's PHP and MySQL, C9 © 2010, Mike Murach & Associates, Inc. Slide 31
How to simulate a random dice roll
$dice = mt_rand(1, 6);
Murach's PHP and MySQL, C9 © 2010, Mike Murach & Associates, Inc. Slide 32
How to generate a random password
$password_length = 8;
$password = str_shuffle($password);
echo $password;
Murach's PHP and MySQL, C9 © 2010, Mike Murach & Associates, Inc. Slide 33
The sprintf function
sprintf($format, $val1[, val2 ...])
Murach's PHP and MySQL, C9 © 2010, Mike Murach & Associates, Inc. Slide 34
A sprintf function that formats two values
$message = sprintf('The book about %s has %d pages.',
'PHP', 800);
Murach's PHP and MySQL, C9 © 2010, Mike Murach & Associates, Inc. Slide 35
Two functions for converting strings to numbers
intval($var)
floatval($var)
Murach's PHP and MySQL, C9 © 2010, Mike Murach & Associates, Inc. Slide 36
How to convert a string to an integer
Using type casting
$value_1 = (int) '42';
$value_2 = (int) '42.5';
$value_3 = (int) '42 miles';
$value_4 = (int) '2,500 feet';
$value_5 = (int) 'miles: 42';
$value_6 = (int) 'miles';
$value_7 = (int) '10000000000';
$value_8 = (int) '042';
$value_9 = (int) '0x42';
Murach's PHP and MySQL, C9 © 2010, Mike Murach & Associates, Inc. Slide 37
How to convert a string to a floating-point number
Using type casting
$value_1 = (float) '4.2';
$value_2 = (float) '4.2 gallons';
$value_3 = (float) 'gallons';
$value_4 = (float) '1.5e-3';
$value_5 = (float) '1e400';
Murach's PHP and MySQL, C9 © 2010, Mike Murach & Associates, Inc. Slide 38