What is the situation! What should I do?

Source: Internet
Author: User
What is the situation !!! For example: $ str = "abcadef"; $ strone = $ str [0]; for ($ I = 0; $ I & lt; strlen ($ str ); $ I ++) {if ($ strone = $ str [$ I]) {$ str [$ I] = ""; what is the situation !!!
For example:

$ Str = "abcadef ";
$ Strone = $ str [0];
For ($ I = 0; $ I If ($ strone = $ str [$ I]) {
$ Str [$ I] = "";
}
}
Var_dump ($ str );


Displayed:

String (7) "bcdef ";

Excuse me, why is this string still 7 characters long? is there any way to change it to 5 characters long,



------ Solution --------------------
$str="abcadef";
$strone=$str[0];
$r = '';
for($i=0;$i   if($strone != $str[$i]){
    $r .= $str[$i];
  }
}
$str = $r;
var_dump($str);

------ Solution --------------------
The solution has been mentioned above, but to avoid repeating the same mistakes, let's talk about the theory --

The program looks "reasonable", but it is actually incorrect. The key lies in the way the php core stores variables.

Php records the length after assigning values to a string variable. to change the length, you need to assign a value again.
$ Str [x] = ''. you cannot assign a new value to the string variable because'' is a null byte and can only be used as a null string.
It is feasible to replace a non-null byte.


For more information, see 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.