mirror of
https://github.com/namibia/awesome-cheatsheets.git
synced 2024-11-23 21:27:40 +00:00
More PHP elements
This commit is contained in:
parent
cc3cb1bc11
commit
014126e4d7
@ -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
|
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
|
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
|
* Declaring an Array
|
||||||
*/
|
*/
|
||||||
|
|
||||||
// Indexed Array
|
// Indexed Array
|
||||||
$arr = array("John", "Doe", "Lorem", "Ipsum");
|
$arr = array("John", "Doe", "Lorem", "Ipsum");
|
||||||
|
|
||||||
@ -28,6 +45,16 @@ $arr = array (
|
|||||||
array("Ipsum",170,150)
|
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
|
* Sorting an Array
|
||||||
*/
|
*/
|
||||||
@ -81,6 +108,26 @@ switch($arr) {
|
|||||||
default:
|
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
|
* Global variables
|
||||||
* http://php.net/manual/en/language.variables.superglobals.php
|
* http://php.net/manual/en/language.variables.superglobals.php
|
||||||
@ -368,6 +415,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.
|
* PHP Regex.
|
||||||
*/
|
*/
|
||||||
|
Loading…
Reference in New Issue
Block a user