Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Cannot unset string offsets

Description

While strings and arrays share the same syntax with square brackets, to access individual elements, that syntax is not available with the unset() function. Strings do not behave like an array, so it is not possible to remove one char with unset.

Example

<?php

$a = 'abc';
unset($a);

?>

Alternatives

  • Explode() the string into an array, use the unset() on the array, and then, implode() it back into a string.
  • Use substr() twice, to cut out the unwanted character.
  • Use preg_replace(), to replace the character with an empty string.
  • Use strtr(), to replace the character with an empty string, if it is unique.