i am doing a stuff in php and not it is debug mode. So i am us
error_reporting(E_ALL);
but When i try to access any character of string it gives me error due to the error reporting.
$sentence = "Hello World";
$sentence[0] //Uninitialized string offset: 0
edited:
public static function prepareSentence($sentence)
{
$sentence = trim($sentence);
if ($sentence[0] == '"') //Uninitialized string offset: 0
$sentence = substr($sentence, 1, strlen($sentence));
if ($sentence[strlen($sentence) - 1] == '"')
$sentence = substr($sentence, 0, -1);
if ($sentence[0] == '"' || $sentence[strlen($sentence) - 1] == '"')
return self::prepareSentence($sentence);
return $sentence;
}
How should I do in order to work in dev mode. I need the error_reporting(E_ALL);
thanks in advance.
For empty string, you can't use $sentence[0]
, that will cause the notice you got.
You can add !empty($sentence)
to check if it is empty.
You are creating $sentence as a string ($sentence = "Hello World";) and then calling it as an array ($sentence[0]). That is no longer allowed. It used to work silently in the background and change the variable to an array for you with that error but as of PHP 7.1 it will fail completely. Comes out as an E_NOTICE error (should actually be upgraded to E_DEPRECATED or something as it now fails but whatever).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With