简体   繁体   English

单引号字符串中包含变量的语法

[英]Syntax for including variable in single quoted string

Sorry for the trivial post, but I cannot figure this out on my own. 对不起的帖子,很抱歉,但是我不能自己弄清楚。

So I am basically appending a div and I need the maximum-length class to have a PHP variable like maximum-length-$max but I cannot figure out the syntax I need to use. 因此,我基本上要追加一个div,我需要maximum-length类具有一个PHP变量,例如maximum-length-$max但是我无法弄清楚我需要使用的语法。

I have tried what is done before: '.$max.' 我尝试过之前做过的事情: '.$max.' ,but this does not work. ,但这不起作用。

$addressFields .= '<div style="position: relative"><input type="text" title="'.$this->__('Street Address '.($_i+1)).'" name="billing[street]['.$_i.']" id="billing:street'.($_i+1).'" value="'.$dataHelper->clearDash($this->getQuote()->getBillingAddress()->getStreet($_i+1)).'" class="'.(($_i == 0)? 'required-entry ' : ''). 'input-text validate-address-lenght maximum-length-$max />';

That is one terrible one-liner to read. 那是一本可怕的单书。

I would do the following using sprintf : 我将使用sprintf执行以下操作:

    $addressFields .= sprintf('
        <div style="position: relative">
            <input type="text"
                    title=%s
                    name="billing[street][%s]"
                    id="billing:street%s"
                    value="%s"
                    class="%s input-text validate-address-lenght maximum-length-%s"
             />',
        $title = $this->__('Street Address ' . ($_i + 1)),
        $name = $_i,
        $id = $_i + 1,
        $value = $dataHelper->clearDash($this->getQuote()->getBillingAddress()->getStreet($_i + 1)),
        $class = ($_i === 0) ? 'required-entry ' : '',
        $max = 'YOUR_MAX_VALUE_HERE'
    );

Also shouldn't "validate-address-lenght" be "validate-address-length" in your class values? 另外,在类值中,“ validate-address-lenght”是否不应该是“ validate-address-length”?

Try to close ' before and after you varable. 尝试在变量前后关闭' Try following code: 尝试以下代码:

$addressFields .= '<div style="position: relative"><input type="text" title="'.$this->__('Street Address '.($_i+1)).'" name="billing[street]['.$_i.']" id="billing:street'.($_i+1).'" value="'.$dataHelper->clearDash($this->getQuote()->getBillingAddress()->getStreet($_i+1)).'" class="'.(($_i == 0)? 'required-entry ' : ''). 'input-text validate-address-lenght maximum-length-'.$max.'" />';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM