简体   繁体   中英

Insert <?php echo in <?php if

<?php if($range == 'arizona'): ?>
    <?php echo $image->county; ?>
<?php endif; ?>

Ok, so this code is working, and the echo output is: "arizona"

Now I would like to replace the 'arizona' on the first line, using the echo. I tried:

<?php if($range == $image->county;): ?>
    <?php echo $image->county; ?>
<?php endif; ?>

But not working. How can I include:

    $image->county;

In the first line?

Try this

<?php if($range == $image->county){ ?>
   <?php echo $image->county; ?>
<?php } ?>

Remove the semicolon from your if expression:

<?php if($range == $image->county): ?>

You're close! Just need to remove the semicolon from the first line:

<?php if($range == $image->county): ?>
    <?php echo $image->county; ?>
<?php endif; ?>

You can also leave php open until the end of your code instead of opening and closing it on every line which can lead to poor readability. Also, you had that extra semicolon in your first line. ;D

<?php if($range == $image->county) {
    echo $image->county;
} ?>

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