這個問題在這里已經有了答案: 將字串評估為條件 PHP 2 個答案 14 小時前關閉。
例子:
$number = "50";
//I am simulating the creation of this $variable since this is the value that it gives me in a real server
$variable = "0 - 10"; //$variable must go like this with quotes
$result = $number $variable; //50 0 - 10 expect
echo $result;
//i need result 40
正如您在示例中看到的,我需要使用 $variable 生成的字串在數學運算中使用它。
uj5u.com熱心網友回復:
您可以使用該eval功能來完成作業。
注意eval() 語言結構非常危險,因為它允許執行任意 PHP 代碼。因此不鼓勵使用它。如果您已仔細驗證除了使用此構造之外別無選擇,請特別注意不要將任何用戶提供的資料傳入其中,而無需事先正確驗證。
<?php
$number = "50";
$variable = "0 - 10";
eval("\$variable = $variable;");
$result = $number $variable;
echo $result; // 40
uj5u.com熱心網友回復:
您可以嘗試使用 PHP eval() 函式來解決您的問題,但是 eval() 語言結構非常危險,因為它允許執行任意 PHP 代碼。見https://www.php.net/manual/en/function.eval.php
<?php
$number = "50";
//I am simulating the creation of this $variable since this is the value that it gives me in a real server
$variable = eval("'0 - 10';"); //$variable must go like this with quotes
$result = $number $variable; //50 0 - 10 expect
echo $result;
?>
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/492744.html
標籤:php
下一篇:用戶名一直被驗證為名稱
