2. Syntax
PHP’s
syntax derived from many languages
More
Java-like with the latest objectoriented additions
Designed
CPTR304: Internet Authoring
primarily as a text processor
2
3. PHP Tags
Standard Tags
<? php
... code
?>
Short Tags
<?
... code
?>
<?=$variable ?>
Script Tags
<script language=“php”
... code
</script>
ASP Tags
<%
... code
%>
CPTR304: Internet Authoring
Short tags, script tags and ASP tags are all considered deprecated.
3
4. Comments
// Single line comment
# Single line comment
/* Multi-line
comment
*/
/**
* API Documentation Example
*
*
*/
function foo($bar) { }
CPTR304: Internet Authoring
4
5. Whitespaces
You can’t have any whitespaces between <? and
php
You cannot break apart keywords
You cannot break apart variable names and
function names
CPTR304: Internet Authoring
5
7. Data Types
boolean
true or false
int
signed numeric integer value
float
signed floating-point value
string
collection of binary data
CPTR304: Internet Authoring
7
8. Numeric Values
Decimal
10; -11; 1452
Standard decimal notation
Octal
0666, 0100
Octal notation – identified by its
leading zero and used to mainly
express UNIX-style access
permissions
Hexadecimal
0x123; 0XFF; -0x100
Base-16 notation
CPTR304: Internet Authoring
8
9. Numeric Values, cont’d
Decimal
0.12; 1234.43; -.123
Traditional
Exponential
2E7, 1.2e2
Exponential notation – a set of
significant digits (mantissa),
followed by the case-insensitive
letter E and by an exponent.
CPTR304: Internet Authoring
9
10. Strings
Equivalent
to text (according to many
programmers)
Actually,
an ordered collection of binary
data
CPTR304: Internet Authoring
10
11. Booleans
Used as the basis for logical operations
Boolean conversion has special rules:
A
number converted to Boolean becomes false if the
original number is zero, and true otherwise
A
string is converted to false only if it is empty or if
it contains the single character 0
When
converted to a number or string, a Boolean
becomes 1 if true, and 0 otherwise
CPTR304: Internet Authoring
11
12. Compound Data Types
Arrays are containers of ordered data elements;
Objects are containers of both data and code.
CPTR304: Internet Authoring
12
13. Other Data Types
NULL
– variable has no value
– used to indicate external
resources that are not used natively by PHP
resource
CPTR304: Internet Authoring
13
15. Variables
A variable can contain any type of data
PHP is loosely typed as opposed to being strongly typed
like C, C++, and Java
Identified by a dollar sign $, followed by an identifier
name
$name = „valid‟; //valid identifier
$_name = „valid‟; //valid identifier
$1name = „invalid‟; //invalid identifier, starts
with a number
CPTR304: Internet Authoring
15
16. Variable Variables
A variable whose name is contained in another variable
$name = „foo‟;
$$name = „bar‟;
echo $foo; //Displays „bar‟
CPTR304: Internet Authoring
16
17. Variable Variables, cont’d
$name = „123‟;
/* 123 is your variable name, this would normally
be invalid. */
$$name = „456‟;
// Again, you assign a value
echo ${„123‟};
//Finally, using curly braces you can output „456‟
CPTR304: Internet Authoring
17
28. Comparisons
==
Equivalence – evaluates to true if both operands have the
same value but not necessarily the same type.
===
Identity – evaluates to true only if the operands are of the
same type and contain the same value.
!=
Not-equivalent – evaluates to true if the operands are not
equivalent, without regards to their type.
!==
Not-identical – evaluates to true if the operands are not of
the same type or contain the same value.
CPTR304: Internet Authoring
28
34. Switch
$a = 0;
switch ($a) {
case true:
break;
case 0:
break;
default:
break;
}
CPTR304: Internet Authoring
34
35. while()
$i = 0;
while ($i < 10) {
echo $i . PHP_EOL;
$i++;
}
CPTR304: Internet Authoring
35
36. do…while()
$i = 0;
do {
echo $i . PHP_EOL;
$i++;
} while ($i < 10)
CPTR304: Internet Authoring
36
37. for()
for ($i = 0; $i < 10; $i++) {
echo $i . PHP_EOL;
}
CPTR304: Internet Authoring
37
38. Errors and Error Management
Compile-time errors Detected by parser while compiling a script.
Cannot be trapped within the script itself.
Fatal errors
Halt the execution of a script. Cannot be trapped.
Recoverable errors
Represent significant failures but can be handled
in a safe way.
Warnings
Recoverable errors that indicate a run-time fault.
Does not halt execution.
Notices
Indicates that an error condition has occurred but
is not necessarily significant. Does not halt script
execution.
38
CPTR304: Internet Authoring
PHP was built with the intent of providing simplicity and choicePHP derived predominantly from C with influence from PerlPHP code can be inserted directly into a text file using a special set of tags
Standard tags – de-facto opening and closing tags; best solution for portability and backward compatibilityShort tags – conflicts with XML headersScript tags – allowed HTML editors, unable to cope with PHP tags, to ignore the PHP code
A series of statements enclosed between two bracesCode blocks can be nested
Two categories: scalar and compositeScalar contains only one value at a time
PHP recognizes two types of numbers, integers and floating-point valuesOctal numbers can be easily confused with decimal numbers
Floating-point numbers, also called floats and, sometimes, doublesExponent: the resulting number is expressed multiplied by 10 to the power of the exponent – e.g. 1e2 = 10064-bit platforms may be capable of representing a wider range of integer numbers than 32-bit
String could also be the contents of an image file, a spreadsheet, or even a music recording
PHP supports two compound data types; they are essentially containers of other data
NB: A value cannot be converted to some special types e.g. you cannot convert any value to a resource – you can, however, convert a resource to a numeric or string data type, in which case PHP will return the numeric ID of the resource
Loosely typed: type of variable implicitly changed as neededStrongly typed: variables can only contain one type of data
$$name – special syntax used to indicate to the interpreter to use the contents of $name to reference a new variable
Use with extreme careThey make code difficult to understand and documentImproper use can lead to significant security issues
Variables can be used to hold function names
A call to isset() will return true if a variable exists or has a value other than NULL
Causes PHP to ignore almost all error messages that occur while that expression is being evaluated