2012-09-04 46 views
0

你好在變量中有一些代碼,並且在Ben Rowe的一些幫助之後,我意識到我需要對我的字符串進行協調,現在我似乎無法解決以下問題......將if和else包含在變量中。我有下面的代碼,IF/ELSE arn't工作,如果任何人都可以幫助,將非常感激。PHP if/else在變量中?

<?php if (!$logged) { ?> 
<div id="payment-address"> 
    <div class="checkout-heading"><span>'.$text_checkout_account.'</span></div> 
    <div class="checkout-content"></div> 
</div> 
<?php } else { ?> 
<div id="payment-address"> 
    <div class="checkout-heading"><span>'.$text_checkout_payment_address.'</span></div> 
    <div class="checkout-content"></div> 
</div> 
<?php } ?> 
<?php if ($shipping_required) { ?> 
<div id="shipping-address"> 
    <div class="checkout-heading">'.$text_checkout_shipping_address.'</div> 
    <div class="checkout-content"></div> 
</div> 
<div id="shipping-method"> 
    <div class="checkout-heading">'.$text_checkout_shipping_method.'</div> 
    <div class="checkout-content"></div> 
</div> 
<?php } ?> 
<div id="payment-method"> 
    <div class="checkout-heading">'.$text_checkout_payment_method.'</div> 
    <div class="checkout-content"></div> 
</div> 

<div id="confirm"> 
    <div class="checkout-heading">'.$text_checkout_confirm.'</div> 
    <div class="checkout-content"></div> 
</div> 

我試圖strippimg了PHP標籤沒有影響,我也試過下面的代碼之前關閉PHP,然後再次打開它,但還是沒有結果:(

+1

'$ logged'和'$ shipping_required'返回'true'還是'false'? –

+0

用'<?= $ text_checkout_account?>'替換'。$ text_checkout_account.''。同樣,所有變數 – Prasanth

回答

2

使用方法如下 -

<div class="checkout-heading"><span><?php echo $text_checkout_account ?></span></div> 

或HTML + PHP

<?php 

echo '<div class="checkout-heading"><span>'.$text_checkout_account.'</span></div>'; 
0

如果你想放在HTML PHP變量的內容你必須做到這一點的:

<div class="checkout-heading"><span><?php echo $text_checkout_account; ?></span></div> 

你可以做到這一點也是在很短的方式:

<div class="checkout-heading"><span><?=$text_checkout_account; ?></span></div> 

重要:第二個例子需要激活短標籤。由於PHP 5.4.0默認可用。

參考:http://www.php.net/manual/en/ini.core.php#ini.short-open-tag

2

不能使用標籤的PHP之外的變量。如果你需要一個變量,你必須使用php標籤,並回顯這個值。像這樣做:

<?php if (!$logged) { ?> 
    <div id="payment-address"> 
    <div class="checkout-heading"><span><?php echo $text_checkout_account ?></span></div> 
    <div class="checkout-content"></div> 
    </div> 
<?php } else { ?> 
    <div id="payment-address"> 
    <div class="checkout-heading"><span><?php echo $text_checkout_payment_address ?></span></div> 
    <div class="checkout-content"></div> 
    </div> 
<?php } ?> 
0

做這樣的HTML

<div id="payment-address"> 
    <div class="checkout-heading"> 
    <span> 
     <?php echo $logged ? $text_checkout_payment_address : $text_checkout_account?> 
    </span> 
    </div> 
    <div class="checkout-content"></div> 
</div> 

如果你感到困惑echo瞭解Ternary operator

+0

這類作品擺放的文字是不是顯示,有什麼想法? – WackyRacer8

+0

@ user1644772變量爲空 –

0

你可以嘗試三元運營商,像這樣:

<div id="payment-address"> 
    <div class="checkout-heading"><span><? echo ($logged?$text_checkout_payment_address:$text_checkout_account); ?></span></div> 
    <div class="checkout-content"></div> 
</div> 
+0

這類作品放文字不顯示,有什麼想法? – WackyRacer8

+0

是的,只有一個:你的變量是否有值... – tborychowski