Adds a new item or items in front of an array.

#php
array_unshift($awesome_array, "new item");
copy

Changes document header and returns document data in JSON format.

#php#header
header('Content-Type: application/json');
copy

Simple class example.

#php
class Shape {

    public $type;
    protected $colour = 'transparent'; // Default value set

    
    // Construct method runs automatically when the Class is called
    public function __construct( $type, $colour ){

        $this->type = $type;
        $this->colour = $colour;

    }

    public function makeSquare(){
        
        $this->type = 'Square';

    }
}

$shape = new Shape( 'Circe', 'Red' );
$shape->makeSquare();
copy

A simple copyrights short code using current year.

#php
<div>&copy;<?php echo date('Y')?> Shortcode.dev </div>
copy

Loop through an array.

#php#loop
$colours_arr = array('orange','blue','purple');

foreach( $colours_arr as $colour){
    echo $colour;
}
copy

Match keyword with the values. This is a new feature in PHP8.

#php#php8
$language_code = "en";

$language = match ($language_code){
    'en' => 'English',
    'es' => 'Spanish',
    'fr' => 'French'
}

echo $language; // returns 'English'
copy

Null coalescing returns the first value if it exists and is not null, otherwise it falls back to the second value.

#php#conditional
$colour = "red";
echo $colour ?? "blue"; // returns "red"
copy

Lets you parse a URL and retrieve parts like scheme, hostname, port, path, query, fragment, username, and password.

#php
$url_to_parse = "https://shortcode.dev/php-cheatsheet?var=hello#php-cheatsheet";
$parsed = parse_url($url_to_parse);

echo $parsed['scheme'];     // https
echo $parsed['hostname'];   // shortcode.dev
echo $parsed['port'];       // ie. 8080
echo $parsed['path'];       // /php-cheatsheet
echo $parsed['query'];      // var=hello
echo $parsed['fragment'];   // php-cheatsheet
copy

Run the following command to get installed PHP version.

#php
php -v
copy

Print value to the view.

#php
$day = "Monday"
print $day;
copy

Print value in a readable format. Useful when fetching large objects or arrays.

#php
print_r($someObject);
copy

Push a new item or items at the end of an array.

#php
array_push($awesome_array, "new item");
copy

This function lets you run a shell command directly from a php file. Output of the command will be returned as a string so it can be displayed too.

#php
/* ls -al returns a list of all folders and files in the directory */
$list_folders = shell_exec('ls -al');

/* Display output */
echo "<pre>$list_folders</pre>";
copy

Lets you format a string and combine it with variables.

#php
$day = date("l");
$year = date("Y");

echo sprintf('Current year: %s. Current day %s', $year, $day);
copy

Ternary operator evaluates first parameter followed by two values. First value is returned when the condition is true otherwise the second value is returned.

#php#conditional
// Condition ? True : False

$hour = 14;
($hour <= 12) ? "Morning" : "Afternoon";
copy

Converts first letter of the string to uppercase.

#php
echo ucfirst("uppercase this string");
// outputs "Uppercase this string"
copy

Return all the data about a variable in a structured format.

#php
var_dump(['Square', 123, true, [ 10, 20, 30 ] ]);

/* Returns:
array(4) {
    [0]=>
    string(6) "Square"
    [1]=>
    int(123)
    [2]=>
    bool(true)
    [3]=>
    array(3) {
      [0]=>
      int(10)
      [1]=>
      int(20)
      [2]=>
      int(30)
    }
  }
*/
copy