Originally posted by drew010
however, if you try to do array_push($array_name,"value");
without declaring $array_name as an array, it will give you an error.
php is not strict when it comes to declaring variables and the data-type of the variables like C/C++ and some other languages are.
You are correct that PHP is "weakly typed," but in my example, $array_name is assumed an array, and no errors will be produced by using array operators on the array. You might even say that PHP is "intuitively typed."
$string="The truth will make you free";
while ($x<strlen($string)) {
$myarray[$x]=$string[$x]." ";
$x++;
}
array_push($myarray, "!");
foreach ($myarray as $t) {
echo $t;
// No errors...OTOH...
array_push($string, "!");
//produces "Warning: first argument to array_push() should be an array"
}
Now, there's little point to arguing about it ... the OP asked a specific question, I gave a correct answer - typical geek response. 😃
Generally, converting strings to arrays for manipulation is not necessary and is a kludge. There are two great suggestions in this thread, and they should be used. However, it is trivial to do, since you can index a string "as if" it were an array 🙂