Introduction to PHP.
Syntactic Characteristics.
PHP has:
-
four scalar types:
-
Boolean
-
Integer
-
Double
-
String
-
Two compound types
-
Array
-
Object
-
Two special types
-
Resource
-
NULL
-
Variables
-
The type of a variable is set every time it is assigned a value
-
An unassigned variable (unbound variable) has the value of NULL
-
Unbound variables appearing on an expression have their value coerced
-
To 0 if the context specifies an integer
-
To the empty string if the context specifies a string
-
The function IsSet takes a variable's name as its parameter and returns a Boolean
-
TRUE if the variable has a non-NULL value
-
FALSE otherwise
-
The function unSet sets a variable back to the unassigned state
-
error_reporting(15);
Changes the error reporting level of the PHP interpreter
so the user can be informed when an unbound variable is referenced
The default level is 7
-
Integer Type
PHP has a single integer type integer
The size of a word size on the machine
-
Double Type
Double literals can have a decimal point or an exponent or both
-
String Type
-
Characters are single bytes -- there is not character type
Single characters are represented by strings of length one
-
String literals just like Perl's are delimited by single (') or double quotes (")
-
Double quoted literals can include embedded newline characters created by the Return key of the keyboard
-
Boolean Type
-
An integer expression used in Boolean context evaluates to
-
FALSE if the expression value is 0,
-
TRUE if the expression value is NOT 0
-
A string expression used in a Boolean context evaluates to
-
FALSE if the expression value is "0", or the empty string
-
TRUE otherwise
-
The string "0.0" evaluates to TRUE
-
Arithmetic Operators/Expressions
-
Arithmetic Operators: +, -, *, /, %, ++, and --
-
+, -, * produce a double(integer) for double (integer) operands
-
If the result of integer division is not an integer, a double is returned
-
The operands of the modulus operator % are coerced to integers if they are not.
-
Large number of predefined functions to operate on numeric values
-
String Operations
-
Scalar Type conversions
-
Implicit type conversions are coercions
-
Numeric values in a string context are coerced to a string
-
String values appearing in a numeric context are coerced to numeric values
-
If the string contains only digits, it is converted to an integer
-
If the string contains only digits and a period or an e/E, it is converted to an double
-
If the string does not begin period, a sign or a digit, it is converted to zero
-
When a double is converted to an integer the fractional part is dropped
-
Explicit type conversions:
-
An expression can be cast to a different type
$sum = 4.777;
$res = (int)$sum; # value of $res is now 4
-
Use type conversion functions: intval, doubleval, or strval
$res = intval(4sum); # value of $res is now 4
-
Use the settype functions which takes two parameters: a variable and a string specifying type name
settype($sum, "integer"); # value of $sum is now 4
-
The type of a variable can be determined
-
The gettype function takes on the variable's name as a parameter
Returns a string with the name of the type of the variable
It may return "unknown"
-
The testing type functions:is_integer, is_double, is_string, is_bool, etc.
-
Assignment operators
just like C and Perl
| Origins and Uses of PHP |
| Overview of PHP |
| General Syntactic Characteristics |
| Primitives, Operations, and Expressions |
| Output |
| Control Statements |
| Arrays |
| Functions |
| Pattern Matching |
| Form Handling |
| Files |
| Cookies |
| Session Tracking |