[英]How to add html inside an if else statement in php?
I have this code where if something then display a link, else display another link. 我有这段代码,如果有的话然后显示一个链接,否则显示另一个链接。 the if and else are php code, and the links are html as you will know, but I was wondering how do you make it so it will not give me any errors, how do I combine php with html? if和else是php代码,链接是html,您会知道的,但是我想知道您是如何做到的,这样就不会给我任何错误,如何将php和html结合起来?
<?php foreach ($user_socs as $user_soc) {
if ($soca == $user_soc) {
<a href="file.php" class="socbuttons">Leave Society</a>;
} else {
<a href="anotherfile.php" class="socbuttons">Join Society</a>;
}
This might help you. 这可能对您有帮助。 This one is from the basics of PHP: 这是来自PHP的基础知识:
<?php foreach ($user_socs as $user_soc) {
if ($soca == $user_soc) {
echo "<a href='file.php' class='socbuttons'>Leave Society</a>";
} else {
echo "<a href='anotherfile.php' class='socbuttons'>Join Society</a>";
}
}
?>
Remember PHP should be surrounded by <?php
?>
. 记住PHP应该被<?php
?>
包围。 Simply end the "PHP part" and then start it again when you finished your HTML. 只需结束“ PHP部分”,然后在完成HTML后再次启动它。
<?php foreach ($user_socs as $user_soc) {
if ($soca == $user_soc) {
?> <a href="file.php" class="socbuttons">Leave Society</a> <?
} else {
?> <a href="anotherfile.php" class="socbuttons">Join Society</a><?
}
Also, you can write HTML in echo
statement, but you should escape some special characters like "
to not interfere with the php code itself. 另外,您可以在echo
语句中编写HTML,但是您应转义一些特殊字符,例如"
,以免干扰php代码本身。
<?php foreach ($user_socs as $user_soc) {
if ($soca == $user_soc) {
echo "<a href=\"file.php\" class=\"socbuttons\">Leave Society</a>";
} else {
echo "<a href=\"anotherfile.php\" class=\"socbuttons\">Join Society</a>";
}
See http://php.net/manual/en/function.echo.php to more information. 有关更多信息,请参见http://php.net/manual/en/function.echo.php 。
Try this 尝试这个
<?php foreach ($user_socs as $user_soc) {
if ($soca == $user_soc) { ?>
<a href="file.php" class="socbuttons">Leave Society</a>;
<?php } else { ?>
<a href="anotherfile.php" class="socbuttons">Join Society</a>;
<?php }
Use alternate syntax: 使用备用语法:
<?php
$aUserSocs = array( 'link1', 'link2', 'link3' );
$soca = 'link2';
$iCountUserSocs = count( $aUserSocs );
for( $i = 0; $i < $iCountUserSocs; ++$i ):
?>
<?php if( $soca == $aUserSocs[ $i ] ): ?>
<a href="file.php" class="socbuttons">Leave Society</a>;
<?php else: ?>
<a href="anotherfile.php" class="socbuttons">Join Society</a>;
<?php endif; ?>
<?php endfor; ?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.