如何使用PHP Eval()函数打开和关闭循环?
我想做的是,我需要生成一个请求给定产品的所有属性的函数。我有4种不同类型的产品。其中一种产品类型是组产品,当产品是组产品时,我们需要浏览组产品的所有子产品,并询问所有子产品的属性。我不希望基于产品类型重复代码,所以在需要时尝试使用PHP Eval()函数启动和关闭循环。但是有人告诉我它怎么不起作用,有人能帮我吗 这是我的密码如何使用PHP Eval()函数打开和关闭循环?,php,Php,我想做的是,我需要生成一个请求给定产品的所有属性的函数。我有4种不同类型的产品。其中一种产品类型是组产品,当产品是组产品时,我们需要浏览组产品的所有子产品,并询问所有子产品的属性。我不希望基于产品类型重复代码,所以在需要时尝试使用PHP Eval()函数启动和关闭循环。但是有人告诉我它怎么不起作用,有人能帮我吗 这是我的密码 //To Get Product Information we will call getProductInfo function $arrProdInfo =
//To Get Product Information we will call getProductInfo function
$arrProdInfo = getProductInfo($prodId);
$pName = $arrProdInfo['name'];
$pCode = $arrProdInfo['code'];
$pType = $arrProdInfo['producttype'];
//Define two Empty variable in which we will store the string to evaluate through PHP Eval() function
$topStr1 = "";
$botStr1 = "";
//If the product type is G(Group Product) then we need to loop through all the products within this group
if ($pType == "G") {
//To fetch all the products within a group product we will call getGroupProd function
$rsltGroupProd = getGroupProd($prodId);
//Set the first string to star the loop
$topStr1 = "while($rowGroupProd = $rsltGroupProd->fetchAssoc()){
$prodId = $rowGroupProd['relproductid'];
if(!is_numeric($prodId)) $prodId = 0;
$pName = $rowGroupProd['name'];
$pCode = $rowGroupProd['code'];
$pType = $rowGroupProd['producttype'];
";
//Set second string to close the loop
$botStr1 = "}";
}
//Eval() should start the loop if it's a Group Product else will not do noting
eval($topStr1);
//A big form to fetch all the attributes of product will be generated here
//Eval() should end the loop if it's a Group Product else will not do noting
eval($botStr1);
您应该将逻辑封装在函数中,并将产品类型作为参数传递。这将消除对eval()代码的需求。谢谢大家的回复 现在我把所有的产品放在一个阵列中。I给定产品不是组产品阵列将只包含一个产品&如果给定产品是组产品,阵列将包含该组产品的所有子产品。然后将在数组中导航以生成一个请求数组中所有产品属性的 但我还是想在时间允许的情况下用Eval()进行一些研发 再次感谢,
Ekta..绝对不要为此使用
eval()
。此外,您的代码丢失。你能再发一次吗?请参阅此处的常见问题解答:如果答案是eval()
,则说明您问错了问题。eval()
不是一个类似C的预处理器。传递给eval()
的代码本身必须在语法上有效。A+1只是为了让更多人了解关于如何。。。避免这种事情;-)哦,欢迎你。