What kind of situation is this! What to do with it

Source: Internet
Author: User
This is what kind of situation!!!
For example:

$str = "Abcadef";
$strone = $str [0];
for ($i =0; $i<>
if ($strone = = $str [$i]) {
$str [$i]= "";
}
}
Var_dump ($STR);


Shows:

String (7) "Bcdef";

Excuse me, why is this string still 7 lengths, there is no way to make him into 5 lengths,



------Solution--------------------
$str = "Abcadef";
$strone = $str [0];
$r = ";
for ($i =0; $i <>
if ($strone! = $str [$i]) {
$r. = $str [$i];
}
}
$str = $r;
Var_dump ($STR);

------Solution--------------------
Solution upstairs everybody said it, but to avoid you repeating it, say something about the theory

The program appears to be "reasonable," but it's actually wrong, and the key is the way PHP cores store variables

PHP has a string variable assigned to the length of the record, to change the length of the need to re-assign value
$str [x]= ' This form does not re-assign a value to a string variable, because ' is a null byte and can only be used for an empty string
It is possible to replace a non-empty byte


For details, please refer to the understanding of PHP kernel
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.