PHP Introduction
Using CGI is disadvantageous as it is difficult to add a small amount of dynamic content to a web page.
PHP is a scripting language that aims to make it easy to write server-side code.
PHP Processing
You install PHP as a web server plugin:
This avoids the need to execute an external program.
PHP code is embedded into HTML pages using tags. This makes it easy to turn static pages into dynamic ones.
The processing follows the following method:
- The web server receives a client request.
- The web server recognises that the client request is for an HTML document containing PHP code.
- The server executes the PHP, substitutes output into the HTML document; the resulting page is then sent to the client.
The client never sees the PHP code, only the HTML document that is produced.
PHP “Hello, world!”
You can write this like so:
<!DOCTYPE html>
<html lang="en-GB">
<head>
<title>Hello, world!</title>
</head>
<body>
<h1>Our First PHP Script</h1>
<?php
print("<p><b>Hello, world!</b></p>\n");
?>
</body>
</html>
- PHP code is enclosed between
<?php
and?>
. - The file name must have the extension
.php
.
There is also a command line program included in php.
This means that it can be used as a scripting language outside of a website.
You can run a php script using a she-bang #!
at the start of the script, or by passing the php file as an argument.
PHP Scripts
- They contain only statements and comments
- No
main
or classes.
- No
- Statements end in a semi-colon
;
. - One line comments start with
//
or#
. - Multiline comments are enclosed in
/*
and*/
.
PHP Types
PHP has eight datatypes including:
Four primitive types:
bool
int
float
string
Two compound types:
array
object
Two special types:
resource
NULL
Integers and Floats
PHP has several pre-defined mathematical functions that you can look up.
PHP also provides pre-defined number constants:
M_PI
- PiNAN
- Not a number.INF
- Infinity
The last two can be returned from mathematical functions where appropriate.
Booleans
When converting to boolean, the following values are “falsy”:
Value | Type |
---|---|
FALSE |
Boolean |
0 | Integer |
0.0 | Float |
‘0’ | String |
’’ | Empty String |
{} | An array with zero elements |
NULL |
Null and other unset variables. |
SimpleXML objects created from empty tags. |
All other values are considered “truthy”.
When converting boolean to string:
TRUE
becomes “1”.FALSE
becomes “”.
Strings
- Strings can be single or double quoted.
- Strings can span several lines.
-
Concatenation is denoted with
.
.This is opposed to
+
like other languages.
You can use substitution in double quoted strings:
$title = "String Operators";
print "<title>$title</title >";
Which produces the following HTML:
<title>String Operators</title>
- In single quoted strings only
\
and'
are escaped. - In double quoted strings all common escape codes are accepted.
Variables
All PHP variables start with $
, they can’t start with a number and are case sensitive:
-
Variables don’t have to be declared before they are used.
You can use a variable with the default value before is it assigned.
-
Variables don’t have to be initialised before they are used.
- Default - generally
FALSE
, empty, orNULL
- values are chosen.
- Default - generally
They can be assigned like so:
$student_id = 123457890;
$b = ($a = 0) + 1;
Constants
You can define global constants with the following function:
define(<name>, <expression>)
This can be used like so:
define("PI", 3.14159);
and you can access them like so:
print "Value if PI:" . PI . "\n";
You can’t use variable substitution with constants.
Values, Variables & Types
PHP provides several functions to find the type of an expression:
Function | Description |
---|---|
string gettype(<expr>) |
Returns the type of expr as a string. |
bool is_<type>(<expr>) |
Checks whether <expr> is of type <type> . |
void var_dump(<expr>) |
Displays structured information about <expr> that include its type and value. |
Type Juggling & Casting
PHP will automatically convert a value to the appropriate type as required by the operation applied to the value (type juggling).
We can also apply an identity function of the target type to force a type conversion:
\[\text{`12'} * 1.0 \rightarrow 12.0\]PHP also support explicit type casting via (<type>)
:
$a = (bool) "foo"
print $a
would return:
TRUE