My PHP Cheat Sheet

General Info

  • phpinfo() will give version and extension support info
  • php error log in xampp is in C:\xampp\php\logs\php_error_log or check for err_log in phpinfo() display.

Multiple URL parameters


Jason encode two dimensional  array

$arr = array();
while($row = mysql_fetch_assoc($result)) {
     $arr[] = $row;
echo json_encode($arr);

see more discussion on

see how to pass Objects and Arrays Between JavaScript and PHP with JSON



Looking for punctuation in UTF8 strings
preg_match ‘/pP$/u’

Useful Links

PHP UTF8 cheatsheet:

Anonymous function:,’%20%3D%3E%20’do_echo’%20%5D%3B&text=%24functions%20%3D%20array(%20’function1’%20%3D%3E%20create_function(,echo%20%24echo%3B’)%20)%3B

Callback function:

Parse XML

PHP simple XML :


To get last weekday before 30th of March 2015:

echo date(‘l jS \of F Y h:i:s A’, strtotime(‘last weekday 30th March 2015’));

output:  Friday 27th of March 2015 10:00:00 AM

strtotime will work for things like next monday, last sunday, +5 hours, +1 week…etc.  Use date to format the output

echo $date->format(‘l m/d/y’) will output format like:  Monday 3/30/15


Error control Operator

An @ before a command in PHP means that no errors are printed. It’s called the error control operator.


Find all text files

foreach (glob("*.txt") as $textfile) {
//do stuff

Start a PHP server on port 8000 with document root foo:

 php -S localhost:8000 -t foo/

Note: default port for Phpstorm server is 63342

Add Associative array or empty (will not add key)

$ret +=($tmp==true)?["a"=>11]:[];

$ret[‘a’] will exist only if $tmp is true