user3782541
user3782541

Reputation:

PHP multiple variables

I am learning some php right now. Is there a way to define a variable with multiple variables?

Something like:

$name = "John"
$lastname = "Doe"
$number = "017434234"

$user = $name,$lastname,$number

Of corse this script does not work. Which is the correct way?

Upvotes: 0

Views: 344

Answers (2)

MrK
MrK

Reputation: 1078

Awesome that youre learning PHP :)

You can make it an array like this whit a function, of course.. this is a little more "over complicated" than the example above.

Script:

//Function array with inputting and selecting what to return:
function MyArray($Firstname, $Lastname, $Number, $OutPutWhat){

   //Create an array:
   $Array = array(
       'FirstName' => "$Firstname",
       'LastName' => "$Lastname",
       'Number' => "$Number"
   );
       //Return the arrayValue[ofthis]
       return $Array[$OutPutWhat];
}

 //Echo out the value
echo MyArray("John", "Doe", "1234567", 'FirstName');  //Outputs the firstname

Upvotes: 0

BobbyTables
BobbyTables

Reputation: 4685

If you want to put them together, the dot concatenates the variables:

$user = $name.$lastname.$number;

If you want to define multiple strings, you can use an array:

$user = ["John", "Doe", "123"];

Or you can use keys in your array to give them meaning:

$user = [firstname => "John", lastname => "Doe"];
echo $user->firstname; //gives John

Upvotes: 1

Related Questions