PHP Notes 101

Embed in HTML:

<?php

code here

?>

 


Comments

/* comment
    comment
    comment */

// Comment

# Comment

 


Variables

Loosely typed, no declarations necessary

Preceded by $, must start with a letter, followed by any alphanumeric and '_' combination

Assigning a value:

$Pi = 3.14;

 


Variable Scope

No such thing as a global variable. 

Variables only exist inside their function.

Variables from the calling function are not available in the called function but they are avaialble via the special array GLOBALS
ie. $GLOBALS("Pi") = 3.14

 


Indirect references to variables

$position = "David"
$$position = "Technician"
print $David

outputs "Technician"

 


Variable tests/management:

isset($position)  returns true if $position has been set and is other than NULL

also:
isset($arr["offset"])
isset($obj->property)

unset($position) removes & frees up from memory $position

empty($position) opposite of isset()

 


Superglobals

$_GET[]   // All get vars received from client browser

$_POST[]   // All post vars received from client browser

$_COOKIE[]   // All cookiess received from client browser

$_ENV[]   // Env vars

$_SERVER[]  // Web server vars

Available everywhere

 


Datatypes

Integers

Floating Point

Strings (can be really big)
$Position = 'Technician'
$Position = "Technician"

Double quotes allows variables embedded in the string construct to automatically be replaced by the variable contents

Special sequences:

 

\n Newline
\t Tab
\" Double quote
\\ Backslash
\0 Ascii 0 (Null)
\r Line feed
\$ Escapes $ sign so is not treated as a variable name
\x{hex #} Hex number ie \0x32

String offsets

$Position = "Technician"
Print $Position{2}  // prints 'c'

 


Booleans

 

Data type False True
Integer 0 not 0
Floating point 0.0 not 0.0
String Empty or zero (0) Neither empty or zero
NULL Always Never
Array 0 elements 1 or more elements
Object Never Always
Resource Never Always