php - string length not decremented when unset string index -


i trying create palindrome deleting 1 character string @ time . while unsetting string index .the string length not getting reduced.

$s = "arun"; var_dump ($s); $s[1] = null; var_dump($s) ; 

output

string(4) "arun" string(4) "aun" 

why length not reduced.

because not in way reducing string, replacing 1 character.

your string gets transformed a r u n a (null) u n, still 4 chars long.

to achieve goal, use substr_replace

<?php $s = "arun"; var_dump ($s); //arun $s = substr_replace($s, "", 1,1); var_dump($s); //aun 

Comments

Popular posts from this blog

c++ - CPP, 'X' button listener -

shared memory - gstreamer shmsrc and shmsink with h264 data -

.net - Bulk insert via Dapper is slower than inserting rows one-by-one -