简体   繁体   English

在 PHP 中连接两个值

[英]Concatenate two values in PHP

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谢谢

A much more maintainable and usable approach to this scenario overall would be to use an array for the "p" values:对于这种情况,总体上更易于维护和可用的方法是使用数组作为“p”值:

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

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.花括号告诉 php 评估其中的内容,然后$使其成为变量名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM