COMP284 Scripting Languages – Handouts (8 on 1)
COMP284 Scripting Languages
Lecture 10: PHP (Part 2)
Handouts (8 on 1)
Ullrich Hustadt
Department of Computer Science
School of Electrical Engineering, Electronics, and Computer Science
University of Liverpool
Contents
1 Scalar types
Integers and Floating-point numbers
Exceptions and error handling
Booleans
Strings
2 Compound types
Arrays
Foreach-loops
Array functions
3 Printing
COMP284 Scripting Languages Lecture 10 Slide L10 – 1
Scalar types Integers and Floating-point numbers
Integers and Floating-point numbers
• PHP distinguishes between
• integer numbers 0 2012 -40 1263978
• floating-point numbers 1.25 256.0 -12e19 2.4e-10
• PHP supports a wide range of pre-defined mathematical functions
abs(number) absolute value
ceil(number) round fractions up
floor(number) round fractions down
round(number [,prec,mode]) round fractions
log(number [,base]) logarithm
rand(min,max) generate an integer random number
sqrt(number) square root
• PHP provides a range of pre-defined number constants including
M_PI 3.14159265358979323846
NAN ‘not a number’
INF ‘infinity’
COMP284 Scripting Languages Lecture 10 Slide L10 – 2
Scalar types Integers and Floating-point numbers
Integers and Floating-point numbers: NAN and INF
The constants NAN and INF are used as return values for some applications
of mathematical functions that do not return a number
• log(0) returns -INF (negative ‘infinity’)
• sqrt(-1) returns NAN (‘not a number’)
In contrast
• 1/0 returns FALSE and produces an error message
• 0/0 returns FALSE and produces an error message
and execution of the script continues!
In PHP 7
• 1/0 returns INF and produces an error message
• 0/0 returns NAN and produces an error message
and execution of the script continues!
COMP284 Scripting Languages Lecture 10 Slide L10 – 3
Scalar types Integers and Floating-point numbers
Integers and Floating-point numbers: NAN and INF
NAN and INF can be compared with each other and other numbers using
equality and comparison operators:
NAN == NAN ; FALSE NAN === NAN ; FALSE NAN == 1 ; FALSE
INF == INF ; FALSE INF === INF ; TRUE INF == 1 ; FALSE
NAN < NAN ; TRUE INF < INF ; TRUE 1 < INF ; TRUE
NAN < INF ; TRUE INF < NAN ; TRUE INF < 1 ; FALSE
NAN < 1 ; TRUE 1 < NAN ; TRUE
In PHP 5.3 and earlier versions, INF == INF returns FALSE
In PHP 5.4 and later versions, INF == INF returns TRUE
COMP284 Scripting Languages Lecture 10 Slide L10 – 4
Scalar types Integers and Floating-point numbers
Integers and Floating-point numbers: NAN and INF
• PHP provides three functions to test whether a value is or is not NAN,
INF or -INF:
• bool is_nan(value)
returns TRUE iff value is NAN
• bool is_infinite(value)
returns TRUE iff value is INF or -INF
• bool is_finite(value)
returns TRUE iff value is neither NAN nor INF/-INF
• In conversion to a boolean value,
both NAN and INF are converted to TRUE
• In conversion to a string,
NAN converts to ’NAN’ and INF converts to ’INF’
COMP284 Scripting Languages Lecture 10 Slide L10 – 5
Scalar types Exceptions and error handling
Exceptions and error handling
PHP distinguishes between exceptions and errors
• A possible way to perform exception handling in PHP is as follows:
try { ... run code here ... // try
} catch (Exception $e) {
... handle the exception here using $e // catch
}
• Errors must be dealt with by an error handling function
(‘Division by zero’ produces an error not an exception)
One possible approach is to let the error handling function
turn errors into exceptions
function exception_error_handler($errno , $errstr ,
$errfile , $errline ) {
throw new ErrorException($errstr , $errno ,
0, $errfile , $errline ); }
set_error_handler("exception_error_handler");
http://www.php.net/manual/en/class.errorexception.php
COMP284 Scripting Languages Lecture 10 Slide L10 – 6
Scalar types Booleans
Booleans
• Unlike Perl, PHP does have a boolean datatype
with constants TRUE and FALSE (case insensitive)
• PHP offers the same short-circuit boolean operators as Java and Perl:
&& (conjunction) || (disjunction) ! (negation)
• Alternatively, and and or can be used instead of && and ||, respectively
• However, not is not a PHP operator
• The truth tables for these operators are the same as for Perl
• Remember that && and || are not commutative, that is,
(A && B) is not the same as (B && A)
(A || B) is not the same as (B || A)
COMP284 Scripting Languages Lecture 10 Slide L10 – 7
http://www.php.net/manual/en/class.errorexception.php
Scalar types Booleans
Type conversion to boolean
When converting to boolean, the following values are considered FALSE:
• the boolean FALSE itself
• the integer 0 (zero)
• the float 0.0 (zero)
• the empty string, and the string ’0’
• an array with zero elements
• an object with zero member variables (PHP 4 only)
• the special type NULL (including unset variables)
• SimpleXML objects created from empty tags
Every other value is considered TRUE (including any resource)
COMP284 Scripting Languages Lecture 10 Slide L10 – 8
Scalar types Strings
Strings
• PHP supports both single-quoted and double-quoted strings
• PHP also supports heredocs as a means to specify multi-line strings
The only difference to Perl is the use of <<< instead of << in their
definition:
<<
print <<