echo str_contains('Hello World', 'World'); // true
str_starts_with()
and str_ends_with()
echo str_starts_with('Hello World', 'Hello'); // true
echo str_ends_with('Hello World', 'World'); // true
gettype()
.echo get_debug_type([]); // 'array'
class Test {
public readonly string $name;
}
enum Status {
case Pending;
case Completed;
}
$fiber = new Fiber(function() {
echo "Fiber!";
});
$fiber->start();
array_is_list([]); // true
array_is_list(['apple', 2, 3]); // true
array_is_list([0 => 'apple', 'orange']); // true
// The array does not start at 0
array_is_list([1 => 'apple', 'orange']); // false
// The keys are not in the correct order
array_is_list([1 => 'apple', 0 => 'orange']); // false
// Non-integer keys
array_is_list([0 => 'apple', 'foo' => 'bar']); // false
// Non-consecutive keys
array_is_list([0 => 'apple', 2 => 'bar']); // false
// Check that the enum exists before trying to use it
if (enum_exists(Suit::class)) {
$myclass = Suit::Hearts;
}
class ItemDTO
{
public function __construct(
public readonly string $title,
public readonly Status $status,
public readonly ?DateTimeImmutable $published_at = null,
) {}
}
function respond(): (JSONResponse&SuccessResponse)|HTMLResponse|string {}
function generateSlug((HasTitle&HasId)|null $post)
{
if ($post === null) {
return '';
}
return
strtolower($post->getTitle())
. $post->getId();
}
<?php
var_dump(json_validate('{ "test": { "foo": "bar" } }')); // true
var_dump(json_validate('{ "": "": "" } }')); // false
?>
getBytesFromString
()
echo str_truncate("Hello World", 5); // Hello...
class A {
protected function ovrTest(): void {}
}
// This will work because ovrTest()
// can be found in the parent class
class B extends A {
#[\Override]
public function ovrTest(): void {}
}
// This will fail because ovrBest()
// (probably a typo) is not in the parent
class C extends A {
#[\Override]
public function ovrBest(): void {}
}
$rando = new Random\Randomizer();
// Generate a float value between a minimum
// value of 0 and a maximum value of 5
$rando->getFloat(0,5); // 2.3937446906217
//
$rando = new Random\Randomizer();
$rando->nextFloat(); // 0.3767414902847
$rando = new Random\Randomizer();
$alpha = 'ABCDEFGHJKMNPQRSTVWXYZ';
$rando->getBytesFromString($alpha, 6); // "MBXGWL"
$rando->getBytesFromString($alpha, 6); // "LESPMG"
$rando->getBytesFromString($alpha, 6); // "NVHWXC"
$rando = new Random\Randomizer();
$weighted = 'AAAAA12345';
$rando->getBytesFromString($weighted, 5); // "1AA53"
$rando->getBytesFromString($weighted, 10); // "42A5A1AA3A"
each()
, create_function()
, etc.