PHP Crib Sheet

Snippets

<?php echo ($x++ & 1?'odd':'even') ?>

Headers and the Like

header("Content-Type: text/html; charset=utf-8");
header("Content-Language: en-US");
header("Cache-Control: public");
header("X-UA-Compatible: IE=edge; chrome=1");
date_default_timezone_set('America/Chicago');

Headers for Caching Problems

… where the browser is inexplicably caching …

header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate"); // HTTP/1.1
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache"); // HTTP/1.0
header('Expires: '.gmdate('D, d M Y H:i:s \G\M\T', time() - (60 * 60)));

For more about these, head over to the W3C and check out information from RFC 2616 on Header Field Definitions. The above solution was largely gleaned from Stack Overflow.


Constants

define('LF',"\n");
define('ROOT',$_SERVER['DOCUMENT_ROOT']);
define('HOST',$_SERVER['SERVER_NAME']);
define('SELF',htmlentities($_SERVER['PHP_SELF']));
define('THIS',htmlentities($_SERVER['SCRIPT_URL']));
define('TSEC',($_SERVER['HTTPS']?'https://':'http://')); // Transfer Security (HTTPS/HTTP)

$_SERVER Super Globals

$_SERVER['PHP_SELF'] → /reference/language/php.html
$_SERVER['argv'] → Array
$_SERVER['argc'] → 0
$_SERVER['GATEWAY_INTERFACE'] → CGI/1.1
$_SERVER['SERVER_ADDR'] → 74.208.16.167
$_SERVER['SERVER_NAME'] → fun.wd2.us
$_SERVER['SERVER_SOFTWARE'] → Apache
$_SERVER['SERVER_PROTOCOL'] → HTTP/1.1
$_SERVER['REQUEST_METHOD'] → GET
$_SERVER['REQUEST_TIME'] → 1490335875
$_SERVER['REQUEST_TIME_FLOAT'] → 1490335875.2175
$_SERVER['QUERY_STRING'] → 
$_SERVER['DOCUMENT_ROOT'] → [obfuscated]
$_SERVER['HTTP_ACCEPT'] → text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
$_SERVER['HTTP_ACCEPT_CHARSET'] → 
$_SERVER['HTTP_ACCEPT_ENCODING'] → x-gzip, gzip, deflate
$_SERVER['HTTP_ACCEPT_LANGUAGE'] → 
$_SERVER['HTTP_CONNECTION'] → 
$_SERVER['HTTP_HOST'] → fun.wd2.us
$_SERVER['HTTP_REFERER'] → 
$_SERVER['HTTP_USER_AGENT'] → CCBot/2.0 (http://commoncrawl.org/faq/)
$_SERVER['HTTPS'] → on
$_SERVER['REMOTE_ADDR'] → 54.87.65.140
$_SERVER['REMOTE_HOST'] → 
$_SERVER['REMOTE_PORT'] → 15832
$_SERVER['REMOTE_USER'] → 
$_SERVER['REDIRECT_REMOTE_USER'] → 
$_SERVER['SCRIPT_FILENAME'] → [obfuscated]
$_SERVER['SERVER_ADMIN'] → webmaster@fun.wd2.us
$_SERVER['SERVER_PORT'] → 443
$_SERVER['SERVER_SIGNATURE'] → 
$_SERVER['PATH_TRANSLATED'] → 
$_SERVER['SCRIPT_NAME'] → /reference/language/php.html
$_SERVER['REQUEST_URI'] → /reference/language/php.html
$_SERVER['PHP_AUTH_DIGEST'] → 
$_SERVER['PHP_AUTH_USER'] → 
$_SERVER['PHP_AUTH_PW'] → 
$_SERVER['AUTH_TYPE'] → 
$_SERVER['PATH_INFO'] → 
$_SERVER['ORIG_PATH_INFO'] → /reference/language/php.html

Magic Constants

__LINE__ → 120
__FILE__ → [obfuscated - shows server-side path and file name]
__DIR__ → [obfuscated - shows server-side path]
__FUNCTION__ → 
__CLASS__ → 
__TRAIT__ → 
__METHOD__ → 
__NAMESPACE__ →