// JavaScript-like inline comment
# perl-like inline comment
/* Java-like block comment */
* Compound statements are formed with braces
* Compound statements are not blocks
----
//explanation//: a block is defined as code for which variables defined between braces (not function bodies), are local to the enclosing block.
Java has this property but JavaScript and PHP do not.
===== Primitives, Operations, and Expressions =====
* [[eg-259:lecture16#variables|Variables]]
* [[eg-259:lecture16#primitive_types|Primitive Types]]
* [[eg-259:lecture16#strings|Strings]]
* [[eg-259:lecture16#booleans|Booleans]]
* [[eg-259:lecture16#arithmetic_operators_and_expressions|Arithmetic Operators and Expressions]]
* [[eg-259:lecture16#string_operations_and_functions|String Operations and Functions]]
* [[eg-259:lecture16#scalar_type_conversions|Scalar Type Conversions]]
===== Variables =====
* There are no type declarations
* An unassigned (unbound) variable has the value ''NULL''
* The ''unset'' function sets a variable to ''NULL''
* The ''IsSet'' function is used to determine whether a variable is ''NULL''
* ''error_reporting(15);'' -- prevents PHP from using unbound variables
* PHP has many predefined variables, including the environment variables of the host operating system
* You can get a list of the predefined variables by calling ''phpinfo()'' in a script
===== Primitive Types =====
* //There are eight primitive types//:
* Four scalar types: ''boolean'', ''integer'', ''double'', and ''string''
* Two compound types: ''array'' and ''object''
* Two special types: ''resource'' and ''NULL''
* //Integer// and //double// are like those of other languages
===== Strings =====
* Characters are single bytes
* String literals are single or double quoted
* //Single-quoted string literals//
* Embedded variables are **not** interpolated
* Embedded escape sequences are **not** recognized
* //Double-quoted string literals//
* Embedded variables **are** interpolated
* If there is a variable name in a double-quoted string but you don't want it interpolated, it must be escaped.
* Embedded escape sequences **are** recognized
* For both single- and double-quoted literal strings, embedded delimiters must be escaped
-----
gettype($total) // may return "unknown"
is_integer($total) // a predicate function
===== Output =====
* Output from a PHP script is HTML that is sent to the browser
* HTML is sent to the browser through standard output
* There are three ways to produce output: ''echo'', ''print'', and ''printf''
* ''echo'' and ''print'' take a string, but will coerce other values to strings:
echo "whatever"; # Only one parameter
echo("first
", $sum) # More than one
print "Welcome to my site!"; # Only one
===== More Output =====
* ''echo'' has a shorthand ''= ?>'' that makes it pleasanter to print the value of a PHP variable in //copy mode//:
Hello =$name?>, welcome to my site. It's =date("D M j Y")?> today.
----
* This shorthand should only be used for printing variables or with functions that return a string.
* The short syntax only works with the [[http://uk2.php.net/manual/en/ini.core.php#ini.short-open-tag|short_open_tag]] configuration setting enabled (which it is in Ubuntu).
===== Hello World (PHP) =====
* PHP code is placed in the body of an HTML document
Trivial php example
===== First PHP Example =====
A simple example to illustrate a PHP document: [[/eg-259/examples/lecture16/today.php|today.php]] ([[http://localhost/eg-259/examples/lecture16/today.php|today.php @ localhost]])
----
today.php – A simple example to illustrate a PHP document
Welcome to my home page
";
print "Today is: ";
print date("l, F jS");
print "
";
?>
Result:
{{eg-259:l16-today.png|Result of executing PHP script today.php}}
===== Control Expressions =====
* Relational operators -- same as JavaScript, (including ''==='' and ''!=='')
* Boolean operators -- two sets, ''&&'' and ''and'', ''||'' and ''or''.
===== Selection statements =====
* ''if'', ''if-else'', ''elseif''
* ''switch'' -- as in C
* The ''switch'' expression type must be integer, double, or string
===== Looping constructs =====
* ''while'' -- just like C
* ''do-while'' -- just like C
* ''for'' -- just like C
* ''foreach'' -- discussed later
===== Other statements =====
* ''break'' -- in any ''for'', ''foreach'', ''while'', ''do-while'', or ''switch''
* ''continue'' -- in any loop
===== Alternative componund delimiters =====
* "syntax sugar" -- improves readability:
if (...) :
...
endif;
===== Another Example =====
An example to illustrate loops and arithmetic: [[/eg-259/examples/lecture16/powers.php|powers.php]] ([[http://localhost/eg-259/examples/lecture16/powers.php|powers.php @ localhost)]]
----
* Code:
powers.php &ndash An example to illustrate loops and arithmetic
Powers table
Number
Square Root
Square
Cube
Quad
$number ");
print(" $root $square ");
print(" $cube $quad ");
}
?>
* Result
{{eg-259:l16-powers.png|Result of executing the powers.php script}}
===== Intermingling HTML and PHP =====
* HTML can be intermingled with PHP script
At this point, $a and $b are
equal
So, we change $a to three times $a
----