Checks if a string is contained in another string.
$sentence = "The quick brown 🦊 jumps over the lazy 🐶.";
$word = "🦊";
if (str_contains($sentence, $word)) {
echo "The sentence contains the word 🦊.";
}
Checks if a string starts with a given substring.
$sentence = "🚀 Launching into space!";
if (str_starts_with($sentence, "🚀")) {
echo "The sentence starts with a rocket emoji!";
}
Checks if a string ends with a given substring.
$sentence = "It's a beautiful day! ☀️";
if (str_ends_with($sentence, "☀️")) {
echo "The sentence ends with a sun emoji!";
}
Gets the type of a variable.
$num = 42;
echo get_debug_type($num); // "integer"
Returns the unique identifier of the given resource.
$file = fopen('test.txt', 'r');
echo get_resource_id($file); // e.g., "7"
Division function that includes support for dividing by zero.
$result = fdiv(10, 0); // INF
Returns a human-readable message for the last PCRE regex execution error.
preg_match('/(/', '');
echo preg_last_error_msg(); // "missing )"
Fetches the first key of an array.
$array = ['🍏'=>'Apple', '🍊'=>'Orange', '🍇'=>'Grape'];
echo array_key_first($array); // "🍏"
Fetches the last key of an array.
$array = ['🍏'=>'Apple', '🍊'=>'Orange', '🍇'=>'Grape'];
echo array_key_last($array); // "🍇"
Gets the severity of the error.
try {
trigger_error("Custom error", E_USER_WARNING);
} catch (ErrorException $e) {
echo $e->getSeverity(); // 512
}
PHP 8 introduced several new filter functions. Here’s an example using filter_var
with FILTER_VALIDATE_BOOL
:
var_dump(filter_var('yes', FILTER_VALIDATE_BOOL)); // bool(true)
A new class that holds references to objects, which does not prevent those objects from being garbage collected.
$weakmap = new WeakMap();
$obj = new stdClass();
$weakmap[$obj] = 'Hello, world!';
PHP 8 introduced Constructor Property Promotion, a new syntax for constructing value objects.
class Money {
public function __construct(
public int $amount,
public string $currency
) {}
}
$tenDollars = new Money(10, 'USD');
This is a switch-like statement.
echo match (1) {
0 => '🚫',
1 => '✅',
default => '⁉️',
};
This new operator (?->) allows null checking when accessing properties or methods.
class User {
public function getAddress(): ?Address {
// returns Address or null
}
}
$user = new User();
$country = $user?->getAddress()?->country; // no error if getAddress() returns null
This feature allows you to pass in values to a function by specifying the value name.
new Money(amount: 10, currency: 'USD');
Also known as annotations in other programming languages.
#[Attribute]
class ExampleAttribute {}
#[ExampleAttribute]
class ExampleClass {}
This feature allows the combination of class properties and the constructor into a single declaration.
class Money {
public function __construct(
public int $amount,
public string $currency
) {}
}
This feature allows for type declarations that can be one of multiple types.
function print_id(int|string $id): void {
echo 'ID: ' . $id;
}
PHP 8 introduces two JIT compilation engines, Tracing JIT and Function JIT.
Note: JIT compilation isn’t a feature you can directly demonstrate with a code snippet, but it’s an important improvement in PHP 8 that can provide significant performance improvements.
In conclusion, PHP is a constantly evolving language with many exciting new features and improvements. Whether you’re a seasoned PHP developer or a newcomer, it’s well worth your time to get to know these new features and using them in your code. Stay curious, keep learning, and happy coding! 🚀
Pieris Massage
jerryfraumaux
Renta Stay UK
Global Business Directory