[英]PHP if/else in Variable?
Hi have a bit of code in a variable and after some help from Ben Rowe I managed to realise I needed to concatinate my strings, now I can't seem to solve the following... Include if and else inside a variable. 嗨,在变量中有一些代码,在本·罗维(Ben Rowe)的帮助下,我设法意识到我需要使字符串变得简明,现在我似乎无法解决以下问题……在变量中包含if和else。 I have the below code that the IF/ELSE arn't working, if anyone can help that would be really appreciated. 我有下面的代码,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>
I tried strippimg out the PHP tags to no effect, I also tried closing the php before the below code and then opening it again but still no result :( 我尝试将strippimg淘汰掉PHP标记无效,我也尝试在以下代码之前关闭php,然后再次将其打开,但仍然没有结果:(
Use it like this - 像这样使用它-
<div class="checkout-heading"><span><?php echo $text_checkout_account ?></span></div>
or html + php 或HTML + PHP
<?php
echo '<div class="checkout-heading"><span>'.$text_checkout_account.'</span></div>';
You can't use php variables outside of the tags. 您不能在标记之外使用php变量。 If you need a variable you have to use php tags, and echo the value. 如果您需要一个变量,则必须使用php标记,并回显该值。 Do it like this: 像这样做:
<?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 } ?>
If you want to place PHP variable's content in HTML you have to do it with: 如果要将PHP变量的内容放置在HTML中,则必须执行以下操作:
<div class="checkout-heading"><span><?php echo $text_checkout_account; ?></span></div>
You can do it also in a short way with: 您也可以使用以下简短方法:
<div class="checkout-heading"><span><?=$text_checkout_account; ?></span></div>
Important: second example requires short tags activated. 重要提示:第二个示例要求激活短标签。 Since PHP 5.4.0
it's available by default. 从PHP 5.4.0
起,默认情况下可用。
Reference: http://www.php.net/manual/en/ini.core.php#ini.short-open-tag
参考: http://www.php.net/manual/en/ini.core.php#ini.short-open-tag
: http://www.php.net/manual/en/ini.core.php#ini.short-open-tag
Do like this in HTML 在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>
If you confused about echo
read about Ternary operator 如果您对echo
感到困惑,请阅读有关三元运算符的信息
You could try ternary operators, like this: 您可以尝试使用三元运算符,如下所示:
<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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.