So, in js, you can have a variable outside of a function which push can be used to add values to it and access it anywhere else.
var names = [];
function my_function_js(){
alert(names);
}
In php, can a variable act in the similar manner?
For example, let say I have an ajax function with a variable. Then I want to know if I can add different values to this variable using ajax at multiple occasions.
$names = array();
function my_function(){
$names[] = $_POST['names'];
}
Let say, for the first ajax call, mike was passed. Then steve for second call and sean for the last.
Would each value override the previous value or would it be saved like in js?
(In other words, I would like to know if I can add values to a php variable using ajax multiple calls).
Thanks.
EDIT:
It was pointed out that ajax variable (in this case $names) will be reset every time a new ajax call is made.
Then, how about have another variable that does not get affected by the ajax call and simply push the ajax value to it?
For example:
$FULL_NAMES = array();
function my_function(){
$names = $_POST['names'];
$FULL_NAMES[] = $names;
}
Would something like this work?
(In other words, I would like to know if I can add values to a php variable using ajax multiple calls).Well no. Each time an ajax call gets called the variable is.. well, let's say reset for instance. If you want to persistenly store such a value then you can either decide to save that to a file (NOT RECOMMENDED) or to use a database and query that each time using a particular criteria maybe. A possible alternative is to make a unique ajax call by passing all the names, but I have no idea of what html + js structure you have and what you effectively need to accomplish.