<?php

// methods below use the null coalescing operator (??)
// instead of isset() and if-else statements

class Node {
    
    public mixed $data;
    public ?Node $next;
    
    public function __construct(mixed $data) {
        $this->data = $data;        
    }
}

class Stack {
    
    public ?Node $head; 
    
    public function push(mixed $data) : void {
        $node = new Node($data);          
        $node->next = $this->head ?? null;
        $this->head = $node;
    }

    public function pop() : mixed {
        $data = $this->head->data ?? null;
        $this->head = $this->head->next ?? null;        
        return $data ?? 'Stack empty';
    }
}

$ll = new Stack();
echo 'pop(): ' . $ll->pop() . PHP_EOL;
for ($i = 1; $i < 4; $i++) { $ll->push($i); }
print_r($ll);
echo 'pop(): ' . $ll->pop() . PHP_EOL;
echo 'pop(): ' . $ll->pop() . PHP_EOL;
echo 'pop(): ' . $ll->pop() . PHP_EOL;
echo 'pop(): ' . $ll->pop() . PHP_EOL;
print_r($ll);

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: