Show Menu
Cheatography

PHP Cheat Sheet (DRAFT) by

Beginners php cheatsheet

This is a draft cheat sheet. It is a work in progress and is not finished yet.

Strings and variables

Print as html
echo "This is my string­";
This is my string
String
"­Hello World!­";
Hello World!
Escape sequence
"­Hello \"Wo­rld­\"!";
Hello "­Wor­ld"!
Concat­enation (Literal)
echo "­one­" . "­two­";
onetwo
Variable creation and assignment
$var = "­Hel­lo";
Variable contat­enation
echo $var . " you";
Hello you
Variable parsing
echo "$var you";
Hello you
Expanding string
echo "­${v­ar}oo! you!;
Hellooo! you!
Concat­enating assigmente
$var .= " world";
Hello world
Assign by reference
$var1 = $var2;

Types and operators

String
$string = "­Hel­lo";
Int
$int = 4;
Float
$float = 4.2;
Expone­nti­ation
4**2 --> (4^2) = 64
Modulo
7 % 3 --> 1
Mathem­atical assignment operator
$int += 3;
Increment and decrement
$int ++;
Global variable
global %var; //Inside a function to access a value declared outside the function
- Result of adding up two floats 9.9 and 1.0 will return an integer, since the result is 10 and evaluates to a whole number. Applies to every calcul­ation. The reverse is also true.
- Operations order: () >> * >> and / >> + and -

Functions

function greeting($value = "Sam") // Default value = Sam
{
    return "Hello $value!";
}

$return_value = greeting("Tom");

echo $return_value; //Prints: Hello Tom!
echo greeting(); //Prints: Hello Sam!

function addXPermanently (&$param) // Passed by reference
{
  $param = $param . "X";
  echo $param;
};
$word = "Hello";
addXPermanently($word); // Prints: HelloX
echo $word; // Prints: HelloX

Built-in Functions

gettyp­e(4); // Prints: integer
Get the type of the parameter given as a string
var_du­mp(4); // Prints: int(10­00000)
Prints details about the argument
strive­("He­llo­"); // Prints: olleH
Prints the string in reversed order
strtol­owe­r("H­ell­o"); // Prints: hello
Lower case the string
str_re­pea­t("H­ell­o", 2); // Prints: HelloHello
Repeats the string the number of times specified
substr­_co­unt­($s­tring, $subst­ring);
Number of instances of a substring within a string
abs(-2); // Returns 2
Returns the absolute value
round(­3.8); // Returns 4
Rounds a number
rand();
Returns a random number between 0 and the largest allowed
rand(3, 10);
Returns a number between 3 and 10 inclusive
getran­dmax()
Returns the largest random number value possible
 

Data Structures

Array
$my_array = array(0, 1, "­A");
Array
$my_array = [0, 1, "­A"];
Print array
print_­r($­my_­array);
Returns array separated by value
implod­e(", ", $my_ar­ray);
Access array index
$y_arr­ay[1];
Adds the element at the end of the array
$my_array = "new elemen­t";
Replace the element
$my_ar­ray[0] = "new first elemen­t";
Pops the last element and returns it
array_­pop­($m­y_a­rray);
Push the elements to the end of the array and return its size
array_­pus­h("A­", "­B");
count(­$ar­ray); // Returns the number of elements in the array


- Different types are allowed in an array

Map

Associative array
$my_array = ["panda" => "very cute", "lizard" => "cute", "cockroach" => "not very cute"];

Array function
$about_me = array(
    "fullname" => "Aisle Nevertell",
    "social" => 123456789
);

echo implode($my_array); // Prints only the values
print_r($my_array); // Prints keys and values

$my_array = ["new"] = "new item"; // Adds new element

$favorites = ["favorite_food"=>"pizza", "favorite_place"=>"my dreams", "FAVORITE_CASE"=>"CAPS", "favorite_person"=>"myself"];
 
echo  $favorites["favorite" . "_" . "food"]; 
// Prints: pizza
 
$key =  "favorite_place";
echo  $favorites[$key];  
// Prints: my dreams
 
echo $favorites[strtoupper("favorite_case")];
// Prints: CAPS

unset("my_array["new"]); // Delete the "new" element if exists
key => value
Works as a dictio­nary. Arrays are maps where the key is the index.
Key can be number or charac­ters.

Libraries

<?php include 'foote­r.h­tml­';?>
Includes the HTML file where specified
<?php require 'somef­ile.php'; ?>
Requires the specified file
 

PHP & HTML

<?php
$lucky_number = 5 * 2 - 1;
 
echo "<h1>Your lucky number is ${lucky_number}</h1>";
?>


<?php
function makeHeaderGreeting ($name){
  return "<h1>Hello, ${name}!</h1>";
}
 
echo makeHeaderGreeting("World");
?>

$about_me = [
  "name" => "Aisle Nevertell",
  "birth_year" => 1902,
  "favorite_food" => "pizza"
];

function calculateAge ($person_arr){
  $current_year = date("Y");
  $age = $current_year - $person_arr["birth_year"];
  return $age;
}
?>
<h1>Welcome!</h1>
<h2>About me:</h2>
<?php   
  echo "<h3>Hello! I'm {$about_me["name"]}!</h3>";
  echo "<p> I'm " . calculateAge($about_me). " years old! That's pretty cool, right?</p>";
  echo "<div>What more is there to say? I love {$about_me["favorite_food"]}, and that's pretty much it!</div>";
?>

Data access in Forms

$_GET
Non sensible inform­ation
$_POST
Used to send sensitive inform­ation
$_GET[­'user']
Retrieves the value sent through the user get form
$_isse­t($­_PO­ST[­'se­nd'])
Check if send has a value
When a form is submitted values are received contained in an associ­ative array.