skip to Main Content

I want to concatenate the following two string, one is a dictionary and other is simple string.
I tried the following but it did not work.

$email = $data['username'] . "@gmail.com"

Tried

$email = $data['username'] . "@gmail.com"

Want the result

echo($email);
[email protected]

2

Answers


  1. Concatenation is correct , you may check the data and key:

    $data = ['username' => 'sadan_khan'];
    $email = isset($data['username']) ? $data['username'] . "@gmail.com" : '[email protected]';
    echo $email;
    
    OUTPUT:
    [email protected]
    
    Login or Signup to reply.
  2. You need to check that $data and $data[‘username’] exist.

            if(isset($data) && isset['$data['username']'])
    
                {$email = $data['username'] . "@gmail.com"echo $email;
    
             }
    

    if $data is an object you need to use $data->username

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search