1
1
mirror of https://github.com/namibia/awesome-cheatsheets.git synced 2024-11-22 04:45:16 +00:00

Merge pull request #252 from JuGid/master

More PHP elements
This commit is contained in:
Julien Le Coupanec 2021-12-18 17:01:46 +01:00 committed by GitHub
commit 94a3376844
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -11,9 +11,26 @@ echo ""; // Print a string or type that can be made into a string(I.E int, float
print_r($arr); // Print anything, with type hints for array's and object's
var_dump($arr); // Print anything, with type hints for any value and sizes
/**
* Usefull string manipulation methods
*/
$string = 'Awesome cheatsheets';
str_contains($string, 'cheat'); // Find if the string contains the specified string (PHP >= 8.0)
str_replace('Awesome', 'Bonjour', $string); // Replace all occurence (PHP >= 8.0)
strcmp($string, 'Awesome cheatsheets'); // Compare two strings
strpos($string, 'a', 0); // Get position in the string
str_split($string, 2); // Split the string
strrev($string); // Reverse a string
trim($string); // Strip whitespace from the beginning and end of a string
ucfirst($string); // Make a string's first character uppercase
lcfirst($string); // Make a string's first character lowercase
substr($string, 0, 4); // Return part of a string
/**
* Declaring an Array
*/
// Indexed Array
$arr = array("John", "Doe", "Lorem", "Ipsum");
@ -28,6 +45,16 @@ $arr = array (
array("Ipsum",170,150)
);
// Declaring array with short syntax
$arr = ["John", "Doe", "Lorem", "Ipsum"]; // Indexed Array
$arr = ["John"=>"10", "Doe"=>"200", "Doe"=>"3000", "Ipsum"=>"40000"]; // Associative Array
$arr = [
["John",100,180],
["Doe",150,130],
["Lorem",500,200],
["Ipsum",170,150], // You can have a "," at the end without throwing syntax errors
];
/**
* Sorting an Array
*/
@ -38,6 +65,25 @@ ksort($arr); // Sort associative arrays in ascending order, according to the key
arsort($arr); // Sort associative arrays in descending order, according to the value.
krsort($arr); // Sort associative arrays in descending order, according to the key.
/**
* Conditions
*/
// If/Elseif/Else
if($i > 10) {
} elseif( $i > 100) {
} else {
}
// Ternary
$string = $state == 'Running' ? 'He is running' : 'I don\'t know';
// Null coalescing
$string = $startDate ?? '';
/**
* Ways of looping
*/
@ -81,6 +127,26 @@ switch($arr) {
default:
}
/**
* Match (PHP >= 8.0)
* https://www.php.net/manual/fr/control-structures.match.php
*/
$food = 'apple';
$return_value = match($food) {
'apple', 'appel' => 'An apple',
'banana' => 'A banana',
'applepie' => 'An applepie',
default => 'A fruit'
};
//You can also use it as a conditionnal and throw exceptions
$str = 'Welcome to awesome cheatsheets';
$return_value = match(true) {
str_contains($str, 'Welcome') && str_contains($str ,'to') => 'en-EN',
str_contains($str, 'Bonjour') && str_contains($str, 'sur') => 'fr-FR',
default => throw new Exception('Not a recognized language')
};
/**
* Global variables
* http://php.net/manual/en/language.variables.superglobals.php
@ -97,6 +163,31 @@ $_ENV; // php.ini options
$argv; // Array of terminal arguments (filename included)
$argc; // Number of arguments passed into terminal
/**
* Functions
*/
// Simple function
function name($parameter);
// Function with return type (void, int, float, string, array, object, mixed)
function name($parameter) : void;
// Function with optionnal parameter
function name($parameter = '') : string;
// Function with typed parameter (? means "can be null")
function name(?string $parameter) : ?string;
// Function with union types (PHP >= 8.0)
function name(int|string $parameter1, array $parameter2) : int|string;
// Function call
name('my_parameter');
// Null safe operator (PHP >= 8.0)
$myObject?->getName()?->startWith('A');
/**
* Class
* http://php.net/manual/en/language.oop5.basic.php
@ -368,6 +459,48 @@ class ClassWithLogger
}
/**
* Enums (PHP >=8.1)
* https://www.php.net/manual/fr/language.types.enumerations.php
*/
interface StateCode {
public function stateCode() : int;
}
enum States implements StateCode {
case Running;
case Stopped;
public function stateCode() : int {
return match($this) {
State::Running => '444',
State::Stopped => '666'
};
}
}
/**
* You can also declare backed Enums
*/
enum States : int implements StateCode {
case Running = 1;
case Stopped = 0;
public function stateCode() : int {
return match($this) {
State::Running => '444',
State::Stopped => '666'
};
}
}
/** Enums can be use as a type */
function notify(State $state) {
// ...
}
notify(State::Running);
/**
* PHP Regex.
*/