PHP Fundamentals Cheat Sheet
by mkpeacock via cheatography.com/1456/cs/489/
Basic Syntax Variables Numerical Operations
Files start with <?php $variableName; Addition $variable = $variable + 5;
Single line comment // a comment $variableName = "Some String"; Subtraction $variable = $variable - 5;
Multi-line comment / comment / $variableName = 'Some String'; Multiplication $variable = $variable * 5;
End of instruction ; $variableName = strtoupper('text'); Division $variable = $variable / 5;
Include code from require_once('some_fil $variableName = 5;
another file e.php'); Arrays
$variable = "Some {$otherVariable} info";
echo $variableName; // output Create $myArray = array();
Classes and Objects
$newVar = $var1 . $var2; // concatenation Push into $myArray[] = "Something";
class SomeClass {
Push to $myArray['key'] = "Value";
private $property;
Functions associative
public $anotherProperty;
Create numeric $myArray = array('value',
protected $yetAnotherProperty = null; function multiply($arg1, $arg2)
'value2');
public function __construct($arg=null) {
{ return $arg * $arg2; Create $a = array('key'=>'val');
$this->property = $arg; } associative
} $param = 4; Print from echo $myArray[0];
public function someMethod() $param2 = 8;
numeric
{ $answer = multiply($param, $param2);
Print from echo $myArray['key'];
echo “Hi”;
associative
} Control Structure: IF
public function getProperty() Associative Keys are strings
// if something is true do something else arrays
{
if( $something == true ) {
return $this->property; Numeric arrays Keys are numbers:
doSomethingElse();
} 0,1,2,3,4
} elseif( $something == false ) {
public function setProperty( $p )
// however, if something is false, do
{
something Control Structure: Switch
$this->property = $p;
doSomething(); switch( $someVariable ) {
}
} else { case 1:
}
// otherwise, lets do nothing echo “Some variable equals 1”;
$myObject = new SomeClass( “123” );
doNothing(); break;
echo $myObject->getProperty(); // 123
} case “cheese”
$myObject->property; // ERROR:private
echo “Some variable equals cheese”;
Control Structure: Loops break;
default:
foreach( $myArray as $key => $value ) {
echo “No idea”;
echo “My array has the value {$value} stored
break;
against the key {$key}<br />”;
}
}
while( someCondition == true ) {
echo ‘hello’;
}
By mkpeacock Published 25th July, 2012. Sponsored by Readability-Score.com
cheatography.com/mkpeacock/ Last updated 5th June, 2014. Measure your website readability!
Page 1 of 1. https://readability-score.com