【问题标题】:PHP code in Variable?变量中的PHP代码?
【发布时间】:2012-09-03 23:49:26
【问题描述】:

我想将一些 php 应用到一个变量,并想知道如何做到这一点。我正在改编一段写成PHP opening closing times 的代码,并想更改它。本质上我希望下面的代码工作......

$open_output = '

<span class="label"><?php echo $text_qty; ?></span>

      <input type="text" name="quantity" size="2" value="<?php echo $minimum; ?>" id="qty"/>
      <input type="hidden" name="product_id" size="2" value="<?php echo $product_id; ?>" />
      <a id="button-cart" class="button" title="<?php echo $button_cart; ?>"><span><?php echo $button_cart; ?></span></a> ';

关于如何做到这一点的任何想法?

完整的代码可以在资源中找到。

【问题讨论】:

标签: php variables include


【解决方案1】:

您需要将字符串和变量连接在一起

$open_output = '<span class="label">'.$text_qty.'</span>
<input type="text" name="quantity" size="2" value="'.$minimum.'" id="qty"/>
<input type="hidden" name="product_id" size="2" value="'.$product_id.'" />
<a id="button-cart" class="button" title="'.$button_cart.'"><span>'.$button_cart.'</span></a> ';

【讨论】:

    【解决方案2】:

    不对,但是可以用eval('?&gt;'. $open_output)执行php代码。您可以使用 get_file_contents() 函数从变量上的文件加载模板并使用 eval() 执行它。

    【讨论】:

    • 是否有可能以某种方式停止 PHP 代码的执行,并启动另一个进程并计算一个可以带回初始代码并使用 'eval' 运行的字符串?
    【解决方案3】:
    $open_output = <<< EOF
    <span class="label"><?php echo $text_qty; ?></span>
    <input type="text" name="quantity" size="2" value="<?php echo $minimum; ?>" id="qty"/>
    <input type="hidden" name="product_id" size="2" value="<?php echo $product_id; ?>" />
    <a id="button-cart" class="button" title="<?php echo $button_cart; ?>"><span><?php echo $button_cart; ?></span></a>
    EOF;
    echo $open_output;
    

    删除

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-08-27
      • 1970-01-01
      • 1970-01-01
      • 2014-06-03
      • 2013-01-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多