Results: 132
Difference between
empty
and
isset
$var = 0;

// Evaluates to true because $var is empty
if (empty($var)) {
    echo '$var is either 0, empty, or not set at all';
}

// Evaluates as true because $var is set
if (isset($var)) {
    echo '$var is set even though it is empty';
}
The following values are considered to be false:
""
(an empty string)
0
(0 as an integer)
0.0
(0 as a float)
"0"
(0 as a string)
NULL
FALSE
array()
(an empty array)
Calendar CODE

// Example usage:
$year = 2023;
$month = 1; // May

function generateMonthArray($year, $month) {
    $numDays = cal_days_in_month(CAL_GREGORIAN, $month, $year);
    $firstDay = date("N", strtotime("$year-$month-01")); // 1 = Monday, 7 = Sunday

    $weekdays = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'];

    $monthArray = array_fill_keys($weekdays, []);

    for ($day = 1; $day <= $numDays; $day++) {
        $weekday = ($firstDay + $day - 2) % 7; // Adjust to start from Monday
        $monthArray[$weekdays[$weekday]][] = $day;
    }
    
    foreach ($monthArray as &$weekDays) {
    	if ($weekDays[0]!=1) {
    		array_unshift($weekDays, '');
    	} else {
    		break;
    	}
    }

    return $monthArray;
}

$result = generateMonthArray($year, $month);

// Print the result
foreach ($result as $weekday => $days) {
    echo $weekday . "\t" . implode("\t", $days) . "\n";
}
Functions don't need to be defined before they are called. In this example, function
printMe()
is defined after it's called but it works without errors
echo printMe();

function printMe() {
    return 'Print some text';
}
Exception is
conditional function
. In this example function
foo()
will not be defined until the
if ($makefoo)
conditional statement gets executed
$makefoo = true;

/* We can't call foo() from here 
   since it doesn't exist yet,
   but we can call bar() */
// foo();

bar();

if ($makefoo) {
  function foo()
  {
    echo "I don't exist until program execution reaches me.\n";
  }
}

/* Now we can safely call foo()
   since $makefoo evaluated to true */

if ($makefoo) foo();

function bar() 
{
  echo "I exist immediately upon program start.\n";
}
Functions within functions. Function
bar()
will not be defined until the function
foo()
is executed
function foo() 
{
  function bar() 
  {
    echo "I don't exist until foo() is called.\n";
  }
}

/* We can't call bar() yet
   since it doesn't exist. */
// bar();


foo();

/* Now we can call bar(),
   foo()'s processing has
   made it accessible. */

bar();
Note: All functions in PHP have the global scope - they can be called outside a function even if they were defined inside and vice versa
class Foo {
    
    public function __call($function, $arguments)
    {
        return "$function has been called with arguments: " . implode(', ', $arguments); 
    }
    
    // public function fireFunction($one, $two, $three) {
    //     return "pre-defined method";
    // }
}
$foo = new Foo();
echo $foo->fireFunction(5, 47, "third argument");
__call
is invoked when the method is inaccessible (when it's not public or does not exist at all)
__get
and
__set
magic methods allows us to get and set any variable without having individual getter and setter methods:
class Foo {
    protected $properties = array();

    public function __get( $key )
    {
        if(array_key_exists($key, $this->properties)){
            return $this->properties[$key];
        }
        return $this->properties[ $key ];
    }
    public function __set( $key, $value )
    {
        $this->properties[ $key ] = $value;
    }
}
$foo = new Foo();
$foo->name = 'David';

echo $foo->name;
In this example, we can set any variable, even if it does not exist. Every variable will be addd to
properties
array
With quotes:
$new = htmlspecialchars("<a href='test'>Test</a>", ENT_QUOTES);
echo $new; // &lt;a href=&#039;test&#039;&gt;Test&lt;/a&gt;
Without quotes:
$new = htmlspecialchars("<a href='test'>Test</a>");
echo $new; // &lt;a href='test'&gt;Test&lt;/a&gt;
The second parameter possible values:
ENT_COMPAT
- Will convert double-quotes and leave single-quotes alone.
ENT_QUOTES
- Will convert both double and single quotes.
ENT_NOQUOTES
- Will leave both double and single quotes unconverted. The default is ENT_COMPAT
Appends the content to the specified file if it exists (if the file does not exist, the function creates the file and writes the content)
file_put_contents($filePath, $content, FILE_APPEND);
Note: if
FILE_APPEND
parameter is not passed, the function overwrites the content.
Child class can not override final method of the parent class
class A {
    final public static function who() {
        echo __CLASS__;
    }
}

class B extends A {
    public static function who() {
        echo __CLASS__;
    }
}

B::who();
Fatal error:
Cannot override final method A::who()
Two classes extends
Animal
abstract class and both implement the same method
prey()
differently
abstract class Animal {
    // child classes must implement this
    abstract function prey();

    public function run() {
        echo "I am running!\n";
    }
}

class Dog extends Animal {
    public function prey() {
        echo "I killed the cat !\n";
    }
}

class Cat extends Animal {
    public function prey() {
        echo "I killed the rat !\n";
    }
}

$dog = new Dog();
$cat = new Cat();

$dog->prey(); // I killed the cat !
$cat->prey(); // I killed the rat !

$dog->run(); // I am running!
$cat->run(); // I am running!
We can call parent class constructor from child class using
parent
keyword
parent::__construct($name);
Complete example:
class User {
    public $username;

    function __construct($name) {
        $this->username = $name;
    }
}
class Admin extends User{
    public $admin_level;
    
    function __construct($name, $admin_level) {
        parent::__construct($name);
        $this->admin_level = $admin_level;
    }
}
$admin1 = new Admin('Tom', 'Manager');

echo $admin1->username;
echo "\n";
echo $admin1->admin_level;
If there is no constructor in child class, then parent class constructor will be called (when appropriate parameters are passed)
Results: 132