php 第 xx 行的 eval() 代码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7874363/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 03:32:28  来源:igfitidea点击:

eval()'d code on line xx

php

提问by Hawkar Oghiz

i have this code , i want to convert the get(title) to variable , its correctly working , for example i have variable 'news , sport, program ' , my problem is that when someone is changing the title="news" to title=new , and i dont have the new variable how i can control it when the user change the title ; this will appear an error that the variable undefined and eval()'d code on line xx error

我有这个代码,我想将 get(title) 转换为变量,它可以正常工作,例如我有变量“新闻、体育、节目”,我的问题是当有人将标题 =“新闻”更改为标题时=new ,而且我没有新变量,当用户更改标题时如何控制它;这将出现一个错误,变量 undefined 和 eval()'d 代码在第 xx 行错误

if(isset($_GET['title']))
    $title=stripslashes($_GET['title']);
else
   $title="news";

$trimed=trim(strtolower($title));
$variable="$".str_replace(" ","_",$trimed);
$title=eval('return '.$variable.';');  

if(!isset($title))
    $title=$$title;

thank you for help

谢谢你的帮助

采纳答案by dynamic

Just do like this:

只是这样做:

$nameOfVar = 'return' . $variable;
$title = $$nameOfVar;

回答by Sarfraz

You can use ${}syntax and replace:

您可以使用${}语法并替换:

$title = eval('return '. $variable.';');  

With:

和:

$title = ${'return '. $variable};