Some Information about PHP
PHP is just one of many server-side scripting languages, but one that is widely supported, widely used and an excellent choice for prepocessing documents on the server before they are sent to the browser. PHP is particularly adept at interfacing with a variety of databases.
See these sample files for illustrations of many of the concepts summarized here, and more.
PHP was developed by Rasmus Lerdorf of the Apache Group. The acronym originally stood for "Personal Home Page", but now it's "PHP: Hypertext Preprocessor".
Here's the PHP timeline:
1995 1997 1998 2000 2004 our version (2011) ?? 1.0 2.0 3.0 4.0 5.0 5.3.2 6.0
.php
extension, though in certain situations this can also
be .php3
, or .phtml
, or something else.include("file.ext");
which reports a warning
and continues if there is a problem.require("file.ext");
which reports an error
and stops execution if there is problem.<?php ...
?>
(They may be available, but it's not a good idea to use
these "short form" delimiters: <? ... ?>
)PHP has a function that can be called to set the error-reporting "level" for a script:
error_reporting(level_number);
The default value of level_number is 7. A value of 15 will cause unbound variables to be reported, which might be a useful thing to have in effect.
Here is a table of the four scalar types in PHP (note that string is regarded as a scalar type):
integer | like long in C++ or Java |
double | like double in C++ or Java |
boolean | true , false |
string | a sequence of characters enclosed in single or double quotes |
0
is false
; any non-zero
number is true
.0.0
is false
, and is the only
false
double value.""
and "0"
(''
and '0'
) are false
, but any other string is
true, including "0.0"
('0.0'
).""
variables are
interpolated and escape sequences are recognized, but within
''
they are not.$str
is "abcdef"
, then
the value of $str{3}
is "d"
.Some languages, including PHP, contain a rather unusual kind of string construct called a heredoc. Here is an illustration of the heredoc syntax:
$someString = <<<MARKER Here is some text delimited by a name I chose (MARKER, in this case) in which I can use "double quotes" or 'single quotes', but variables like $x will be interpolated. I can even type MARKER here but the heredoc ends when I type it at the beginning of a line with a semi-colon immediately following ... MARKER;
Here are the basic type coercion/conversion rules for deciding what type a result will have when values of different types are involved in an expression that produces the result:
The context usually determines the nature of the coercion.
—>
string—>
number (., e or E —>
double)
(integer)$var
intval($var)
, doubleval($var)
or
strval($var)
settype($var, "integer")
gettype($var)
returns "integer"
,
"double", ...is_integer($var)
, is_int($var)
,
is_long($var)
is_double($var)
, is_float($var)
,
is_real($var)
is_boolean($var)
is_string($var)
is_array($var)
is_object($var
)Here is a table of the two compound types in PHP:
array | details |
object | details |
Here is a table of the two special types in PHP:
NULL | for variables which have no value |
resource | can ignore for the time being |
NULL
TypeNULL
can be context-coerced to 0
or
""
.isset($x)
is true
if $x
is
non-NULL
and otherwise false
.unset($x)
deletes $x
so that
isset($x)
returns false
.Here is a list of PHP operators:
+ - * / %
++ --
== != <
<= > >=
&& and || or !
=
, as
well as += *=
, etc.? :
.
Notes:
/
is not integer division in PHP.%
are coerced to integers if
necessary.===
and !==
should be preferred over
==
and !=
because they test for exact
equality (that is for both value and type) while == and !=
only test for value, and may also cause type coercion in a way that
makes the test misleading)PHP has selection constructs analogous to those of Java, C and C++,
except that the switch
-statement permits the use of integer,
double or string values ???????:
if (condition) statement if (condition) statement1 else statement2 if (condition1) statement1 else if (condition2) statement2 else if (condition3) ... else default_statement switch (expression) { case value1: statement1 break; case value2: statement2 break; case value3: //Multiple values can cause the same action case value4: statement_for_either break; ... default: default_statement } Also elseif ......................
Note that any "statement" in any of the above patterns can be replaced by a compound statement (several statements enclosed in braces).
JavaScript has looping constructs analogous to those of Java, C and C++:
for (initialization; condition; update) statement while (condition) statement do statement while (condition); Also foreach ............................
Notes:
break;
statement in the body of a loop causes the
loop to be terminated at that point and execution to continue with the
next statement following that loop.continue;
statement in the body of a loop causes
the current loop iteration to terminate and execution to return to the
"top" of the loop and carry on with the next iteration.echo
and print
can both be used for output:
echo
can accept a comma-separted list of
arguments.print
can accept only one argument.echo
also
accepts only one argument.echo "Hello, ", "world!"
;echo ("Hello, world!");
print "Hello, world!"
print ("Hello, world!")
printf("format",value);
(sample formats:
"%5d"
, "%8s"
, "%6.2f"
)Here's the syntax for a programmer-defined function:
function name([par_1, par_2, ..., par_n]) { statements [return value] }
f(&x)
is pass-by-reference, and can be either formal or actual.global
, as in f(){ global $x; }
to access a global $x
.static
, which causes retention of the value between calls, as in a C++ function.PHP has a number of superglobal variables (or, more simply, superglobals):
$_GET["nameOfElement"]
$_POST["nameOfElement"]
$_COOKIE["nameOfElement"]
$_SESSION["nameOfElement"]
$fileString = fread($fileVar, filesize("file.ext"));
$num_bytes_written = fwrite($fileVar, $str);
fclose($fileVar);
General Notes