Title: _underscore_to_camelcase() bug
Last modified: August 24, 2016

---

# _underscore_to_camelcase() bug

 *  [BevanR](https://wordpress.org/support/users/bevanr/)
 * (@bevanr)
 * [11 years, 1 month ago](https://wordpress.org/support/topic/_underscore_to_camelcase-bug/)
 * `_underscore_to_camelcase( $str )` has a programming error:
 * `for ( $i = 1; $i <= count($parts); $i ++ )` compares “less than or equal to”(`
   <=`) but should compare only “less than” (`<`). This causes an undefined offset
   PHP notice for the last value of `$i`, which equals the length of the array, 
   because there is no item with that index. This is a classic out-by-one error.
 * `foreach` can be used with a check inside the loop for the first item to simplify
   the code and reduce the likelihood of such errors.
 * Also, `strtoupper()` in `if ($parts[$i] == 'id') ... strtoupper($parts[$i])` 
   is redundant, since the returned value is always ‘ID’.
 * In total;
 *     ```
       foreach (explode('_', $input) as $part) {
         $result .= empty($result) ? $part : ($part === 'id' ? 'ID' : ucfirst($part));
       }
       ```
   
 * This will have different behaviour where the input parameter is “_foo_bar”. This
   implementation will convert it to “fooBar”, but the current implementation will
   convert it to “FooBar”. If this difference is important, `isset()` could be used
   instead, or `foreach (... as $i => $part)` with `if ($i === 0)`.
 * Unit tests for this function would be very wise.
 * [https://wordpress.org/plugins/gigya-socialize-for-wordpress/](https://wordpress.org/plugins/gigya-socialize-for-wordpress/)

The topic ‘_underscore_to_camelcase() bug’ is closed to new replies.

 * ![](https://s.w.org/plugins/geopattern-icon/gigya-socialize-for-wordpress_335aa2.
   svg)
 * [Gigya - Social Infrastructure](https://wordpress.org/plugins/gigya-socialize-for-wordpress/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/gigya-socialize-for-wordpress/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/gigya-socialize-for-wordpress/)
 * [Active Topics](https://wordpress.org/support/plugin/gigya-socialize-for-wordpress/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/gigya-socialize-for-wordpress/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/gigya-socialize-for-wordpress/reviews/)

 * 0 replies
 * 1 participant
 * Last reply from: [BevanR](https://wordpress.org/support/users/bevanr/)
 * Last activity: [11 years, 1 month ago](https://wordpress.org/support/topic/_underscore_to_camelcase-bug/)
 * Status: not resolved