####PHP cheat sheet.
Last active
January 6, 2016 06:21
-
-
Save kurtisdunn/5f5965baf188840f7e40 to your computer and use it in GitHub Desktop.
PHP cheat sheet.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<? | |
//Strings | |
$str = "This is a string \n"; | |
$str2 = 'This is another string.'; | |
print "<p>The string '$str' is ".strlen($str). | |
" characters long.</p>"; | |
//Date | |
print date('r'). | |
"<br>"; | |
$now_1 = getdate(); | |
$now_2 = localtime(); | |
print "{$now_1['hours']}:{$now_1['minutes']}:{$now_1['seconds']}\n"; | |
print "$now_2[2]:$now_2[1]:$now_2[0]"; | |
print "<br />"; | |
$a = getdate(); | |
printf('%s %d, %d', $a['month'], $a['mday'], $a['year']); | |
print "<br /><br />"; | |
//Arrays | |
$cars = ['Ferrari', 'Maserati', 'Audi', 'BMW', 'Alfa Romeo']; | |
print $cars[0]; | |
print "<ul>"; | |
foreach($cars as $car) { | |
print "<li>$car</li>"; | |
} | |
print "</ul>"; | |
//Numeric Example | |
foreach([5, '5', '05', 12.3, '16.7', 'five', 0xDECAFBAD, '10e200'] as $maybeNumber) { | |
$isItNumeric = is_numeric($maybeNumber); | |
$actualType = gettype($maybeNumber); | |
print "<br>Is the $actualType $maybeNumber numeric? "; | |
if (is_numeric($maybeNumber)) { | |
print "yes \r\n"; | |
} else { | |
print "no"; | |
} | |
print "\n"; | |
} | |
//Functions | |
function add($a, $b) { | |
return $a + $b; | |
} | |
$total = add(2, 2); | |
print "<br><br>Function 'add' = $total<br>"; | |
function wrap_in_html_tag($text, $tag = 'strong') { | |
return "<$tag>$text</$tag>"; | |
} | |
print wrap_in_html_tag("Apache HTML requests are slow and annoying!<br>"); | |
//Classes | |
class Animal { | |
public $name = "default"; | |
public $age = 0; | |
} | |
$animal = new animal; | |
//Web stuff | |
//Cookie | |
setcookie('Car', 'BMW'); | |
if (isset($_COOKIE['Car'])) { | |
print "<br>You drive a {$_COOKIE['Car']}.<br>"; | |
} | |
//Query String | |
$vars = array( | |
'make' = > 'Ferrari', | |
'model' = > '488 GTB', | |
'year' = > '2016' | |
); | |
$query_string = http_build_query($vars); | |
$url = '/muppet/select.php?'.$query_string; | |
print $url; | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment