array unshift
Adds a new item or items in front of an array.
array_unshift($awesome_array, "new item");
copy
Adds a new item or items in front of an array.
array_unshift($awesome_array, "new item");
copy
Changes document header and returns document data in JSON format.
header('Content-Type: application/json');
copy
Simple class example.
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.
<div>©<?php echo date('Y')?> Shortcode.dev </div>
copy
Loop through an array.
$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.
$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.
$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.
$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 -v
copy
Print value to the view.
$day = "Monday"
print $day;
copy
Print value in a readable format. Useful when fetching large objects or arrays.
print_r($someObject);
copy
Push a new item or items at the end of an array.
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.
/* 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.
$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.
// Condition ? True : False
$hour = 14;
($hour <= 12) ? "Morning" : "Afternoon";
copy
Converts first letter of the string to uppercase.
echo ucfirst("uppercase this string");
// outputs "Uppercase this string"
copy
Return all the data about a variable in a structured format.
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