0

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

2
  • 1
    Do $logged and $shipping_required return true or false? Commented Sep 4, 2012 at 9:20
  • replace '.$text_checkout_account.' with <?= $text_checkout_account ?>. similarly, all variables Commented Sep 4, 2012 at 9:24

5 Answers 5

2

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>';
2

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 } ?>
0

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

0

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

2
  • This kind of works put the text is not displaying, any ideas? Commented Sep 4, 2012 at 9:58
  • @user1644772 variables are empty Commented Sep 4, 2012 at 13:01
0

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>
1
  • This kind of works put the text is not displaying, any ideas? Commented Sep 4, 2012 at 10:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.