ComeRun
ComeRun

Reputation: 921

PHP passing session variables to array

In the following code snippet, I need to pass two value from the session but somehow I am not able to achieve it. I am a beginner in PHP and this code is not written by me but I need to modify it to cover what I need.

I need to pass two variables which is being used as a session posted from the previous page after the submission of data from class.php. first one is $_SESSION['data']['name'] and the second one $_SESSION['data']['lastname']

require_once 'admin/class.php';

    $requests = array('version'  => '1',                     
                      'info'     => 'Full name: ' + $_SESSION['data1']['code1'] + ' ' + $_SESSION['data']['lastname'],
                      'amount'   => $amount      
                      );    

Basically the way I am passing the variables to the array is not correct. Any help would be appreciated.

Upvotes: 0

Views: 927

Answers (4)

Vivek Singh
Vivek Singh

Reputation: 21

in php use (.) for concatenation like below you can get the idea

'info' => 'Full name: ' . $_SESSION['data1']['code1'] . ' ' . $_SESSION['data']['lastname']

Upvotes: 2

MaThar Beevi
MaThar Beevi

Reputation: 304

   1. This way is directly assign two variable in session
    $_SESSION['name'] = $_POST['inputname'];
    $_SESSION['lastname']=$_POST['inputlastname'];

    And next page to show:
    echo $_SESSION['name']." ".$_SESSION['lastname'];

   2. If you want to store array format
    $_SESSION["name_array"] = array(
                                    0 => array(
                                            "first_name" => $name, "last_name" => $lastname
    ));

    and next page to so foreach loop like

    foreach($_SESSION["name_array"] as $item)
    {
    echo $item['first_name'];
    echo $item['last_name'];
    }

Upvotes: 0

Mubashar Abbas
Mubashar Abbas

Reputation: 5663

You need to make sure that you call session_start() in the beginning of every file, where you are going to use the $_SESSION variable.

Upvotes: 0

Sougata Bose
Sougata Bose

Reputation: 31739

+ is not the concatenation operator in PHP. It is .. It should be -

'info' => 'Full name: ' . $_SESSION['data1']['code1'] . ' ' . $_SESSION['data']['lastname']

Upvotes: 2

Related Questions