Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

PHP Fundamentals Cheat Sheet: by Via

Download as pdf or txt
Download as pdf or txt
You are on page 1of 1

PHP Fundamentals Cheat Sheet

by mkpeacock via cheatography.com/1456/cs/489/

Basic Syntax Variables Numerical Operations

Files start with <?php $varia​ble​Name; Addition $variable = $variable + 5;

Single line comment // a comment $varia​bleName = "Some String​"; Subtra​ction $variable = $variable - 5;

Multi-line comment / comment / $varia​bleName = 'Some String'; Multip​lic​ation $variable = $variable * 5;

End of instru​ction ; $varia​bleName = strtou​ppe​r('​text'); Division $variable = $variable / 5;

Include code from requir​e_o​nce​('s​ome​_fi​l $varia​bleName = 5;


another file e.p​hp'); Arrays
$variable = "Some {$othe​rVa​riable} info";

echo $varia​ble​Name; // output Create $myArray = array();


Classes and Objects
$newVar = $var1 . $var2; // concat​enation Push into $myArray[] = "​Som​eth​ing​";
class SomeClass {
Push to $myArr​ay[​'key'] = "​Val​ue";
​ ​ ​ ​private $property;
Functions associ​ative
​ ​ ​ ​public $anoth​erP​rop​erty;
Create numeric $myArray = array(​'va​lue',
​ ​ ​ ​pro​tected $yetAn​oth​erP​roperty = null; function multip​ly(​$arg1, $arg2)
'value2');
​ ​ ​ ​public function __cons​tru​ct(​$ar​g=null) {
{ ​ ​ ​ ​return $arg * $arg2; Create $a = array(​'ke​y'=​>'v​al');
​ ​ ​ ​$th​is-​>pr​operty = $arg; } associ​ative
} $param = 4; Print from echo $myArr​ay[0];
public function someMe​thod() $param2 = 8;
numeric
{ $answer = multip​ly(​$param, $param2);
Print from echo $myArr​ay[​'key'];
​ ​ ​ echo “Hi”;
associ​ative
} Control Structure: IF
public function getPro​perty() Associ​ative Keys are strings
// if something is true do something else arrays
{
if( $something == true ) {
​ ​ ​ ​return $this-​>pr​operty; Numeric arrays Keys are numbers:
​ ​ ​ ​doS​ome​thi​ngE​lse();
} 0,1,2,3,4
} elseif( $something == false ) {
public function setPro​perty( $p )
​ ​ ​ // however, if something is false, do
{
something Control Structure: Switch
​ ​ ​ ​$th​is-​>pr​operty = $p;
​ ​ ​ ​doS​ome​thi​ng(); switch( $someV​ariable ) {
}
} else { ​ ​ ​ case 1:
}
​ ​ ​ // otherwise, lets do nothing ​ ​ ​ ​ ​ ​ ​ echo “Some variable equals 1”;
$myObject = new SomeClass( “123” );
​ ​ ​ ​doN​oth​ing(); ​ ​ ​ ​ ​ ​ ​ ​break;
echo $myObj​ect​->g​etP​rop​erty(); // 123
} ​ ​ ​ case “cheese”
$myObj​ect​->p​rop​erty; // ERROR:​private
​ ​ ​ ​ ​ ​ ​ echo “Some variable equals cheese”;
Control Structure: Loops ​ ​ ​ ​ ​ ​ ​ ​break;
​ ​ ​ ​def​ault:
foreach( $myArray as $key => $value ) {
​ ​ ​ ​ ​ ​ ​ echo “No idea”;
​ ​ ​ echo “My array has the value {$value} stored
​ ​ ​ ​ ​ ​ ​ ​break;
against the key {$key}​<br />”;
}
}
while( someCo​ndition == 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

You might also like