What Are the New Features in PHP 8?

new features in php 8
new features in php 8
// Fibonacci function  
function fibonacci($n) {  
    if ($n <= 1) return $n;  
    return fibonacci($n - 1) + fibonacci($n - 2);  
}  
  
// Measure execution time  
$start = microtime(true);  
echo fibonacci(35); // Change the input to a larger number for a more noticeable difference  
$end = microtime(true);  
  
$execution_time = $end - $start;  
echo "\nExecution time: " . $execution_time . " seconds";
Example
 
function processInput(int|string $input): void {  
    if (is_int($input)) {  
        echo "Integer: $input";  
    } else {  
        echo "String: $input";  
    }  
}  
  
processInput(42);    // Outputs: Integer: 42  
processInput("test"); // Outputs: String: test
#[Route("/home")]  
class HomeController {  
    #[Route("/index")]  
    public function index() {  
        // ...  
    }  
}  
class User {  
    public function __construct(  
        private string $name,  
        private int $age  
    ) {}  
}  
  
$user = new User("John Doe", 30);  
 function createUser(string $name, int $age, bool $isAdmin = false): void {  
    // ...  
}  
  
createUser(name: "Jane Doe", age: 25, isAdmin: true); 
$result = match ($statusCode) {  
    200 => 'OK',  
    404 => 'Not Found',  
    500 => 'Internal Server Error',  
    default => 'Unknown Status',  
};  
  
echo $result; // Outputs: OK, Not Found, etc.  
$user = getUser();  
$address = $user?->getAddress()?->getStreet();  
  
echo $address; // Outputs: Street name or null if any property is null  
$cache = new WeakMap();  
$object = new stdClass();  
  
$cache[$object] = 'cached value';  
  
unset($object);  
  
// The object is now eligible for garbage collection, and the cache entry is removed  
/ str_contains  
if (str_contains('Hello, world!', 'world')) {  
    echo 'Found'; // Outputs: Found  
}  
  
// str_starts_with  
if (str_starts_with('PHP 8 is great', 'PHP')) {  
    echo 'Starts with PHP'; // Outputs: Starts with PHP  
}  
  
// str_ends_with  
if (str_ends_with('PHP 8 is great', 'great')) {  
    echo 'Ends with great'; // Outputs: Ends with great  
}
// Case-sensitive check  
if (str_contains('Hello, world!', 'World')) {  
    echo 'Found'; // Outputs nothing since it's case-sensitive  
}  
  
// Case-insensitive check  
if (stripos('Hello, world!', 'World') !== false) {  
    echo 'Found'; // Outputs: Found  
}  
if (str_starts_with('PHP 8 is great', '')) {  
    echo 'Starts with an empty string'; // Outputs: Starts with an empty string  
}  
var_dump(0 == 'foo'); // PHP 7: true, PHP 8: false  
var_dump(0 == '');    // PHP 7: true, PHP 8: false  
$input = $_GET['input'] ?? throw new InvalidArgumentException('Input required');
unction add(int $a, int $b): int {  
    return $a + $b;  
}  
  
add("1", "2"); // PHP 7: TypeError with less informative message, PHP 8: More detailed TypeError message  

Conclusion

You May Also Like

Leave a Reply

Your email address will not be published. Required fields are marked *

×