Hey everyone,
I am fairly new to PHP, and I now have a problem that has been bugging me for a while.
Here is some example code to explain my problem.
PHP Code:
<?php
$myArray array('first','second','third');
function myFunct1(){
global $myArray;
if(//stuff){
$myArray = array("first" => "Errors Occurred, Please Try Again.");
}
}
function myFunct2(){
global $myArray;
if(//stuff){
$myArray = array("second" => "Errors Occurred, Please Try Again.");
}
}
function myFunct3(){
global $myArray;
if(//stuff){
$myArray = array("third" => "Errors Occurred, Please Try Again.");
}
}
myFunct1();
myFunct2();
myFunct3();
print_r($myArray);
?>
What I expect to happen, is that if the IF statement is true in each case, the array should hold 3 different strings. However what it does is, only ever keep the last string written to it. Can someone explain to me why it would do this?
If it doesn't make sense, I have attached my actual code with some examples.
Thanks - CC7
## SOLVED ##
I realized that
PHP Code:
$myArray = array("third" => "Errors Occurred, Please Try Again.");
AND
PHP Code:
$myArray["third"] = "Errors Occurred, Please Try Again.";
are NOT the same. The fist one (re)-creates an array. The second one updates the index!
Bookmarks