Sorry to ask if it was already asked before, but none of those solutions worked for me. Everytime I try to add into array, what I get is new array. See PHP code below:
<?php
session_start();
if (!isset($_SESSION['$rNumber'])){
$_SESSION['$rNumber'] = 0;
}
if(empty($_SESSION['words']))
{
$_SESSION['words'] = array();
}
if (isset($_POST['button1'])){
$random = rand(10, 20);
$_SESSION['$rNumber'] = $_SESSION['$rNumber'] + $random;
$word = 'You entered a farm and earned '.$random.' golds.';
array_push($_SESSION['words'], $word);
} else if ...
}
?>
When I var_dump the $words[], it always has 1 value only, though I add this very many times! Please let me know if additional info needed. Thanks!
You are inspecting the wrong variable with var_dump() So try to var_dump($_SESSION['words'])
and not var_dump($words)
. I tried it and it works for me. Array push adds the value $word
into $_SESSION['words']
, therefore you have to var_dump the $_SESSION['words']
because $word
is still just a string, not an array.