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

url?param=value1&param=value2&param=value3

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

 

REGEX

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

Useful Links

PHP UTF8 cheatsheet:
http://blog.loftdigital.com/blog/php-utf-8-cheatsheet

 

Parse XML

PHP simple XML : http://www.w3schools.com/Php/php_xml_simplexml_read.asp

DateTime

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
}