Skip to main content

New PHP8 Functions

·156 words·1 min·
Table of Contents

In this post let’s cover a few new interesting functions in PHP81

String functions
#

str_contains()2
#

This one has been a long time coming. Instead of

if (strpos('Lululu I've got some apples, 'apples') !== false) { /* … */ }

the following will suffice

if (str_contains('Lululu I've got some apples, 'apples')) { /* … */ }

str_starts_with() & str_ends_with()3
#

Two other functions that should have been added a long time ago:

str_starts_with('haystack', 'hay') // true
str_ends_with('haystack', 'stack') // true

Types & resources
#

get_debug_type()4
#

This returns the variable type. Difference with gettype() is that get_debug_type() is more specific:

namespace Foo;

class Bar
{
}

$bar = new Bar();

echo gettype($bar)."\n"; // Object
echo get_debug_type($bar); // Foo\Bar

get_resource_id()5
#

Before php8 we had to cast resources to int to get the id:

$resourceId = (int) $resource;

Now the good PHP people have given us a function:

$resourceId = get_resource_id($resource);