Skip to content

Instantly share code, notes, and snippets.

@jgraup
Last active December 29, 2015 05:39
Show Gist options
  • Save jgraup/7623849 to your computer and use it in GitHub Desktop.
Save jgraup/7623849 to your computer and use it in GitHub Desktop.
PHP - Common
## Start/Stop
<?php // StartPHP
doPHPFunction();
/*
?> // StopPHP
*/
<htmlElement attrib="<?php echo $value; ?>" />
## Comments
# This is a comment
// This is a comment
/* This is a comment */
## Variables
$myInt = 1;
$myArray = [];
$myArray2 = array();
$aArray = array( "foo" => "bar" );
const DEFAULT_VALUE = 42;
## Properties
echo $aArray {"foo" }; // bar
## Print
echo "value"; // value
print_r ( $_REQUEST );
var_dump ( $_GET );
var_dump ( $_POST );
print_r ( $_SERVER );
## Include
require ("filename.php");
require_once (__DIR__ . "../lib/filename.php");
include ("filename.php");
include_once ("filename.php");
## Loop - For count
$count = count($j);
for ($i = 0; $i < $count; $i++) {
//...
}
## Loop - Value
$myArray = array();
// Loop through values
foreach( $myArray as $value ) {
echo $value;
}
## Loop - Key Value Pair
// Convert of the data from the URL query to an associative Array
foreach( $_REQUEST as $key => $value ) {
$myArray { strtolower($key) } = strtolower( $value );
}
var_dump($myArray);
## Array - Push
array_push($myArray,$value);
## Array - Shuffle
$randomized = shuffle($myArray);
## Array - Pick Random Index -> Value
$randomValue = $myArray[array_rand($myArray,1)];
## Array - Split/Explode Join/Implode
$space_separated = "a b c d e";
$array = explode(" ", $space_separated);
$comma_separated = implode(",", $array);
## Array - Contains
$array = array ("needle", "a", "b");
if (in_array("needle", $array)){
//...
}
## Replace
$phrase = "You should eat fruits, vegetables, and fiber every day.";
$healthy = array("fruits", "vegetables", "fiber");
$yummy = array("pizza", "beer", "ice cream")
$newphrase = str_replace($healthy, $yummy, $phrase);
## String Length
strlen ("12345"); // 5
## Is?
if (isset($foo) && is_array($foo)) { return $bar; }
## Date/Time
date_default_timezone_set('America/Los_Angeles');
$format = "D, d M Y h:m:s A +0000";
echo date($format); // Fri, 10 Jan 2014 07:00:00 PM +0000
## Classes - Get/Set
class A {
public $foo;
public function setFoo($bar) { $this->foo = $bar; }
public function getFoo() { return $this->foo; }
}
$bat = new A();
// Slower
$bat->setFoo('baz');
$baz = $bat->getFoo();
// Faster
$bat->foo = 'baz';
$baz = $bat->foo;
## Classes - Constructor
/**
* KeyValue Class Example
*/
class KeyValue
{
/**
* @var $key string Description
* @var $value string Description
*/
public $key;
public $value;
/**
* @param $key string This will be used for the key
* @param $value string This will be used for the value
*/
public function __construct($key, $value = "default") {
$this->key = $key;
$this->value = $value;
}
}
$kv = new KeyValue("myKey", "myValue");
echo $kv->key; // myKey
echo $kv->value; // myValue
## Classes - Static Functions
/**
* Class Common
*/
class Common
{
/**
* static @var $VERSION string Current version
*/
public static $VERSION = "1.0.0";
/**
* Check to see if a value is empty
* @param null $value
* @return bool true if the value is null or empty
*/
public static function IsNullOrEmpty($value = null)
{
return ($value === null || !isset($value) || trim($value) === '' );
}
}
Common::IsNullOrEmpty("A"); // false
Common::IsNullOrEmpty(null); // true
Common::$VERSION; // 1.0.0
## Header - Redirect
header('Location: http://www.example.com/');
## Header - Content Types
header("Content-type: text/xml; charset=utf-8");
header('Content-type: application/javascript');
header('Content-type: application/json');
## JSON
$obj = json_decode ('{"foo":"bar"}');
var_dump ($obj); // object(stdClass)#419 (1) {["foo"]=>string(3) "bar" }
$json = json_encode ($obj);
echo $json, "\n"; // {"foo":"bar"}
$test = json_decode ($json);
echo $test->foo; // bar
## JSONP - Callback
if ($data)
{
if ($_REQUEST['callback']) {
// jsonp
header('Content-type: application/javascript');
echo $_REQUEST['callback'],'(', $data,');';
} else {
header('Content-type: application/json');
echo $data;
}
}
## Errors
error_reporting(E_ALL);
error_reporting(E_ERROR | E_WARNING | E_PARSE | E_NOTICE);
error_reporting(E_ALL ^ E_NOTICE);
function myErrorHandler($errno, $errstr, $errfile, $errline)
{
echo "Error: [$errno] $errstr<br />\n";
}
$old_error_handler = set_error_handler("myErrorHandler");
## XML - XPath
$xml = new SimpleXMLElement($string);
/* Search for <a><b><c> */
$result = $xml->xpath('/a/b/c');
while(list( , $node) = each($result)) {
echo '/a/b/c: ',$node,"\n";
}
## CONSTANTS
$hasFoo = defined ("FOO");
if(!$hasFoo) define ("FOO", "BAR");
$foo = constant ("FOO");
## FILE
$exists = file_exists ( $filename );
$info = pathinfo($filepath);
@jgraup
Copy link
Author

jgraup commented Nov 27, 2013

// require vs. include - errors

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment