For example: $str = 'Welcome to register xxx, your registration code is: {$code}';
When calling, first generate $code = 123456;
How to replace $code
in $str
with 123456
, the result is: Welcome to register xxx, your registration code is: 123456
Solution 1:
$code = 123456;
$str = '欢迎注册xxx,你的注册码是:{$code}';
eval("$str = \"$str\";");
Are there any other elegant solutions?
天蓬老师2017-05-16 13:16:08
<?php
$name = 'Panda';
$code = 6379;
printf("欢迎注册%s,你的注册码是:%d",$name,$code);
//欢迎注册Panda,你的验证码是6379
?>
For details on the printf() function, please see http://php.net/sprintf