Cheat Sheet: PHP

Posted by admin at January 5, 2021

PHP is the longest language I have worked on out of the bunch. I started out using it for building websites back in the day. These days, I mostly employ the language for building backend APIs for database applications. Below are just the basics on PHP, for reference purposes only.

BOF and EOF
<?php /* start and end the code with these tags... */ ?>
Documentation
/**
  * Function Name
  * A description of the what the function is doing
  * @param nameOfParameter1 data type
  * ...
  * @return data type
  */
Single line comments
// or #
Multi line comments
/* ... */
EOL delimiter
;
Basic Output
echo "BlinkWiki";
  echo("BlinkWiki");
DATATYPES
Integer
// numerical value from -2billion to +2billion, with no decimals
  int $count;
Float
// numerical value with decimal places
  float $count = 20.51;
String
// Collection of characters enclosed in quotes
  string $message;
Boolean
// logical variable that has TRUE or FALSE as value
  bool $isReady = true;
Array
// collection of items that can be referenced by indices
  $words = array("Hello", "BlinkWiki");
Object
// instance of a class
  $blinkWiki = new BlinkWiki();
INITIALIZATION
Naming variables
$variableName
Naming constants
CONSTANT_NAME
Initializing a constant
define("PI", 3.14);
Initializing an integer
$count = 36;
Initializing a string
$message = "Hello BlinkWiki";
String delimiters
// Single quotes
  $message = 'Hello BlinkWiki'; 
  // Double quotes
  $message = "Hello BlinkWiki";
  // Multiline
  $message = <<<Hello and welcome to BlinkWiki:
  This article is on cheat sheets and language comparisons.
  >>>;
  // Execute the string as shell command
  $message = `Hello and welcome to BlinkWiki:
  This article is on cheat sheets and language comparisons.`;
Initializing an integer
$count = 23;
Initializing an array
$messages = [
      "Hello",
      "Welcome to BlinkWiki",
      "Here is a cheatsheet"
  ];
  $scores = array(54, 5, 6, 60, 9, 40, 54);
OPERATORS
Operators
+ // Addition
  - // Subtraction
  * // Multiplication
  / // Division
  % // Modulus division
  ** // Raised to power
  = Equation
Logical Operators
|| // Logical OR
  && // Logical AND
  == // Logical Equation
  === // Is identical
  != // Logical Inequality
  <> // Logical Inequality
  !=== // Is not identical
  < // Less than
  > // Greater than
  <= // Less than or equal to 
  >= // Greater than or equal to
Assignment Operators
+= // Add and assign
  -= // Subtract and assign
  *= // Multiply and assign
  /= // Divide and assign
  %= // Complete Modulus division and assign
Increments and Decrements
++$count // Increase count and then return it
  --$count // Decrease count and then return it
  $count++ // Return count and then increase it
  $count-- // Return count and then decrease it
String Operators
Concatenation
$message = "Blink" . "Wiki"; // returns `BlinkWiki`
  $message .= " Cheat Sheets"; // returns 'BlinkWiki Code Sheets'
CONDITIONALS
If ... ElseIf ... Else
if (condition1) {
    // run this if condition1 was met
  } else if (condition2) {
    // run this if condition2 was met
  } else {
    // run this if neither condition1 nor consition2 were met
  }
Switch ... Case
switch ($variable) {
    case "One":
      // $variable == "One"
      break;
    case "Two":
      // $variable == "Two"
      break;
    case "Three":
      // $variable == "Three"
      break;
    default:
      // $variable is not "One", "Two" or "Three"
  }
LOOP CONSTRUCTS
While
$i = 0;
  // while this condition is still being met
  while ($i < 3) {
    // TODO: provide the logic
    $i++;
    echo $i;
  }
Do While
$i = 0;
  // start the loop
  do {
    // TODO: provide the logic
    $i++;
    echo $i;
    // stop the loop when the condition below has been met
  } while ($i < 3);
Foreach
// define a collection
  $collection = array(0,1,2,3);
  // start a loop to go through every item in a collection
  foreach ($collection as $number) {
    echo $number;
  }
For
// start the loop from $i = 0 and stop when $i<3
  for ($i=0; $i<3, $i++) {
    echo $i;
  }
CONSTRUCTS
Function
function greet() {
    // TODO function logic goes here ...
  }
Function returning a variable
function greet($parameter) {
    // TODO function logic goes here ...
    return "Hello BlinkWiki";
  }
Function with Parameter
function greet($parameter) {
    // TODO function logic goes here ...
  }
Function with Parameter
  and default value
function greet($parameter="BlinkWiki") {
    // TODO function logic goes here ...
    return "Hello " . $parameter;
  }
Function with unknown 
  number of parameters
function greet(...$parameters) {
    // TODO return the second parameter provided ...
    return $parameter[2];
  }
HANDLING ERRORS
Try ... Throw ... Catch
try {
     // TODO: code that throws an exception on condition
     if (exception_condition == true) {
       // throw an exception
       throw new Exception("There was a problem running this...");
     }
     // catch the exception
  } catch (Exception $e) {
     // TODO: inform user of the exception
  }

   0 likes

Suggested Read