mirror of
https://github.com/Fechin/reference.git
synced 2026-11-27 07:58:57 +03:00
fix: trivial formatted posts
Every other file's format which was automatically converted correctly. Each has been manually checked and they should all be visually the same.
This commit is contained in:
97 files changed
+10250
-10850
No files matched your search
+131
-143
@@ -3,21 +3,19 @@ title: PHP
|
||||
date: 2021-01-04 15:23:28
|
||||
background: bg-[#7477a9]
|
||||
tags:
|
||||
- web
|
||||
- web
|
||||
categories:
|
||||
- Programming
|
||||
- Programming
|
||||
intro: |
|
||||
This [PHP](https://www.php.net/manual/en/) cheat sheet provides a reference for quickly looking up the correct syntax for the code you use most frequently.
|
||||
This [PHP](https://www.php.net/manual/en/) cheat sheet provides a reference for quickly looking up the correct syntax for the code you use most frequently.
|
||||
plugins:
|
||||
- copyCode
|
||||
- copyCode
|
||||
---
|
||||
|
||||
|
||||
Getting Started
|
||||
---------------
|
||||
|
||||
## Getting Started
|
||||
|
||||
### hello.php
|
||||
|
||||
```php
|
||||
<?php // begin with a PHP open tag.
|
||||
|
||||
@@ -26,14 +24,15 @@ print("Hello cheatsheets.zip");
|
||||
|
||||
?>
|
||||
```
|
||||
|
||||
PHP run command
|
||||
|
||||
```shell script
|
||||
$ php hello.php
|
||||
```
|
||||
|
||||
|
||||
|
||||
### Variables
|
||||
|
||||
```php
|
||||
$boolean1 = true;
|
||||
$boolean2 = True;
|
||||
@@ -45,12 +44,11 @@ unset($float); // Delete variable
|
||||
$str1 = "How are you?";
|
||||
$str2 = 'Fine, thanks';
|
||||
```
|
||||
|
||||
See: [Types](#php-types)
|
||||
|
||||
|
||||
|
||||
|
||||
### Strings
|
||||
|
||||
```php
|
||||
$url = "cheatsheets.zip";
|
||||
echo "I'm learning PHP at $url";
|
||||
@@ -62,12 +60,11 @@ $hello = "Hello, ";
|
||||
$hello .= "World!";
|
||||
echo $hello; # => Hello, World!
|
||||
```
|
||||
|
||||
See: [Strings](#php-strings)
|
||||
|
||||
|
||||
|
||||
|
||||
### Arrays
|
||||
|
||||
```php
|
||||
$num = [1, 3, 5, 7, 9];
|
||||
$num[5] = 11;
|
||||
@@ -75,11 +72,11 @@ unset($num[2]); // Delete variable
|
||||
print_r($num); # => 1 3 7 9 11
|
||||
echo count($num); # => 5
|
||||
```
|
||||
|
||||
See: [Arrays](#php-arrays)
|
||||
|
||||
|
||||
|
||||
### Operators
|
||||
|
||||
```php
|
||||
$x = 1;
|
||||
$y = 2;
|
||||
@@ -87,11 +84,13 @@ $y = 2;
|
||||
$sum = $x + $y;
|
||||
echo $sum; # => 3
|
||||
```
|
||||
|
||||
See: [Operators](#php-operators)
|
||||
|
||||
|
||||
### Include {.row-span-3}
|
||||
|
||||
#### vars.php
|
||||
|
||||
```php
|
||||
<?php // begin with a PHP open tag.
|
||||
$fruit = 'apple';
|
||||
@@ -99,7 +98,9 @@ echo "I was imported";
|
||||
return 'Anything you like.';
|
||||
?>
|
||||
```
|
||||
|
||||
#### test.php
|
||||
|
||||
```php
|
||||
<?php
|
||||
include 'vars.php';
|
||||
@@ -122,9 +123,8 @@ echo $result; # => Anything you like.
|
||||
?>
|
||||
```
|
||||
|
||||
|
||||
|
||||
### Functions
|
||||
|
||||
```php
|
||||
function add($num1, $num2 = 1) {
|
||||
return $num1 + $num2;
|
||||
@@ -132,12 +132,11 @@ function add($num1, $num2 = 1) {
|
||||
echo add(10); # => 11
|
||||
echo add(10, 5); # => 15
|
||||
```
|
||||
|
||||
See: [Functions](#php-functions)
|
||||
|
||||
### Comments
|
||||
|
||||
|
||||
|
||||
### Comments
|
||||
```php
|
||||
# This is a one line shell-style comment
|
||||
|
||||
@@ -147,23 +146,19 @@ See: [Functions](#php-functions)
|
||||
yet another line of comment */
|
||||
```
|
||||
|
||||
|
||||
|
||||
### Constants
|
||||
|
||||
```php
|
||||
const MY_CONST = "hello";
|
||||
|
||||
echo MY_CONST; # => hello
|
||||
|
||||
# => MY_CONST is: hello
|
||||
echo 'MY_CONST is: ' . MY_CONST;
|
||||
echo 'MY_CONST is: ' . MY_CONST;
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
### Classes
|
||||
|
||||
```php
|
||||
class Student {
|
||||
public function __construct($name) {
|
||||
@@ -172,17 +167,13 @@ class Student {
|
||||
}
|
||||
$alex = new Student("Alex");
|
||||
```
|
||||
|
||||
See: [Classes](#php-classes)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
PHP Types
|
||||
---------------
|
||||
|
||||
## PHP Types
|
||||
|
||||
### Boolean {.row-span-2}
|
||||
|
||||
```php
|
||||
$boolean1 = true;
|
||||
$boolean2 = TRUE;
|
||||
@@ -192,12 +183,11 @@ $boolean4 = FALSE;
|
||||
$boolean5 = (boolean) 1; # => true
|
||||
$boolean6 = (boolean) 0; # => false
|
||||
```
|
||||
|
||||
Boolean are case-insensitive
|
||||
|
||||
|
||||
|
||||
|
||||
### Integer {.row-span-2}
|
||||
|
||||
```php
|
||||
$int1 = 28; # => 28
|
||||
$int2 = -32; # => -32
|
||||
@@ -208,23 +198,27 @@ $int5 = 0b101; # => 5 (binary)
|
||||
# => 2000100000 (decimal, PHP 7.4.0)
|
||||
$int6 = 2_000_100_000;
|
||||
```
|
||||
|
||||
See also: [Integers](https://www.php.net/manual/en/language.types.integer.php)
|
||||
|
||||
|
||||
### Strings
|
||||
|
||||
```php
|
||||
echo 'this is a simple string';
|
||||
```
|
||||
|
||||
See: [Strings](#php-strings)
|
||||
|
||||
### Arrays
|
||||
|
||||
```php
|
||||
$arr = array("hello", "world", "!");
|
||||
```
|
||||
|
||||
See: [Arrays](#php-arrays)
|
||||
|
||||
|
||||
### Float (Double)
|
||||
|
||||
```php
|
||||
$float1 = 1.234;
|
||||
$float2 = 1.2e7;
|
||||
@@ -237,9 +231,8 @@ $float5 = 1 + "10.5"; # => 11.5
|
||||
$float6 = 1 + "-1.3e3"; # => -1299
|
||||
```
|
||||
|
||||
|
||||
|
||||
### Null
|
||||
|
||||
```php
|
||||
$a = null;
|
||||
$b = 'Hello php!';
|
||||
@@ -252,8 +245,8 @@ $a === null # => false
|
||||
is_null($a) # => false
|
||||
```
|
||||
|
||||
|
||||
### Iterables
|
||||
|
||||
```php
|
||||
function bar(): iterable {
|
||||
return [1, 2, 3];
|
||||
@@ -265,16 +258,13 @@ function gen(): iterable {
|
||||
}
|
||||
foreach (bar() as $value) {
|
||||
echo $value; # => 123
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
PHP Strings
|
||||
---------------
|
||||
|
||||
|
||||
## PHP Strings
|
||||
|
||||
### String
|
||||
|
||||
```php
|
||||
# => '$String'
|
||||
$sgl_quotes = '$String';
|
||||
@@ -289,10 +279,8 @@ $escaped = "a \t tab character.";
|
||||
$unescaped = 'a slash and a t: \t';
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
### Multi-line
|
||||
|
||||
```php
|
||||
$str = "foo";
|
||||
|
||||
@@ -309,9 +297,8 @@ $str
|
||||
END;
|
||||
```
|
||||
|
||||
|
||||
|
||||
### Manipulation
|
||||
|
||||
```php
|
||||
$s = "Hello Phper";
|
||||
echo strlen($s); # => 11
|
||||
@@ -326,20 +313,21 @@ echo strtolower($s); # => hello phper
|
||||
echo strpos($s, "l"); # => 2
|
||||
var_dump(strpos($s, "L")); # => false
|
||||
```
|
||||
|
||||
See: [String Functions](https://www.php.net/manual/en/ref.strings.php)
|
||||
|
||||
|
||||
|
||||
PHP Arrays
|
||||
---------------
|
||||
## PHP Arrays
|
||||
|
||||
### Defining {.row-span-2}
|
||||
|
||||
```php
|
||||
$a1 = ["hello", "world", "!"]
|
||||
$a2 = array("hello", "world", "!");
|
||||
$a3 = explode(",", "apple,pear,peach");
|
||||
```
|
||||
|
||||
#### Mixed int and string keys
|
||||
|
||||
```php
|
||||
$array = array(
|
||||
"foo" => "bar",
|
||||
@@ -349,7 +337,9 @@ $array = array(
|
||||
);
|
||||
var_dump($array);
|
||||
```
|
||||
|
||||
#### Short array syntax
|
||||
|
||||
```php
|
||||
$array = [
|
||||
"foo" => "bar",
|
||||
@@ -357,11 +347,10 @@ $array = [
|
||||
];
|
||||
```
|
||||
|
||||
|
||||
|
||||
### Multi array
|
||||
|
||||
```php
|
||||
$multiArray = [
|
||||
$multiArray = [
|
||||
[1, 2, 3],
|
||||
[4, 5, 6],
|
||||
[7, 8, 9],
|
||||
@@ -372,8 +361,8 @@ print_r($multiArray[0][1]) # => 2
|
||||
print_r($multiArray[0][2]) # => 3
|
||||
```
|
||||
|
||||
|
||||
### Multi type {.row-span-2}
|
||||
|
||||
```php
|
||||
$array = array(
|
||||
"foo" => "bar",
|
||||
@@ -389,15 +378,14 @@ $array = array(
|
||||
var_dump($array["foo"]);
|
||||
|
||||
# => int(24)
|
||||
var_dump($array[42]);
|
||||
var_dump($array[42]);
|
||||
|
||||
# => string(3) "foo"
|
||||
var_dump($array["multi"]["dim"]["a"]);
|
||||
```
|
||||
|
||||
|
||||
|
||||
### manipulation
|
||||
|
||||
```php
|
||||
$arr = array(5 => 1, 12 => 2);
|
||||
$arr[] = 56; // Append
|
||||
@@ -406,9 +394,11 @@ sort($arr); // Sort
|
||||
unset($arr[5]); // Remove
|
||||
unset($arr); // Remove all
|
||||
```
|
||||
|
||||
See: [Array Functions](https://www.php.net/manual/en/ref.array.php)
|
||||
|
||||
### Indexing iteration
|
||||
|
||||
```php
|
||||
$array = array('a', 'b', 'c');
|
||||
$count = count($array);
|
||||
@@ -419,6 +409,7 @@ for ($i = 0; $i < $count; $i++) {
|
||||
```
|
||||
|
||||
### Value iteration
|
||||
|
||||
```php
|
||||
$colors = array('red', 'blue', 'green');
|
||||
|
||||
@@ -426,7 +417,9 @@ foreach ($colors as $color) {
|
||||
echo "Do you like $color?\n";
|
||||
}
|
||||
```
|
||||
|
||||
### Key iteration
|
||||
|
||||
```php
|
||||
$arr = ["foo" => "bar", "bar" => "foo"];
|
||||
|
||||
@@ -438,6 +431,7 @@ foreach ( $arr as $key => $value )
|
||||
```
|
||||
|
||||
### Concatenate arrays
|
||||
|
||||
```php
|
||||
$a = [1, 2];
|
||||
$b = [3, 4];
|
||||
@@ -448,6 +442,7 @@ $result = [...$a, ...$b];
|
||||
```
|
||||
|
||||
### Into functions
|
||||
|
||||
```php
|
||||
$array = [1, 2];
|
||||
|
||||
@@ -458,8 +453,8 @@ function foo(int $a, int $b) {
|
||||
foo(...$array);
|
||||
```
|
||||
|
||||
|
||||
### Splat Operator
|
||||
|
||||
```php
|
||||
function foo($first, ...$other) {
|
||||
var_dump($first); # => a
|
||||
@@ -470,16 +465,12 @@ foo('a', 'b', 'c' /*, ...*/ );
|
||||
function foo($first, string ...$other){}
|
||||
```
|
||||
|
||||
|
||||
|
||||
PHP Operators {.cols-4}
|
||||
---------------
|
||||
|
||||
|
||||
## PHP Operators {.cols-4}
|
||||
|
||||
### Arithmetic
|
||||
|
||||
| - | - |
|
||||
|------|----------------|
|
||||
| ---- | -------------- |
|
||||
| `+` | Addition |
|
||||
| `-` | Subtraction |
|
||||
| `*` | Multiplication |
|
||||
@@ -488,18 +479,19 @@ PHP Operators {.cols-4}
|
||||
| `**` | Exponentiation |
|
||||
|
||||
### Assignment
|
||||
|
||||
| - | - |
|
||||
|----------|---------------------|
|
||||
| -------- | ------------------- |
|
||||
| `a += b` | Same as `a = a + b` |
|
||||
| `a -= b` | Same as `a = a – b` |
|
||||
| `a *= b` | Same as `a = a * b` |
|
||||
| `a /= b` | Same as `a = a / b` |
|
||||
| `a %= b` | Same as `a = a % b` |
|
||||
|
||||
|
||||
### Comparison {.row-span-2}
|
||||
|
||||
| - | - |
|
||||
|-------|------------------------------|
|
||||
| ----- | ---------------------------- |
|
||||
| `==` | Equal |
|
||||
| `===` | Identical |
|
||||
| `!=` | Not equal |
|
||||
@@ -512,19 +504,18 @@ PHP Operators {.cols-4}
|
||||
| `<=>` | Less than/equal/greater than |
|
||||
|
||||
### Logical
|
||||
|
||||
| - | - |
|
||||
|-------|--------------|
|
||||
| ----- | ------------ | --- | --- |
|
||||
| `and` | And |
|
||||
| `or` | Or |
|
||||
| `xor` | Exclusive or |
|
||||
| `!` | Not |
|
||||
| `&&` | And |
|
||||
| `||` | Or |
|
||||
|
||||
|
||||
|
||||
| ` | | ` | Or |
|
||||
|
||||
### Arithmetic {.col-span-2}
|
||||
|
||||
```php
|
||||
// Arithmetic
|
||||
$sum = 1 + 1; // 2
|
||||
@@ -540,25 +531,21 @@ echo ++$num; // Prints 3 (increments before evaluation)
|
||||
$num /= $float; // Divide and assign the quotient to $num
|
||||
```
|
||||
|
||||
|
||||
|
||||
### Bitwise
|
||||
|
||||
| - | - |
|
||||
|------|--------------------|
|
||||
| ---- | ------------------ | ----------------- |
|
||||
| `&` | And |
|
||||
| `|` | Or (inclusive or) |
|
||||
| ` | ` | Or (inclusive or) |
|
||||
| `^` | Xor (exclusive or) |
|
||||
| `~` | Not |
|
||||
| `<<` | Shift left |
|
||||
| `>>` | Shift right |
|
||||
|
||||
|
||||
|
||||
|
||||
PHP Conditionals
|
||||
---------------
|
||||
## PHP Conditionals
|
||||
|
||||
### If elseif else
|
||||
|
||||
```php
|
||||
$a = 10;
|
||||
$b = 20;
|
||||
@@ -572,14 +559,14 @@ if ($a > $b) {
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
### Switch
|
||||
|
||||
```php
|
||||
$x = 0;
|
||||
switch ($x) {
|
||||
case '0':
|
||||
print "it's zero";
|
||||
break;
|
||||
break;
|
||||
case 'two':
|
||||
case 'three':
|
||||
// do something
|
||||
@@ -590,6 +577,7 @@ switch ($x) {
|
||||
```
|
||||
|
||||
### Ternary operator
|
||||
|
||||
```php
|
||||
# => Does
|
||||
print (false ? 'Not' : 'Does');
|
||||
@@ -607,6 +595,7 @@ echo $b ?? 'b is unset';
|
||||
```
|
||||
|
||||
### Match
|
||||
|
||||
```php
|
||||
$statusCode = 500;
|
||||
$message = match($statusCode) {
|
||||
@@ -617,10 +606,11 @@ $message = match($statusCode) {
|
||||
};
|
||||
echo $message; # => server error
|
||||
```
|
||||
|
||||
See: [Match](https://www.php.net/manual/en/control-structures.match.php)
|
||||
|
||||
|
||||
### Match expressions
|
||||
|
||||
```php
|
||||
$age = 23;
|
||||
|
||||
@@ -634,12 +624,10 @@ $result = match (true) {
|
||||
echo $result; # => young adult
|
||||
```
|
||||
|
||||
|
||||
PHP Loops
|
||||
---------------
|
||||
|
||||
## PHP Loops
|
||||
|
||||
### while
|
||||
|
||||
```php
|
||||
$i = 1;
|
||||
# => 12345
|
||||
@@ -649,6 +637,7 @@ while ($i <= 5) {
|
||||
```
|
||||
|
||||
### do while
|
||||
|
||||
```php
|
||||
$i = 1;
|
||||
# => 12345
|
||||
@@ -657,8 +646,8 @@ do {
|
||||
} while ($i <= 5);
|
||||
```
|
||||
|
||||
|
||||
### for i
|
||||
|
||||
```php
|
||||
# => 12345
|
||||
for ($i = 1; $i <= 5; $i++) {
|
||||
@@ -667,6 +656,7 @@ for ($i = 1; $i <= 5; $i++) {
|
||||
```
|
||||
|
||||
### break
|
||||
|
||||
```php
|
||||
# => 123
|
||||
for ($i = 1; $i <= 5; $i++) {
|
||||
@@ -678,6 +668,7 @@ for ($i = 1; $i <= 5; $i++) {
|
||||
```
|
||||
|
||||
### continue
|
||||
|
||||
```php
|
||||
# => 1235
|
||||
for ($i = 1; $i <= 5; $i++) {
|
||||
@@ -689,6 +680,7 @@ for ($i = 1; $i <= 5; $i++) {
|
||||
```
|
||||
|
||||
### foreach
|
||||
|
||||
```php
|
||||
$a = ['foo' => 1, 'bar' => 2];
|
||||
# => 12
|
||||
@@ -696,15 +688,13 @@ foreach ($a as $k) {
|
||||
echo $k;
|
||||
}
|
||||
```
|
||||
|
||||
See: [Array iteration](#php-value-iteration)
|
||||
|
||||
|
||||
|
||||
PHP Functions
|
||||
---------------
|
||||
|
||||
## PHP Functions
|
||||
|
||||
### Returning values
|
||||
|
||||
```php
|
||||
function square($x)
|
||||
{
|
||||
@@ -715,6 +705,7 @@ echo square(4); # => 16
|
||||
```
|
||||
|
||||
### Return types
|
||||
|
||||
```php
|
||||
// Basic return type declaration
|
||||
function sum($a, $b): float {/*...*/}
|
||||
@@ -726,6 +717,7 @@ function getC(): C { return new C; }
|
||||
```
|
||||
|
||||
### Nullable return types
|
||||
|
||||
```php
|
||||
// Available in PHP 7.1
|
||||
function nullOrString(int $v) : ?string
|
||||
@@ -735,9 +727,11 @@ function nullOrString(int $v) : ?string
|
||||
echo nullOrString(3); # => odd
|
||||
var_dump(nullOrString(4)); # => NULL
|
||||
```
|
||||
|
||||
See: [Nullable types](https://www.php.net/manual/en/migration71.new-features.php)
|
||||
|
||||
### Void functions
|
||||
|
||||
```php
|
||||
// Available in PHP 7.1
|
||||
function voidFunction(): void
|
||||
@@ -750,6 +744,7 @@ voidFunction(); # => Hello
|
||||
```
|
||||
|
||||
### Variable functions
|
||||
|
||||
```php
|
||||
function bar($arg = '')
|
||||
{
|
||||
@@ -760,11 +755,8 @@ $func = 'bar';
|
||||
$func('test'); # => In bar(); arg: test
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
### Anonymous functions
|
||||
|
||||
```php
|
||||
$greet = function($name)
|
||||
{
|
||||
@@ -775,10 +767,8 @@ $greet('World'); # => Hello World
|
||||
$greet('PHP'); # => Hello PHP
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
### Recursive functions
|
||||
|
||||
```php
|
||||
function recursion($x)
|
||||
{
|
||||
@@ -790,9 +780,8 @@ function recursion($x)
|
||||
recursion(1); # => 1234
|
||||
```
|
||||
|
||||
|
||||
|
||||
### Default parameters
|
||||
|
||||
```php
|
||||
function coffee($type = "cappuccino")
|
||||
{
|
||||
@@ -806,12 +795,11 @@ echo coffee(null);
|
||||
echo coffee("espresso");
|
||||
```
|
||||
|
||||
|
||||
|
||||
### Arrow Functions
|
||||
|
||||
```php
|
||||
$y = 1;
|
||||
|
||||
|
||||
$fn1 = fn($x) => $x + $y;
|
||||
|
||||
// equivalent to using $y by value:
|
||||
@@ -822,14 +810,10 @@ echo $fn1(5); # => 6
|
||||
echo $fn2(5); # => 6
|
||||
```
|
||||
|
||||
|
||||
|
||||
|
||||
PHP Classes
|
||||
---------------
|
||||
|
||||
## PHP Classes
|
||||
|
||||
### Constructor
|
||||
|
||||
```php
|
||||
class Student {
|
||||
public function __construct($name) {
|
||||
@@ -844,6 +828,7 @@ $alex->print(); # => Name: Alex
|
||||
```
|
||||
|
||||
### Inheritance
|
||||
|
||||
```php
|
||||
class ExtendClass extends SimpleClass
|
||||
{
|
||||
@@ -860,6 +845,7 @@ $extended->displayVar();
|
||||
```
|
||||
|
||||
### Classes variables {.row-span-2}
|
||||
|
||||
```php
|
||||
class MyClass
|
||||
{
|
||||
@@ -879,18 +865,19 @@ class MyClass
|
||||
protected $var6 = 'pro';
|
||||
|
||||
// The class only
|
||||
private $var7 = 'pri';
|
||||
private $var7 = 'pri';
|
||||
}
|
||||
```
|
||||
|
||||
Access statically
|
||||
|
||||
```php
|
||||
echo MyClass::MY_CONST; # => value
|
||||
echo MyClass::$staticVar; # => static
|
||||
```
|
||||
|
||||
|
||||
|
||||
### Magic Methods
|
||||
|
||||
```php
|
||||
class MyClass
|
||||
{
|
||||
@@ -907,10 +894,10 @@ class MyClass
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
### Interface
|
||||
|
||||
```php
|
||||
interface Foo
|
||||
interface Foo
|
||||
{
|
||||
public function doSomething();
|
||||
}
|
||||
@@ -918,17 +905,17 @@ interface Bar
|
||||
{
|
||||
public function doSomethingElse();
|
||||
}
|
||||
class Cls implements Foo, Bar
|
||||
class Cls implements Foo, Bar
|
||||
{
|
||||
public function doSomething() {}
|
||||
public function doSomethingElse() {}
|
||||
}
|
||||
```
|
||||
|
||||
Miscellaneous
|
||||
-------------
|
||||
## Miscellaneous
|
||||
|
||||
### Basic error handling
|
||||
|
||||
```php
|
||||
try {
|
||||
// Do something
|
||||
@@ -940,6 +927,7 @@ try {
|
||||
```
|
||||
|
||||
### Exception in PHP 8.0 {.col-span-2}
|
||||
|
||||
```php {.wrap}
|
||||
$nullableValue = null;
|
||||
|
||||
@@ -952,12 +940,15 @@ try {
|
||||
```
|
||||
|
||||
### Custom exception {.row-span-2}
|
||||
|
||||
```php
|
||||
class MyException extends Exception {
|
||||
// do something
|
||||
}
|
||||
```
|
||||
|
||||
Usage
|
||||
|
||||
```php
|
||||
try {
|
||||
$condition = true;
|
||||
@@ -970,6 +961,7 @@ try {
|
||||
```
|
||||
|
||||
### Nullsafe Operator {.row-span-2}
|
||||
|
||||
```php
|
||||
// As of PHP 8.0.0, this line:
|
||||
$result = $repo?->getUser(5)?->name;
|
||||
@@ -986,19 +978,22 @@ if (is_null($repo)) {
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
See also: [Nullsafe Operator](https://wiki.php.net/rfc/nullsafe_operator)
|
||||
|
||||
### Regular expressions
|
||||
|
||||
```php
|
||||
$str = "Visit Quickref.me";
|
||||
echo preg_match("/qu/i", $str); # => 1
|
||||
```
|
||||
|
||||
See: [Regex in PHP](/regex#regex-in-php)
|
||||
|
||||
### fopen() mode
|
||||
|
||||
| - | - |
|
||||
|------|--------------------------|
|
||||
| ---- | ------------------------ |
|
||||
| `r` | Read |
|
||||
| `r+` | Read and write, prepend |
|
||||
| `w` | Write, truncate |
|
||||
@@ -1006,9 +1001,8 @@ See: [Regex in PHP](/regex#regex-in-php)
|
||||
| `a` | Write, append |
|
||||
| `a+` | Read and write, append |
|
||||
|
||||
|
||||
|
||||
### Runtime defined Constants
|
||||
|
||||
```php
|
||||
define("CURRENT_DATE", date('Y-m-d'));
|
||||
|
||||
@@ -1016,16 +1010,10 @@ define("CURRENT_DATE", date('Y-m-d'));
|
||||
echo CURRENT_DATE; # => 2021-01-05
|
||||
|
||||
# => CURRENT_DATE is: 2021-01-05
|
||||
echo 'CURRENT_DATE is: ' . CURRENT_DATE;
|
||||
echo 'CURRENT_DATE is: ' . CURRENT_DATE;
|
||||
```
|
||||
|
||||
|
||||
Also see
|
||||
-------
|
||||
## Also see
|
||||
|
||||
- [PHP Docs](https://www.php.net/manual/en/index.php)
|
||||
- [Learn X in Y minutes](https://learnxinyminutes.com/docs/php/)
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user