在回音中创建带有回音的php if语句

在回音中创建带有回音的php if语句,php,echo,Php,Echo,大家早上好,我正在尝试做下面的工作,这样如果类型不是“main”,区域名称就会在li中重复出现。然而,它只是输出字符串中的内容,有没有一种方法可以成功地实现这一点,并节省我创建800英尺页面的时间 <?php if ($type=="main" ) { echo ' <ul class="footer-nav"> <h2>Links</h2>

大家早上好,我正在尝试做下面的工作,这样如果类型不是“main”,区域名称就会在li中重复出现。然而,它只是输出字符串中的内容,有没有一种方法可以成功地实现这一点,并节省我创建800英尺页面的时间

        <?php if ($type=="main" ) {
                echo '
                <ul class="footer-nav">
                <h2>Links</h2>
                <li><a href="index.php">Home</a></li>
                <li><a href="services.php">Our Services</a></li>
                <li><a href="upvc.php">Upvc repairs</a></li>
                <li><a href="emergency.php">Emergency 24/7</a></li>
              </ul>';}
              else
                echo '
                <ul class="footer-nav">
                <h2>Links</h2>
                <li><a href="locksmiths-<?php echo $areaname ?>.php">Home</a></li>
                <li><a href="services-<?php echo $areaname ?>.php">Our Services</a></li>
                <li><a href="services-upvc-repairs-<?php echo $area-name ?>.php">Upvc repairs</a></li>
                <li><a href="services-<?php echo $area-name ?>-24hour.php">Emergency 24/7</a></li>
              </ul>'
  ?>

这应该可以解决您的问题。 你忘了做一个适当的陈述

        <?php if ($type=="main" ) {
            echo '
            <ul class="footer-nav">
            <h2>Links</h2>
            <li><a href="index.php">Home</a></li>
            <li><a href="services.php">Our Services</a></li>
            <li><a href="upvc.php">Upvc repairs</a></li>
            <li><a href="emergency.php">Emergency 24/7</a></li>
          </ul>';}
          else {
            echo '
            <ul class="footer-nav">
            <h2>Links</h2>
            <li><a href="locksmiths-'.$areaname.'.php">Home</a></li>
            <li><a href="services-'.$areaname.'.php">Our Services</a></li>
            <li><a href="services-upvc-repairs-'.$areaname.'.php">Upvc repairs</a></li>
            <li><a href="services-'.$areaname.'-24hour.php">Emergency 24/7</a></li>
          </ul>';}
?>

您有一些语法错误,请这样使用

<?php if ($type=="main" ) {
                echo '
                <ul class="footer-nav">
                <h2>Links</h2>
                <li><a href="index.php">Home</a></li>
                <li><a href="services.php">Our Services</a></li>
                <li><a href="upvc.php">Upvc repairs</a></li>
                <li><a href="emergency.php">Emergency 24/7</a></li>
              </ul>';
            }else{
                echo '
                <ul class="footer-nav">
                <h2>Links</h2>
                <li><a href="locksmiths-'.$areaname.'.php">Home</a></li>
                <li><a href="services-'.$areaname.'.php">Our Services</a></li>
                <li><a href="services-upvc-repairs-'.$areaname.'.php">Upvc repairs</a></li>
                <li><a href="services-'.$areaname.'-24hour.php">Emergency 24/7</a></li>
              </ul>';
            }
  ?>


确保变量名,如我将
$area name
替换为
$areaname

无需将echo放入echo中。。请遵循以下更改

<?php if ($type=="main" ) :
            echo '
            <ul class="footer-nav">
            <h2>Links</h2>
            <li><a href="index.php">Home</a></li>
            <li><a href="services.php">Our Services</a></li>
            <li><a href="upvc.php">Upvc repairs</a></li>
            <li><a href="emergency.php">Emergency 24/7</a></li>
          </ul>';
          else:
            echo '
            <ul class="footer-nav">
            <h2>Links</h2>
            <li><a href="locksmiths-'.$areaname.'.php">Home</a></li>
            <li><a href="services-.'$areaname.'.php">Our Services</a></li>
            <li><a href="services-upvc-repairs-.'$area-name.'.php">Upvc repairs</a></li>
            <li><a href="services-'.$area-name.'-24hour.php">Emergency 24/7</a></li>
          </ul>'
    endif;?>

您可以用HTML编写php代码,而不必使用双引号,如下所示:

<?php if ($type == "main"): ?>
    <ul class="footer-nav">
         <h2>Links</h2>
         <li><a href="index.php">Home</a></li>
         <li><a href="services.php">Our Services</a></li>
         <li><a href="upvc.php">Upvc repairs</a></li>
         <li><a href="emergency.php">Emergency 24/7</a></li>
     </ul>
<?php else: ?>
    <ul class="footer-nav">
        <h2>Links</h2>
        <li><a href="locksmiths-<?= $areaname ?>.php">Home</a></li>
        <li><a href="services-<?= $areaname ?>.php">Our Services</a></li>
        <li><a href="services-upvc-repairs-<?= $area-name ?>.php">Upvc repairs</a></li>
        <li><a href="services-<?= $area-name ?>-24hour.php">Emergency 24/7</a></li>
    </ul>
<?php endif; ?>

    链接
    链接
最短的代码

像我一样,您只能回显一次公共数据

变量
$area name
无效。。。我将其转换为$area\u名称

<?php 
    echo '<ul class="footer-nav"><h2>Links</h2>'; //common data
if ($type=="main")
    echo '
        <li><a href="index.php">Home</a></li>
        <li><a href="services.php">Our Services</a></li>
        <li><a href="upvc.php">Upvc repairs</a></li>
        <li><a href="emergency.php">Emergency 24/7</a></li>';
else echo '
        <li><a href="locksmiths-'.$areaname.'.php">Home</a></li>
        <li><a href="services-'.$areaname.'.php">Our Services</a></li>
        <li><a href="services-upvc-repairs-'.$area_name.'.php">Upvc repairs</a></li>
        <li><a href="services-'.$area_name.'-24hour.php">Emergency 24/7</a></li>';
echo '</ul>';//common data
?>
  • $area name
    是无效的变量名,可能是输入错误

  • 看到你的html,我建议你浏览一下

下面是另一种提高可读性的方法,首先删除
echo

<?php if ($type=="main" ): ?>
              <ul class="footer-nav">
                <h2>Links</h2>
                <li><a href="index.php">Home</a></li>
                <li><a href="services.php">Our Services</a></li>
                <li><a href="upvc.php">Upvc repairs</a></li>
                <li><a href="emergency.php">Emergency 24/7</a></li>
              </ul>
<?php else: ?>
              <ul class="footer-nav">
                <h2>Links</h2>
                <li><a href="locksmiths-<?php echo $areaname; ?>.php">Home</a></li>
                <li><a href="services-<?php echo $areaname; ?>.php">Our Services</a></li>
                <li><a href="services-upvc-repairs-<?php echo $areaname; ?>.php">Upvc repairs</a></li>
                <li><a href="services-<?php echo $areaname; ?>-24hour.php">Emergency 24/7</a></li>
              </ul>
<?php endif;?>

    链接
    链接

更好的方法是使用三元运算符,这样您就不需要重复HTML,只需更改链接:

<ul class="footer-nav">
    <h2>Links</h2>
    <li><a href="<?= ($type == "main" ? 'index.php' :     'locksmiths-'.$areaname.'.php') ?>">Home</a></li>
    <li><a href="<?= ($type == "main" ? 'services.php' :  'services-'.$areaname.'.php') ?>">Our Services</a></li>
    <li><a href="<?= ($type == "main" ? 'upvc.php' :      'services-upvc-repairs-'.$areaname.'.php') ?>">Upvc repairs</a></li>
    <li><a href="<?= ($type == "main" ? 'emergency.php' : 'services-'.$areaname.'-24hour.php') ?>">Emergency 24/7</a></li>
</ul>
    链接
或者,您可能希望将HTML中的数据提取到一个数组中,这将使添加更多链接或使链接更具动态性(在数据库中)更加容易

<?php
$links = [
    [
        'name' => 'Home',
        'main' => 'index.php',
        'area' => 'locksmiths-'.$areaname.'.php'
    ], [
        'name' => 'Our Services',
        'main' => 'services.php',
        'area' => 'services-'.$areaname.'.php'
    ], [
        'name' => 'Upvc repairs',
        'main' => 'upvc.php',
        'area' => 'services-upvc-repairs-'.$areaname.'.php'
    ], [
        'name' => 'Emergency 24/7',
        'main' => 'emergency.php',
        'area' => 'services-'.$areaname.'-24hour.php'
    ],
];
?>
<ul class="footer-nav">
    <h2>Links</h2>
    <?php foreach ($links as $link): ?>
    <li><a href="<?= ($type == "main" ? $link['main'] : $link['area']) ?>"><?= $link['name'] ?></a></li>
    <?php endforeach ?>
</ul>

    链接
$areaname='wales';
$pages=[
'index.php'=>'Home',
'services.php'=>'我们的服务',
'upvc.php'=>'upvc修复',
'emergency.php'=>'emergency 24/7'
];
$area\U URL=[
“锁匠-$areaname.php”,
“服务-$areaname.php”,
“服务upvc修复-$areaname.php”,
“服务-$areaname-24hour.php”
];
如果($type!=“main”)
$pages=array\u combine($area\u url,$pages);
//输出菜单
回显“链接\n\n”;

比其他答案更清晰,但怀疑它是否符合OP的要求。@sakuto,我猜他无法评估echo的其他部分。我想这应该对他有帮助。没有测试它!它的
type==main
不是
type==mail
谢谢大家这么快的回复!我认为h2是ul的直系子是无效的html。很容易,获胜者:)我只能投一个作为正确答案,尽管他们是,这是我使用的答案
$areaname = 'wales';
$pages = [
    'index.php' => 'Home',
    'services.php' => 'Our Services',
    'upvc.php' => 'Upvc repairs',
    'emergency.php' => 'Emergency 24/7'
];
$area_urls = [
    "locksmiths-$areaname.php",
    "services-$areaname.php",
    "services-upvc-repairs-$areaname.php",
    "services-$areaname-24hour.php"
];
if($type != 'main')
    $pages = array_combine($area_urls, $pages);
// Output menu
echo "<h2>Links</h2>\n<ul class='footer-nav'>\n";
    foreach($pages as $link => $page)
        printf("\t<li><a href='%s'>%s</a></li>\n", $link, $page);
echo "</ul>\n";