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

Anonymous function:

https://stackoverflow.com/questions/1499862/can-you-store-a-function-in-a-php-array#:~:text=If%20you%20want%20to%20store,’%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:

https://www.php.net/manual/en/language.types.callable.php

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
}

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=array();
$ret +=($tmp==true)?["a"=>11]:[];

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