PHP
===
Variables:
----------
$variable_name = value;
global $g_var = value;
static $s_var = value;
function birthday(){...}
PHP super globals:
------------------
$GLOBALS
$_SERVER
$_GET
$_POST
$_COOKIE
$_FILES
$_ENV
$_REQUEST
$_SESSION
ex: in a file names "test.php"
echo $_SERVER["PHP_SELF"];
output: /test.php
Strings:
--------
$str = "world";
echo "Hello $str";
$str.= "y"; // Concatenation
* You can't include variables when using single quotes.
\n - new line
\r - return
\t - tab
\$ - dollar sign
\" - double quote
Wrong: echo "
hello
";
Right: echo 'hello
';
String comparing:
* Returns 0 when equal.
Not case sensitive: strcmp (str1, str2);
Case sensitive: strcasecmp (str1, str2);
Concatination:
echo $my_str1 . "hello" . $my_str2;
echo $my_str1 . 3 . $my_str2;
Constants:
----------
* Constants are global.
ex. define("HELLO", "Hello world!");
echo HELLO; // outputs "Hello world."
Predefined constants:
__LINE__
__FILE__
Boolean operators:
------------------
AND, OR, XOR, NOR, NOT
Conditions:
-----------
if (...){ ... }
elseif (...){ ... }
else { ... }
switch (...) {
case ... : ... break;
default: ... ;
}
The ? Operator:
---------------
{expression} ? return_when_expression_true : return_when_expression_false;
Loops:
------
while ( ... ) { ... }
do { ... } while (...);
for (init_expression; test_expression; mod_expression) { ... }
break - will break out of the loop.
continue - skipps the current loop cycle.
Function default value:
-----------------------
function capitalize( $str, $each=TRUE )
{ ... }
Function referecnces:
---------------------
function foo(&$str) { ... }
foo(&$my_string);
Including and requiring PHP files:
----------------------------------
include('my_file.php');
include_once
require
require_once
* Require functions will halt execution if the required file doesn't exist.
PHP functions:
--------------
$exists=function_exists("my_function");
Classes:
--------
class A {
var $m_var;
A() {...} // ctor
public function __construct() {...} // php5 style constructor
public function __destruct() {...} // php5 style destructor
function my_func() {$this->m_var = 10;}
}
$obj = new A();
obj->my_func();
Inheritance:
------------
calss B extends A {
// The parent ctor won't be called automaticaly
__construct() {parent::__construct();}
// Overriding
function my_func() {parent::my_func();}
}
:: operator
You can refer to class variables and methods instead of refering to instance variables and methods. Will affect all instances of that class.
References:
-----------
$str = "hello";
$ref = &$str; // ref points to the same mem as str
Arrays:
-------
php picks the lowest empty numeric index:
$weekdays[] = 'Monday'
$weekdays[] = 'Tuesday'
- Manual indexing:
$weekdays[0] = 'Monday'
$weekdays[1] = 'Tuesday'
- Init via php 'array' function:
$weekdays=array('Monday', 'Tuesday', 'Wednesday');
- Associative array (string indexed):
$shapes=array('Soda Can' => 'Cylinder',
'Note Pad' => 'Rectangle',
'Apple' => 'Sphere');
- Traversing associative array:
foreach ($shapes as $key => $value) {
print"The $key is a $value.
\n";
}
- Validating the array:
$arr = array('val1', 'val2');
echo is_array($arr) ? 'a valid array' : 'not an array';
- Counting the array elements:
$cnt = count($arr);
- Sorting the array:
sort($arr, sort_regular);
sort($arr, sort_numeric);
sort($arr, sort_string);
sort($arr, sort_locale_string);
- Dumping all the array:
echo var_dump($arr);
- Extracting variables from an array:
(Generates local variables from name:val pairs)
extract($arr);
- To prevent "extract" of overiting vars with the same name:
extract($array, EXTR_PREFIX_ALL, "the_prefix");
- Building an array from variables:
(The complement to "extract")
$SodaCan='Cylinder';
$NotePad='Rectangle';
$arr = compact('SodaCan', 'Note Pad');
- Array functions:
Reset (array); - Resets the pointer to the beginning of the array.
array_push (array,elements); - Adds elements to the end of the array.
array_pop (array); - Returns and removes the last element of an array.
array_unshift (array,elements); - Adds elements to the beginning of the array.
array_shift (array); - Returns and removes the first element of an array.
array_merge (array,array); - Combines two arrays together and returns the new array.
array_keys (array); - Returns an array containing all of the keys from the supplied array.
array_values (array); - Returns an array containing all of the values from the supplied array.
Shuffle (array); - Resorts the array in random order.