Results: 133
Handle fatal errors CODE
PHP 7.0 + can handle fatal errors without terminating PHP process. It means we can catch the fatal error using
catch
block
try {
    undefinedFunction();
} catch (Error $e) {
    echo 'Fatal error occurred: ' . $e->getMessage();
}
After running the above code on PHP 7+ the result will be:
Fatal error occurred: Call to undefined function undefinedFunction()
If we run the same code on PHP 5.6, we will get the following result:
<br />
<b>Fatal error</b>:  Call to undefined function undefinedFunction() in <b>[...][...]</b> on line <b>5</b><br />
Secure password rules
We need to follow the rules if we want our passwords to be strong enough
1. Contains at least 8 characters
2. Contains at least one uppercase letter
3. Contains at least one lowercase letter
4. Contains at least one number
5. Contains at least one special character
calculate age based on the provided date of birth
function age($dob) {
    // Seconds in a year
    $seconds = 31556926;

    // Calc. age based on the provided date_of_birth val
    $age = floor((time() - strtotime($dob)) / $seconds);

    return $age;
}
$Datetime_object = DateTime::createFromFormat('Y-m-d', "2020-12-23");
short_open_tag
If we want to use short open tag
<?
instead of
<?php
then we must configure
short_open_tag
setting in
php.ini
to
on
<?
echo '"short_open_tag" should be ON if we want to use short tags ' ;
?>
abstract class AbstractClass
{
    // Force Extending class to define this method
    abstract protected function getValue();
    abstract protected function prefixValue($prefix);

    // Common method
    public function printOut() {
        print $this->getValue() . "\n";
    }
}
About abstract class:
Classes defined as abstract cannot be instantiated
Any class that contains at least one abstract method must also be abstract
Methods defined as abstract simply declare the method's signature - they cannot define the implementation
Use static property inside and outside of a class
Use static property inside a class
echo self::$someProperty;
Outside a class
Wheather::$someProperty;
call static method inside and outside of a class
Call the method inside a class
self::celsiusToFarenheit(20);
Outside a class
Wheather::celsiusToFarenheit(20);
Main benefit of using getters and setters is to filter & check values according to current needs
Magic method
__destruct
gets called when we delete an object manually. If we don't delete the object during the code execution, it will be called automatically at the end of the code execution
class User {
    public $username;
    public $friends = ['Tom','David'];

    function __construct($name) {
        $this->username = $name;
        print $this->username . "'s object is created'\n";
    }

    function __destruct() {
        print $this->username . "'s object is deleted'\n";
    }
}
$user1 = new User('George');
$user2 = new User('David');
echo $user1->username . "\n";
Results: 133