String is called immutable but in this case value of string changed. why ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

String is called immutable but in this case value of string changed. why ?

? <?php //program to swap the string $name1="Learner"; $name2="Programmer"; $temp=$name1 ; $name1 =$name2 ; $name2 =$temp; echo "name1 =".$name1 ."<br>"; echo "name2 =".$name2 ; ?>

20th May 2017, 6:21 AM
surendra ojha
surendra ojha - avatar
3 Answers
+ 9
$name = "Learner"; $name[0] = "P"; // Error This code leads to error because you can not change string that way.
20th May 2017, 6:59 AM
Wilson Bol
Wilson Bol - avatar
+ 4
In fact, you're not changing a string. You're assigning a new string to a variable instead.
20th May 2017, 7:22 AM
Álvaro
+ 2
thanks
20th May 2017, 11:15 AM
surendra ojha
surendra ojha - avatar