Trying to Access Array Offset on Value of Type Bool in PHP 7.4

Trying to access array offset on value of type bool in PHP 7.4

Easy with PHP ?? null coalescing operator

return $Row['Data'] ?? 'default value';

Or you can use as such

$Row['Data'] ??= 'default value';
return $Row['Data'];

Error : Trying to access array offset on value of type int

$key is probably not a string, you can use gettype() to check.

You can access to number digits with substr() :

for ($i=0; $i<strlen($string); $i++){
$ord = ord(substr($string, $i, 1));

If you prefer use array-access you must cast $string to (string) :

function strToHex($string){
$string = (string)$string;

A final propal could be :

function strToHex($string)
{
$result = '';
$n = strlen($string);
for ($i = 0; $i < $n; $i++) {
$c = substr($string, $i, 1);
$c = ord($c);
$result .= sprintf('%02X', $c);
}
return $result;
}

echo strToHex(1234); // 31323334
echo strToHex('Yop!'); // 596F7021


Related Topics



Leave a reply



Submit