the unset( $val ) line ensures that the $val variable is deleted after the loop has finished. This is generally a good idea, because when the loop finishes, $val still holds a reference to the last element (that is, "Dickens"). If you were to change $val later in your code, you would inadvertently alter the last element of the $authors array
If you want to change selection, open document below and click on "Move attachment"
php 6;Kafka", "Tolkien", "Dickens" ); // Displays "Steinbeck Kafka Hardy Dickens"; foreach ( $authors as &$val ) { if ( $val == "Tolkien" ) $val = "Hardy"; echo $val . " "; } <span>unset( $val ); echo "<span>
"; // Displays "Array ( [0] => Steinbeck [1] => Kafka [2] => Hardy [3] => Dickens )" print_r ( $authors );
Notice ho Summary
status | not read | | reprioritisations | |
---|
last reprioritisation on | | | suggested re-reading day | |
---|
started reading on | | | finished reading on | |
---|
Details