0

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.

2
  • 3
    php concatinate variables with "." not with "+" Commented Apr 28, 2015 at 5:02
  • Also you have ['data1']['code1'] instead of ['data']['name'] Commented Apr 28, 2015 at 5:03

4 Answers 4

2

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

'info' => 'Full name: ' . $_SESSION['data1']['code1'] . ' ' . $_SESSION['data']['lastname']
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the following I receive error 'info' => 'info: '.$_SESSION['data']['name']' family: '.$_SESSION['data']['lastname']' '.$_SESSION['data']['phone']' ID: '.$_POST['userId'] Parse error: syntax error, unexpected '' family: '' (T_CONSTANT_ENCAPSED_STRING), expecting ')'
You are missing the . before family.
2

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

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

Comments

0

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.

Comments

0
   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'];
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.