Home > Mobile >  Concatenate two values in PHP
Concatenate two values in PHP

Time:11-06

Is there any way to concatenate values

$mark = $_SESSION["mark"];  //consider this 2

$p1 = 10;
$p2 = 20;
$p3 = 30;

$price = $p.$mark;   //i want result 20  [$p2]

Thanks

CodePudding user response:

A much more maintainable and usable approach to this scenario overall would be to use an array for the "p" values:

$mark = $_SESSION["mark"];  //consider this 2
$pVals = array(1 => 10, 2 => 20, 3 => 30);
$price = $pVals[$mark]; 

CodePudding user response:

You can concatenate variable names and strings to get dynamic variable names like this:

$price = ${'p'.$mark};

The curly braces tell php to evaluate the content in them and then $ makes it a variable name.

  •  Tags:  
  • php
  • Related