PHP - Advanced Basics
Advanced Integers
Official doc : Integerscopy to clipboard
$a = 1234; // decimal number $a = -123; // a negative number $a = 0123; // octal number (equivalent to 83 decimal) $a = 0x1A; // hexadecimal number (equivalent to 26 decimal) $a = 0b11111111; // binary number (equivalent to 255 decimal)
Advanced String
Nowdoc
Official doc : NowdocNowdocs are to single-quoted strings what heredocs are to double-quoted strings. A nowdoc is specified similarly to a heredoc, but no parsing is done inside a nowdoc. The construct is ideal for embedding PHP code or other large blocks of text without the need for escaping. It shares some features in common with the SGML construct, in that it declares a block of text which is not for parsing.
Heredoc
Official doc : HeredocA third way to delimit strings is the heredoc syntax: <<<. After this operator, an identifier is provided, then a newline. The string itself follows, and then the same identifier again to close the quotation.
Double quoted
Official doc : Double quotedSequence | Meaning |
---|---|
\n | linefeed (LF or 0x0A (10) in ASCII) |
\r | carriage return (CR or 0x0D (13) in ASCII) |
\t | horizontal tab (HT or 0x09 (9) in ASCII) |
\v | vertical tab (VT or 0x0B (11) in ASCII) (since PHP 5.2.5) |
\e | escape (ESC or 0x1B (27) in ASCII) (since PHP 5.4.4) |
\f | form feed (FF or 0x0C (12) in ASCII) (since PHP 5.2.5) |
\\ | backslash |
\$ | dollar sign |
\" | double-quote |
\[0-7]{1,3} | the sequence of characters matching the regular expression is a character in octal notation, which silently overflows to fit in a byte (e.g. "\400" === "\000") |
\x[0-9A-Fa-f]{1,2} | the sequence of characters matching the regular expression is a character in hexadecimal notation |
\u{[0-9A-Fa-f]+} | the sequence of characters matching the regular expression is a Unicode codepoint, which will be output to the string as that codepoint's UTF-8 representation (added in PHP 7.0.0) |
Variables
Advanced Variables
copy to clipboard#array declaration $array = []; #push to array $array[] = 'new entry'; #secure variable in string echo "${variable[0]}"; #dynamic variable name echo ${'variable' . $i}; #special assign variable with numeric name ${0} = 'test'; #dynamic variable $$var;
Assignment by Reference
Official doc : Assignment by ReferenceAssignment by reference is also supported, using the "$var = &$othervar;" syntax. Assignment by reference means that both variables end up pointing at the same data, and nothing is copied anywhere.
Superglobals Variables
Official doc : Superglobals Variables Superglobals are built-in variables that are always available in all scopes- $GLOBALS
- $_SERVER
- $_GET
- $_POST
- $_FILES
- $_COOKIE
- $_SESSION
- $_REQUEST
- $_ENV
PHP Loose/Stric type comparison
Official doc : PHP type comparison tablesTRUE | FALSE | 1 | 0 | -1 | "1" | "0" | "-1" | NULL | array() | "php" | "" | |
---|---|---|---|---|---|---|---|---|---|---|---|---|
TRUE | TRUE | FALSE | TRUE | FALSE | TRUE | TRUE | FALSE | TRUE | FALSE | FALSE | TRUE | FALSE |
FALSE | FALSE | TRUE | FALSE | TRUE | FALSE | FALSE | TRUE | FALSE | TRUE | TRUE | FALSE | TRUE |
1 | TRUE | FALSE | TRUE | FALSE | FALSE | TRUE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE |
0 | FALSE | TRUE | FALSE | TRUE | FALSE | FALSE | TRUE | FALSE | TRUE | FALSE | TRUE | TRUE |
-1 | TRUE | FALSE | FALSE | FALSE | TRUE | FALSE | FALSE | TRUE | FALSE | FALSE | FALSE | FALSE |
"1" | TRUE | FALSE | TRUE | FALSE | FALSE | TRUE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE |
"0" | FALSE | TRUE | FALSE | TRUE | FALSE | FALSE | TRUE | FALSE | FALSE | FALSE | FALSE | FALSE |
"-1" | TRUE | FALSE | FALSE | FALSE | TRUE | FALSE | FALSE | TRUE | FALSE | FALSE | FALSE | FALSE |
NULL | FALSE | TRUE | FALSE | TRUE | FALSE | FALSE | FALSE | FALSE | TRUE | TRUE | FALSE | TRUE |
array() | FALSE | TRUE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | TRUE | TRUE | FALSE | FALSE |
"php" | TRUE | FALSE | FALSE | TRUE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | TRUE | FALSE |
"" | FALSE | TRUE | FALSE | TRUE | FALSE | FALSE | FALSE | FALSE | TRUE | FALSE | FALSE | TRUE |
TRUE | FALSE | 1 | 0 | -1 | "1" | "0" | "-1" | NULL | array() | "php" | "" | |
---|---|---|---|---|---|---|---|---|---|---|---|---|
TRUE | TRUE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE |
FALSE | FALSE | TRUE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE |
1 | FALSE | FALSE | TRUE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE |
0 | FALSE | FALSE | FALSE | TRUE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE |
-1 | FALSE | FALSE | FALSE | FALSE | TRUE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE |
"1" | FALSE | FALSE | FALSE | FALSE | FALSE | TRUE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE |
"0" | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | TRUE | FALSE | FALSE | FALSE | FALSE | FALSE |
"-1" | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | TRUE | FALSE | FALSE | FALSE | FALSE |
NULL | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | TRUE | FALSE | FALSE | FALSE |
array() | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | TRUE | FALSE | FALSE |
"php" | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | TRUE | FALSE |
"" | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | FALSE | TRUE |
Alternative Control Structures syntax
if/else
copy to clipboardif ( condition ) : statement; endif;copy to clipboard
if ( condition ) : statement1; else: statement2; endif;copy to clipboard
condition ? ifStatementIf : elseStatement;
switch
multiple condition for same statement copy to clipboardswitch (testedValue) { case condition1: case condition2: statement1;break; case condition3: statement3;break; default: defaultStatement; }copy to clipboard
switch (testedValue) : case condition1: case condition2: statement1;break; case condition3: statement3;break; default: defaultStatement; endswitch
isset shorthand
copy to clipboard$foo = $var??'default value'; #shorthand for $foo = isset($var)? $var : 'default value';
Functions
Functions Coercive mode
copy to clipboardfunction foo (string ...$args){ //$args is an array with all arguments passed print_r($args); };
Anonymous functions
Official doc : Anonymous functionscopy to clipboard
$foo = function($arg){ return $arg; }; echo $foo('arg');
Typed anonymous functions with return type and external variable with Assignment by reference
Official doc : functions returning valuescopy to clipboard
$externalVar = true; $foo = function( string $arg ) use ( bool &$externalVar ) : ?string { $externalVar = false; return $externalVar ? $arg : null; }; // will echo false echo $externalVar ? 'true' : 'false'; // will echo true echo $foo('arg')===null ? 'true' : 'false';
Using namespaces: Aliasing/Importing
Official doc : Using namespaces: Aliasing/Importingcopy to clipboard
use some\namespace\{ClassA, ClassB, ClassC as C}; use some\namespace\{ClassA, ClassB, ClassC as C}; use function some\namespace\{fn_a, fn_b, fn_c}; use const some\namespace\{ConstA, ConstB, ConstC};
Exeptions
Official doc : Exeptionscopy to clipboard
function doSomething(){ // throwing an custom exception throw new Exception('Exeption message'); } //trying to do something try { doSomething();} //Catching exception if throwed catch (Exception $exception) { var_dump($exception->getMessage()); }
Exeptions
copy to clipboardError ArithmeticError DivisionByZeroError AssertionError ParseError TypeError ArgumentCountError Exception ClosedGeneratorException DOMException ErrorException IntlException LogicException BadFunctionCallException BadMethodCallException DomainException InvalidArgumentException LengthException OutOfRangeException PharException ReflectionException RuntimeException OutOfBoundsException OverflowException PDOException RangeException UnderflowException UnexpectedValueException SodiumExceptionExeptions list tool
Exeptions list by php version