|
PHP
An Introduction for Programmers |
|
Prof. David Bernstein |
| Computer Science Department |
| bernstdh@jmu.edu |
; (except for the last statement)
{ and a }
;
# and the end of the line
is a comment// and the end of the line
is a comment/* and a */
is a commentlong type of the C compiler)double type of the C compiler). operator\ is the escape character and can be used to
represent special characters (e.g., \n represents
the newline character) and Unicode characters
(e.g., \u00e9 is é)false, 0, 0.0,
"", an array with 0 elements, and
NULL all evaluate to false
true
$ followed by an identifierNULL
$$
(e.g., $name = "price"; $$name = 5; will
assign the value 5 to the
variable named price)+)-)*)/)/)%)++) and Decrement (--)-)>
<
>=
<=
==
===
!=
!==
&&, and
||, or
!
xor
if Statementwhile Loopsdo-while Loopsfor Loops
function area(width, height)
{
$result = 0;
if (($width > 0) && ($height > 0)) $result = $width * $height;
return $result;
}
f):
$f = function($width, $height)
{
$result = 0;
if (($width > 0) && ($height > 0)) $result = $width * $height;
return $result;
};
$size = area(10.2, 20.9);
isset(); default values
(i.e., including an assignment) in the formal parameter list;
and/or func_num_args()
and func_get_arg()
global in that functionstatic modifier in the
initializationdefine() function
define("DEPT", "CS");
$course = DEPT . "531";
$grades = array(90, 85, 73, 88);
$bad_idea = array("CS", 139, 4, "B-");
[] Operator:
$a = array(2, 4, 6); $a[9] = 20;
$inventions = array("Edison"=>"Light Bulb", "Whitney"=>"Cotton Gin");
count() and sizeof() Functions:
foreach Loops[] operator more than once
To pass a parameter by reference, include an &
before the variable name in the formal parameter list.
function clear(&$data)
{
$n = count($data);
for ($i=0; $i<$n; $i++)
{
$data[$i] = 0;
}
}
$sales = array(100, 100, 100);
clear($sales);
// All the elements of $sales now have the value 0
To pass a parameter by value, omit the &.
function clear($data)
{
$n = count($data);
for ($i=0; $i<$n; $i++)
{
$data[$i] = 0;
}
}
$sales = array(100, 100, 100);
clear($sales);
// All the elements of $sales still have the value 100
To return by reference, include an & before the
variable name in the formal parameter list and use
the =& operator to assign the result.
function &get(&$data, $index)
{
return $data[$index];
}
$courses = array("CS139", "CS159", "CS240");
$first =& get($courses, 0);
$first = "CS149";
// $courses[0] now contains the string "CS149"
To return by value, omit the & and use the =
operator to assign the result.
function get(&$data, $index)
{
return $data[$index];
}
$courses = array("CS139", "CS159", "CS240");
$first = get($courses, 0);
$first = "CS149";
// $courses[0] still contains the string "CS139"
this:
Membership Operator:
->
public, protected,
or private) and a variable initialization
and method consists of a visibility and
a function declaration
class Course
{
private $dept = "";
private $number = "";
function __construct($d, $n)
{
$this->dept = $d;
$this->number = $n;
}
function toString()
{
return $this->dept . $this->number;
}
}
new Operator:
$bestever = new Course("CS", "531");
echo $bestever->toString();
static modifier:: operator and internally using
self and the :: operator
const modifier (not the
define() function):: operator and internally using the
identifier
class Course
{
const FALL = 0;
const SPRING = 1;
const SUMMER = 2;
private $dept = "";
private $number = "";
private $semester = self::FALL;
function __construct($d, $n, $s)
{
$this->dept = $d;
$this->number = $n;
if (($s === self::SUMMER) || ($s == self::SPRING)) $this->semester = $s;
}
function getSemester()
{
return $this->semester;
}
function toString()
{
return $this->dept . $this->number;
}
}
$cs531 = new Course("CS", "531", Course::SPRING);
array)
before the formal parameterNULL)
class Student
{
private $courses = array();
function addCourse(Course $course)
{
$this->courses[count($this->courses)] = $course;
}
}
parent and the :: operatorself and the
:: operator (rather than $this
and the -> operator)abstract modifier in the declaration
of the class or methodclass replaced by interface
implements
class replaced by trait
include and require Statements:
include generates a warning and
require generates a fatal error if the file
is not foundinclude_once and require_once Statements:
namespace statementuse statementfile_exists()
fopen() and fclose()
fread() and fwrite()
fscanf() and fprintf()