From a2457f571b508bd78950c796634db84a0ac35614 Mon Sep 17 00:00:00 2001 From: "nplgdjp@gmx.com" Date: Wed, 18 Jun 2025 19:36:27 +0200 Subject: [PATCH 1/4] add: section PHP Trait --- source/_posts/php.md | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/source/_posts/php.md b/source/_posts/php.md index 6a8c890..72e31ec 100644 --- a/source/_posts/php.md +++ b/source/_posts/php.md @@ -913,6 +913,48 @@ class Cls implements Foo, Bar } ``` +## PHP Trait + +### Trait declaration & Use case {.col-span-3} +```php +// Declare Trait allows reuse of code +// across multiple classes for common characteristics or use cases + +format('Y-m-d H:i:s') . " | " . $message; + } +} + +// Use case : +// Call trait with "use in Class" + +class User { + use Logger; + + public function createUser() { + // User creation logic + $this->log("User created."); + } +} + +class Product { + use Logger; + + public function createProduct() { + // Product creation logic + $this->log("Product created."); + } +} + +$user = new User(); +$user->createUser(); // Output ex: | 2025-06-18 14:06:09 | User created. + +$product = new Product(); +$product->createProduct(); // Output ex: | 2025-06-18 14:06:09 | Product created. +``` ## Miscellaneous ### Basic error handling From 34f7e9429e1aa659d39a4991d33ff08307b48c3c Mon Sep 17 00:00:00 2001 From: "nplgdjp@gmx.com" Date: Wed, 18 Jun 2025 19:43:23 +0200 Subject: [PATCH 2/4] add: section PHP Enums --- source/_posts/php.md | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/source/_posts/php.md b/source/_posts/php.md index 72e31ec..f886128 100644 --- a/source/_posts/php.md +++ b/source/_posts/php.md @@ -955,6 +955,37 @@ $user->createUser(); // Output ex: | 2025-06-18 14:06:09 | User created. $product = new Product(); $product->createProduct(); // Output ex: | 2025-06-18 14:06:09 | Product created. ``` + +## PHP Enums (PHP 8.1) + +### Enum Declaration & Use case {.col-span-3} + +```php + "Waiting for approval.", + Status::Approved => "Your request has been approved.", + Status::Rejected => "Your request has been rejected.", + }; +} + +$currentStatus = Status::Approved; +echo getStatusMessage($currentStatus); // Output : Your request has been approved. +``` + + ## Miscellaneous ### Basic error handling From 0b2802d34eafe1ae6fb24b3e738215a670c97318 Mon Sep 17 00:00:00 2001 From: "nplgdjp@gmx.com" Date: Wed, 18 Jun 2025 19:58:56 +0200 Subject: [PATCH 3/4] add: section PHP Date & Time Handling --- source/_posts/php.md | 87 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) diff --git a/source/_posts/php.md b/source/_posts/php.md index f886128..6285479 100644 --- a/source/_posts/php.md +++ b/source/_posts/php.md @@ -985,6 +985,93 @@ $currentStatus = Status::Approved; echo getStatusMessage($currentStatus); // Output : Your request has been approved. ``` +## Date & Time Handling + +### Current date and time +```php +// DateTime in PHP handles both date and time + +format('Y-m-d H:i:s.u'); + +// Output ex: 2024-04-27 14:35:22.123456 +``` + + +### Creating specific date/time objects +```php +format('Y-m-d'); + +// Output : 2024-04-27 + +// Create a time object +$t = new DateTime('15:30:45'); +echo $t->format('H:i:s'); + +// Output : 15:30:45 + +// Create a datetime object +$dt = new DateTime('2024-04-27 15:30:45'); +echo $dt->format('Y-m-d H:i:s'); + +// Output : 2024-04-27 15:30:45 +``` + +### Converting between date formats +```php +format('Y-m-d H:i:s'); + +// Output : 2024-04-27 14:00:0027 + +// Convert a DateTime object to a string +$formatted_str = $dt_obj->format('d/m/Y H:i'); +echo $formatted_str; + +// Output : 27/04/2024 14:00 +``` +### Timestamps and Unix time {.col-span-2} +```php +setTimestamp($timestamp); +echo $dt_from_timestamp->format('Y-m-d H:i:s'); + +// Output ex : 2025-06-18 13:33:03 +``` + + +### Date difference and timedelta {.col-span-1} +```php +diff($date2); +echo $interval->days; + +// Output : 4 + +// Using DateInterval for date arithmetic +$new_date = clone $date1; +$new_date->add(new DateInterval('P10D')); +echo $new_date->format('Y-m-d'); + +// Output : 2024-05-07 +``` ## Miscellaneous From adfd364a5cd2c484056cfa9fb052f4f6ddd1c73b Mon Sep 17 00:00:00 2001 From: "nplgdjp@gmx.com" Date: Wed, 18 Jun 2025 20:07:03 +0200 Subject: [PATCH 4/4] add: Super Global Variables in Miscellaneous --- source/_posts/php.md | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/source/_posts/php.md b/source/_posts/php.md index 6285479..0d03607 100644 --- a/source/_posts/php.md +++ b/source/_posts/php.md @@ -985,7 +985,7 @@ $currentStatus = Status::Approved; echo getStatusMessage($currentStatus); // Output : Your request has been approved. ``` -## Date & Time Handling +## PHP Date & Time Handling ### Current date and time ```php @@ -1162,6 +1162,25 @@ See: [Regex in PHP](/regex#regex-in-php) | `a` | Write, append | | `a+` | Read and write, append | +### Super Global Variables {.col-span-2} + +| Variable | Description | +|--------------|:-------------------------------------------------------------| +| `$_SERVER` | Holds information about headers, paths, and script locations | +| `$_GET` | Contains data sent via URL parameters (query string) | +| `$_POST` | Contains data sent via HTTP POST method | +| `$_FILES` | Contains information about uploaded files | +| `$_COOKIE` | Contains cookie data | +| `$_SESSION` | Stores session variables | +| `$_REQUEST` | Contains data from `$_GET`, `$_POST`, and `$_COOKIE` | +| `$_ENV` | Contains environment variables | +| `$GLOBALS` | References all global variables available in the script | + +{.left-text} + +Super Global Variables are built-in variables always available in all scopes. + + ### Runtime defined Constants ```php