简体   繁体   中英

How to return a value of a function from one page to another page i php and store it in an array using session?

i have returned the value of variable $tsal from sales.php page from the 
following function   
 function totalsal()
{

    $tsal= $this->fsal()+$this->bonus();
    return $tsal;

}
this is my index.php page
session_start();

$tsal->$obj->totalsal();


$_SESSION['totalsal']=$tsal;

How can i store previous value of variable in session so that new value does not override the previous one. and also print the new value along with the previous one

On the second page, or any page thereafter:

session_start();

if(isset($_SESSION['totalsal'])) {
    echo "Your total sal is: " . $_SESSION['totalsal'];
}

you need to check, if the variable was already set you can do this by using isset(). the following example will set the variable, if it was not set before. it prevents to be overwritten. it works on the same page too (for example if you refresh the page).

if(!isset($_SESSION['total'])){           // ! means not - if the variable is NOT set
 $_SESSION['total'] = getResultFromAnything();
}

echo $_SESSION['total'];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM