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

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

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

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

#php
var_dump($your_variable);
copy
NordVPN NordVPN