Age calculate in PHP

$birthDate = "1970-01-05";
$currentDate = date("d-m-Y");
$age = date_diff(date_create($birthDate), date_create($currentDate));
echo "Current age is ".$age->format("%y");
// Current age is 52

PHP_EOL is newline character in a cross-platform-compatible

PHP_EOL = cross-platform-compatible newline character

PHP_EOL is ostensibly used to find the newline character in a cross-platform-compatible way, so it handles DOS/Unix issues.

Note that PHP_EOL represents the endline character for the current system. For instance, it will not find a Windows endline when executed on a unix-like system.

Example (Save the $DataArray to $filename):

file_put_contents($filename, implode(PHP_EOL, $DataArray), LOCK_EX);
Reference:
Adam Bellaire (2008), https://stackoverflow.com/questions/128560/when-do-i-use-the-php-constant-php-eol

Remove empty array elements

// PHP 7.4 and later
print_r(array_filter($linksArray, fn($value) => !is_null($value) && $value !== ''));

// PHP 5.3 and later
print_r(array_filter($linksArray, function($value) { return !is_null($value) && $value !== ''; }));

// PHP < 5.3
print_r(array_filter($linksArray, create_function('$value', 'return $value !== "";')));
Reference:

BoltClock (2020), https://stackoverflow.com/questions/3654295/remove-empty-array-elements

update/upgrade PHP 7.2 to latest version

source : https://askubuntu.com/questions/1146109/how-to-update-upgrade-php-7-2-to-latest-version-safely

sudo add-apt-repository ppa:ondrej/php
sudo apt update
sudo apt install php7.3 # The latest version in this repo for 18.04 is currently php7.4.  
apt-cache search php7.3 | grep php7.3  
apt install $(apt list --installed | grep php7.2- | cut -d'/' -f1 | sed -e 's/7.2/7.3/g') 

自建了 php autoload ,並同時使用 composer 的 autoload,應該注意什麼?

不知道大家有沒有看過下面的 php autoload 教學?

Eric G. Huang 不像樣工程師
PHP系列 – Autoload 自動載入
http://justericgg.logdown.com/posts/196891-php-series-autoload

跟據這個教學,我解決了很多 class include require 檔案引入的困難,而且用了很長時間。

下面提供一些參考資料,讓大家可以分享。

Composer Basic usage: autoloading
https://getcomposer.org/doc/01-basic-usage.md#autoloading

PHP PSR-4 Autoloader 機制
http://blog.tonycube.com/2016/09/php-psr-4-autoloader.html

什麼是 PHP Standards Recommendations(PSR)(PHP標準建議)?
https://www.php-fig.org/psr/

PHP PSR-4 Autoloader 機制
http://blog.tonycube.com/2016/09/php-psr-4-autoloader.html

Composer Basic usage: autoloading
https://getcomposer.org/doc/01-basic-usage.md#autoloading

運用Composer Autoloader

1. 準備自建的 namespace 和 目錄位置

e.g. 
namespace1 = application\control\, 目錄位置 = {user_directory}/application/control 
namespace2 = application\module\, 目錄位置 = {user_directory}/application/module 
namespace3 = application\mouule\sql\, 目錄位置 = {user_directory}/application/module/sql
Class 例子:

<?php
//application/module/config.php

namespace application\module;

class config
{


    function __construct() {
        ...
    }
}

2. 在 composer.json 加入自建的 namespace 和 目錄位置

{
    "autoload": {
        "psr-4": {
            "application\\control\\": "application/control/",
            "application\\module\\": "application/module/",
            "application\\module\\sql\\": "application/module/sql/"
        }
    }
}

3. 進入 console mode,使用用戶權限及用戶的root目錄,執行下面的程式

composer dump-autoload

4. 在需要的php 程式中,頂部加入(因為我的 composer 是放在 /vendor中,請自行改進)

require __DIR__ . '/vendor/autoload.php';