简体   繁体   中英

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. I have the below code that the IF/ELSE arn't working, if anyone can help that would be really appreciated.

 <?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 :(

Use it like this -

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

or html + php

<?php

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

You can't use php variables outside of the tags. If you need a variable you have to use php tags, and echo the value. 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:

<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.

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

Do like this in 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

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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