This document provides a cheat sheet on PHP fundamentals including basic syntax, variables, numerical operations, arrays, classes and objects, functions, control structures like if/else, switch, loops, and more. It covers PHP basics like declaring variables, echoing output, concatenating strings, creating and accessing arrays, defining classes with properties and methods, writing functions, and using common control flow structures. The cheat sheet is intended to be a quick reference for PHP fundamentals.
This document provides a cheat sheet on PHP fundamentals including basic syntax, variables, numerical operations, arrays, classes and objects, functions, control structures like if/else, switch, loops, and more. It covers PHP basics like declaring variables, echoing output, concatenating strings, creating and accessing arrays, defining classes with properties and methods, writing functions, and using common control flow structures. The cheat sheet is intended to be a quick reference for PHP fundamentals.
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