简体   繁体   中英

php code inside <option> tag

How can I embed php code inside of HTML option tag.

Currently my code runs as following:

    $last_y = date("Y") -1;
    echo "<option value='Annual'>Annual - $last_y</option>";

But I would like to get rid of the $last_y variable and use "date("Y") -1" inside the option tag. Have tried different escaping, but this didn't work.

Do something like this :

<?php 
 //code
$last_y = date("Y") -1; ?>
<option value='Annual'>Annual - <?php echo $last_y;?> </option>

or

<?php echo "<option value='Annual'>Annual - ".$last_y."</option>"; ?>

这应该可以完成这项工作:

    <?php echo "<option value='Annual'>Annual - ".(date("Y") -1)."</option>"; ?>

Try this:

<?php $last_y = date("Y") -1; ?>
<option value='Annual'>Annual - <?php echo $last_y; ?></option>

To get rid of the PHP code inside your string you can use printf() like so

printf('<option value="Annual">Annual - %u</option>', date('Y') - 1);

This comes in especially handy when you want to use the same value multiple times

printf('<option value="Annual %1$u">Annual - %1$u</option>', date('Y') - 1);

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