Backend Development 7 min read
Unlock PHP 8’s New System Functions: fdiv, str_contains, mb_str_pad, bcceil & More
This article introduces the latest PHP 8 system functions—including fdiv for floating‑point division, str_contains, str_starts_with, str_ends_with for string checks, mb_str_pad for multibyte padding, and new arbitrary‑precision math functions like bcceil, bcfloor, bcround—providing clear examples and usage tips for backend developers.
Laravel Tech Community
Laravel Tech Community
PHP System Functions Overview
PHP core provides built‑in functions for common tasks such as string manipulation, array handling, and mathematical operations. Since PHP 8.0 additional useful functions have been added.
1. fdiv()
<?php
// Floating‑point division, returns the quotient
echo fdiv(10, 3); // 3.333333333333
?>2. str_contains()
<?php
$str = "125abc4556";
if (str_contains($str, 'abc')) {
echo 'Found';
} else {
echo 'Not Found';
}
?>3. str_starts_with() and str_ends_with()
<?php
$str = "abc125abc4556xyz";
if (str_starts_with($str, 'abc')) {
echo 'Starts with abc';
}
if (str_ends_with($str, 'xyz')) {
echo 'Ends with xyz';
}
?>4. Named arguments
<?php
function example(int $a, string $b): int|string {
return $a . $b;
}
$m = example(a: 10, b: "hello");
var_dump($m); // string(7) "10hello"
?>5. json_validate()
<?php
$res1 = json_validate('{ "test": { "foo": "bar" } }');
var_dump($res1); // bool(true)
$res2 = json_validate('{ "": "": "" }');
var_dump($res2); // bool(false)
?>6. mb_str_pad()
<?php
// Pad a multibyte string to a given length (PHP 8.3+)
var_dump(mb_str_pad('▶▶', 6, '❤❓❇', STR_PAD_RIGHT)); // string(18) "▶▶❤❓❇❤"
var_dump(mb_str_pad('▶▶', 6, '❤❓❇', STR_PAD_LEFT)); // string(18) "❤❓❇❤▶▶"
var_dump(mb_str_pad('▶▶', 6, '❤❓❇', STR_PAD_BOTH)); // string(18) "❤❓▶▶❤❓"
var_dump(mb_str_pad('🎉', 3, '祝', STR_PAD_LEFT)); // string(10) "祝祝🎉"
?>7. bcceil()
<?php
var_dump(bcceil('4.3')); // string(1) "5"
var_dump(bcceil('9.999')); // string(1) "10"
var_dump(bcceil('-3.14'));// string(2) "-3"
?>8. bcdivmod()
<?php
list($quot, $rem) = bcdivmod('5', '3');
echo $quot; // 1
echo $rem; // 2
list($quot, $rem) = bcdivmod('-5', '3');
echo $quot; // -1
echo $rem; // 2
?>9. bcfloor()
<?php
var_dump(bcfloor('4.3')); // string(1) "4"
var_dump(bcfloor('9.999')); // string(1) "9"
var_dump(bcfloor('-3.14'));// string(2) "-4"
?>10. bcround()
<?php
var_dump(bcround('3.4')); // "3"
var_dump(bcround('3.5')); // "4"
var_dump(bcround('3.6')); // "4"
var_dump(bcround('5.045', 2)); // "5.05"
var_dump(bcround('5.055', 2)); // "5.06"
var_dump(bcround('678', -2)); // "700"
var_dump(bcround('123.45', 3)); // "123.450"
var_dump(bcround('123.45', 2)); // "123.45"
var_dump(bcround('123.45', 1)); // "123.5"
var_dump(bcround('123', 0)); // "123"
var_dump(bcround('120', -1)); // "120"
var_dump(bcround('100', -2)); // "100"
var_dump(bcround('0', -3)); // "0"
?>11. array_merge() new syntax
<?php
$arrayA = ['a' => 1];
$arrayB = ['b' => 2];
$result = array_merge(['c' => 0], $arrayA, $arrayB);
print_r($result); // Array ( [c] => 0 [a] => 1 [b] => 2 )
?>12. array_is_list()
<?php
$array = [1, 2, 3];
var_dump(array_is_list($array)); // bool(true)
$array = ['a' => 1, 'b' => 2, 'c' => 3];
var_dump(array_is_list($array)); // bool(false)
?>13. array_key_first(), array_key_last() and array_count_values()
<?php
$array = [1, 2, 3, 3];
echo array_key_first($array); // 0
echo array_key_last($array); // 3
$ret = array_count_values($array);
print_r($ret); // Array ( [1] => 1 [2] => 1 [3] => 2 )
?>14. password_hash() & password_verify()
<?php
$password = 'myLaravel';
$hash = password_hash($password, PASSWORD_DEFAULT); // Argon2id by default
$isValid = password_verify($password, $hash);
?>15. array_find() and array_find_key()
<?php
$numbers = [1, 2, 3, 4];
$found = array_find($numbers, fn($v) => $v > 3); // 4
$users = [
['id' => 1, 'name' => 'Alice'],
['id' => 2, 'name' => 'Bob'],
];
$user = array_find_key($users, 'id', fn($k) => $k == 1);
print_r($user); // ['id'=>1,'name'=>'Alice']
?>Written by
Laravel Tech Community
Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.
0 followers
Reader feedback
How this landed with the community
Rate this article
Was this worth your time?
Discussion
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
