21

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.

9
  • a little more code would be nice. Do you use the sentence variable before? Commented Feb 4, 2012 at 5:34
  • runs just fine: ideone.com/R6y3n Commented Feb 4, 2012 at 5:40
  • this shouldn't return any errors. Can u give the complete code atleast relevant to this section. As far as i can see, this is fine Commented Feb 4, 2012 at 5:40
  • When i echo the $sentence it gives the correct string. Commented Feb 4, 2012 at 5:44
  • 1
    And if you don't want to strip " within the sentence, you can use trim($sentence, '"') Commented Feb 4, 2012 at 5:56

4 Answers 4

43

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.

Sign up to request clarification or add additional context in comments.

4 Comments

actually I was parsing bunch of strings from the file. So the file was not empty. There were a lot of string chunks but due to some spaces for the method came null string and gave the error. It is my bad. I just checked and realized. tx.
The problem with empty is that empty("0") will also return true.
@KeVin programmatically 0 and empty are different, empty is absence of data while zero is a valid value.
@ClainDsilva , not sure what you mean, since the initial warning is absolutely valid. PHP considers string of "0" empty. Hence, this is an invalid answer I do consider, too.
0

In case you are using WordPress and having the same issue, ensure to add domain-name.tld/wp-login as your URl redirect

Comments

-1

If you are sure about the variable type and the fact of its definition (e.g. function test(string $a) // ...), have you considered the operator @?:

> unset($a); echo $a[1] === '2' ? 'T' : 'F';
WARNING  Undefined variable $a.
WARNING  Trying to access array offset on value of type null.
F

> $a = '1'; echo $a[1] === '2' ? 'T' : 'F';
WARNING  Uninitialized string offset 1 on line 2.
F

> $a = '12'; echo $a[1] === '2' ? 'T' : 'F';
T
> unset($a); echo @$a[1] === '2' ? 'T' : 'F';
F

> $a = '1'; echo @$a[1] === '2' ? 'T' : 'F';
F

> $a = '12'; echo @$a[1] === '2' ? 'T' : 'F';
T

PHP supports one error control operator: the at sign (@). When prepended to an expression in PHP, any diagnostic error that might be generated by that expression will be suppressed...

The @-operator works only on expressions. A simple rule of thumb is: if one can take the value of something, then one can prepend the @ operator to it. For instance, it can be prepended to variables, functions calls, certain language construct calls (e.g. include), and so forth. It cannot be prepended to function or class definitions, or conditional structures such as if and foreach, and so forth.

Source

Alternative solutions:

- Check for the size of the string before using the offset.
- Use substr(), which returns empty string for out of range offsets.

Source

13 Comments

It's generally considered preferable to check explicitly for edge cases, rather than relying on @ to suppress errors. It might mask other errors that you didn't intend.
Thank you, @Barmar, and I do get what you mean, but for example, in this particular case of accessing a string-typed variable, what kind of error would get masked?
It will mask an undefined variable, not just an out-of-range index.
For instance, if you make a typo in the variable name.
I mean, cases not of incompetent developers but runtime ^^" // For example, inside a function of explicit definition: function test(string $a): string { /* ... */ }
|
-2

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).

1 Comment

This is not correct. What happened in PHP 7.1 was that now negative indices was allowed as well: php.net/manual/en/language.types.string.php#example-51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.