PHP cheat sheet

<?php $_SERVER[‘PHP_SELF’]; ?>

If you use superglobal $_SERVER with PHP_SELF in form’s action=”<?php $_SERVER[‘PHP_SELF’]; ?>”, then it will keep you on the same page.


This is a very useful function to include header.php and footer.php of some other files to make it easier to maintain. Sometimes when used with conditional logic then different header or footer can be served to the user. E.g logged in AND logged out user can have different headers served to them when function include(); is used with conditional logic.


Used to DEFINE constants. In code CONSTANTS must be used without single quotes, but when defining a constant. Single quotes are needed.

define('HOSTNAME', 'localhost');



connecting to MySQL database

$conn = mysqli_connect('localhost', 'root', 'password', 'db_name');

PHP functions I have mastered

  1. ucfirst() // capitilizes only the first letter or the first word of string, the rest will stay the same
  2. ucwords(); // frist letters of all words in a string will be CAPITALIZED;
  3. strtolower(); // makes all letters lower case
  4. strtoupper(); //makes all letters upper case
  5. strlen() // counts the number of characters in string
  6. strrev(); //revirses the string
  7. str_replace(“word”, “replacing word”, $string) // replaces SPACES etc in an a word
  8. in_array();
  9. is_null(); // makes VALUE NULL
  10. explode(); // converts string to an ARRAY
  11. implode(); // converts an array into string
  12. unset($variable); //unsets the VARIABLE and if u echo it out you will get error because that variable has been unset
  13. strcmp(); // string compair function; If 1st string is shorter than 2nd string then value will be NEGATIVE and if 1st is longer than string 2nd string, then value will be POSITIVE number; IF both strings are equal then value will be 0 (ZERO); COMPARISON is done based on ASCII values.
  14. str_shuffle(); // shuffles string and even numbers
  15. strpos(); // (case sensitive) find starting position of a string
  16. stripos(); // (case INSENSITIVE) find starting position of a string
  17. wordwrap($string, 3, “<br>”, true); // breaks all words and sentences at specified length (3 characters);
  18. substr($inputText, $start, $length); // cuts out specified part of a string
  19. is_numeric(“12”); // this doesn’t make NUMERIC values, but it just checks if the string in it is NUMERIC or not; If it is NUMERIC (with or without “”), then it will display 1 as a result; In case it’s a string then it will not ECHO out anything because value will be ZERO and ZERO is usually not echoed out;
  20. intval(); // truns alfanumeric number like (234bs) into integer. In this case just numeric part will be displayed instead of “234bs” it will echo out “234”.
  21. preg_match(/pattern/, string, array); // finds a patter(word) in a string; just one (the 1st one in string); /pattern/i then it will be case insensitive.
    • if used with ” i ” like this /pattern/i then it becomes case insensitive;
    • if used with ” | ” then multiple strings or charackters or numbers can be searched like /a|b|c|9/i
    • if you need to find ALL patterns then used this function instead preg_match_all();
    • if you need to get a range of numbers of letters then used with ” [ ] ” like this /[0-9] | [a-zA-Z]; or even just from a to c like this /[a-c]/ or /[a-hA-H]/
    • ” ^ ” letters or numbers after this mark should NOT BE INCLUDED like this /[^s]/ this will escape all letters ” s “ or exclude numbers like this /[^0-9]/
  22. session_start(); $_SESSION[‘username’]=’heikijapan’; Sessions are SUPERGLOBALS and can be accessed from other files very easily. But before trying to access SESSION by name you should start it. Code below explains it:
    • session_start();
    • echo “Welcome, ” . $_SESSION[‘username’];
  23. array_pop(); // creates a new variable using the last item in the ARRAY
  24. unset(); this CLEARS $variables value, kind of resets it’s value
  25. There are many METHODS for sorting ARRAY VALUES
    • sort($array); // this will sort everything by ASCENDING order
    • rsort($array); // sorts array in DESCENDING order
    • ksort($array); // sorts by KEYS in ASCENDING order
    • krsort($array); // sorts by KEYS in DESCENDING order
  26. $mergedArray($array1, $array2); // this will merge multiple arrays into one new array

foreach (); helps to access every value in an array like this.

// code using foreach loop

 $array = ['Heiki', 2024, 'Japan'];

    foreach ($array as $values) {

        echo $values . '<br>';

// RESULT from this code (all values have been accessed)


Associative array (how to print ARRAY keys only and ARRAY values only)

    $pets = ['name' => 'fury', 'type' => 'cat', 'age' => 5];



foreach(); can also be used with associated arrya’s $keys and $values as well

    foreach ($pets as $keys => $values) {

        echo 'KEY: ' . $keys . ' VALUE: ' . $values . '<br>';