How to resolve Uninitialized string offset error: 0 in

Asked

Viewed 2,261 times

3

I am using Phreeze PHP to create a CRUD application but I am getting the following message when generating the application, the message is this:

Uninitialized string offset: 0 in Modifier.lcfirst.php at line 16

The page with the code is like this:

function smarty_modifier_lcfirst($s) {
     return strtolower( $s{0} ). substr( $s, 1 );
}

The project page, if anyone is interested, is this: Pheeze

  • What are you passing to the function? It should give this error if you pass an empty string.

  • Usually the solution is isset()

  • 2

    Hello @rray and bfavaretto thanks for the tips, I managed to solve with isset().

1 answer

2


This is because the string does not have offset 0. That is, it is empty.

Example:

$a = ''

$a{0}; // PHP error:  Uninitialized string offset: 0

As stated in some comments, you can use the function isset to make this check. It is also possible to use the function empty to know if the string is empty.

You can change the function to the following form:

smarty_modifier_lcfirst($s) { 

    if (empty($s)) return;

    return strtolower( $s{0} ). substr( $s, 1 );
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.