echo html 和变量的语法帮助

发布于 2024-11-08 06:37:08 字数 1497 浏览 0 评论 0原文

我有这段代码(见下文),我无法回显变量。第 5 行是一个 echo,在该 echo 中是一个 html 负载(我已经转义了引号)和一个变量负载。我无法让变量在主回显中回显。

更新: 其中还有一个需要考虑的 onclick。

<?php
   if(
       in_array("Branding", get_field('categories')) && $grid_title == "Branding"
   ){
     echo "
        <div class=\"grid-box\" onclick=\"location.href='<?php echo get_page_link($post->ID) ?>';\" style=\"cursor: pointer;\">
        <div class=\"phase-1\">
           <img class=\"grid-image\" src=\"<?php echo $fields->thumb_image; ?>\" alt=\"<?php echo $fields->company_name; ?>\" height=\"152\" width=\"210\" />
           <div class=\"grid-heading\">
                <h2><?php echo $fields->company_name; ?></h2>
                <h3><?php echo implode(', ',get_field('categories'));?></h3>
            </div> 
        </div>
        <div class=\"phase-2\">
            <div class=\"grid-info\">
                <h4><?php echo $fields->project_name; ?></h4>
                <p><?php echo $fields->description; ?></p>
            </div>
            <div class=\"grid-heading-hover\">
                <h2><?php echo $fields->company_name; ?></h2>
                <h3><?php echo implode(', ',get_field('categories'));?></h3>
            </div> 
        </div>
    </div>
     ";
   }
   ?>

I have this code (see below) that I'm having trouble echoing the variables out. On the 5th line is an echo, within that echo is a load of html (which I've escaped the quotation marks) and a load of variables. I can't get the variables to echo out within the main echo.

Update:
Also within there is an onclick that needs to be taken into account.

<?php
   if(
       in_array("Branding", get_field('categories')) && $grid_title == "Branding"
   ){
     echo "
        <div class=\"grid-box\" onclick=\"location.href='<?php echo get_page_link($post->ID) ?>';\" style=\"cursor: pointer;\">
        <div class=\"phase-1\">
           <img class=\"grid-image\" src=\"<?php echo $fields->thumb_image; ?>\" alt=\"<?php echo $fields->company_name; ?>\" height=\"152\" width=\"210\" />
           <div class=\"grid-heading\">
                <h2><?php echo $fields->company_name; ?></h2>
                <h3><?php echo implode(', ',get_field('categories'));?></h3>
            </div> 
        </div>
        <div class=\"phase-2\">
            <div class=\"grid-info\">
                <h4><?php echo $fields->project_name; ?></h4>
                <p><?php echo $fields->description; ?></p>
            </div>
            <div class=\"grid-heading-hover\">
                <h2><?php echo $fields->company_name; ?></h2>
                <h3><?php echo implode(', ',get_field('categories'));?></h3>
            </div> 
        </div>
    </div>
     ";
   }
   ?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

别低头,皇冠会掉 2024-11-15 06:37:14

只需在 if 之后关闭 php 标签,并在结束分号之前打开它即可。

if(
       in_array("Branding", get_field('categories')) && $grid_title == "Branding"
   ){
   ?>
        <div class="grid-box" onclick="location.href='<?php echo get_page_link($post->ID) ?>';" style="cursor: pointer;">
        <div class="phase-1">
           <img class="grid-image" src="<?php echo $fields->thumb_image; ?>" alt="<?php echo $fields->company_name; ?>" height="152" width="210" />
           <div class="grid-heading">
                <h2><?php echo $fields->company_name; ?></h2>
                <h3><?php echo implode(', ',get_field('categories'));?></h3>
            </div> 
        </div>
        <div class="phase-2">
            <div class="grid-info">
                <h4><?php echo $fields->project_name; ?></h4>
                <p><?php echo $fields->description; ?></p>
            </div>
            <div class="grid-heading-hover">
                <h2><?php echo $fields->company_name; ?></h2>
                <h3><?php echo implode(', ',get_field('categories'));?></h3>
            </div> 
        </div>
    </div>
   <?php
   }
   ?>

Just close the php tag after the if and open it before the closing semi-colon.

if(
       in_array("Branding", get_field('categories')) && $grid_title == "Branding"
   ){
   ?>
        <div class="grid-box" onclick="location.href='<?php echo get_page_link($post->ID) ?>';" style="cursor: pointer;">
        <div class="phase-1">
           <img class="grid-image" src="<?php echo $fields->thumb_image; ?>" alt="<?php echo $fields->company_name; ?>" height="152" width="210" />
           <div class="grid-heading">
                <h2><?php echo $fields->company_name; ?></h2>
                <h3><?php echo implode(', ',get_field('categories'));?></h3>
            </div> 
        </div>
        <div class="phase-2">
            <div class="grid-info">
                <h4><?php echo $fields->project_name; ?></h4>
                <p><?php echo $fields->description; ?></p>
            </div>
            <div class="grid-heading-hover">
                <h2><?php echo $fields->company_name; ?></h2>
                <h3><?php echo implode(', ',get_field('categories'));?></h3>
            </div> 
        </div>
    </div>
   <?php
   }
   ?>
撕心裂肺的伤痛 2024-11-15 06:37:13

这样的事情

echo " ... <?php ... ?> ...";

是行不通的。

相反,您可以“闯入和退出” php,例如:

...
?>
<div class="grid-box" onclick="location.href='<?php echo get_page_link($post->ID) ?>';" style="cursor: pointer;">
<div class="phase-1"> ... <?php

或使用 s/printf

printf(
    '<div class="grid-box" onclick="location.href='%s';" style="cursor: pointer;">',
    get_page_link($post->ID)
);

或使用字符串连接:

echo "html ...", get_page_link($post->ID), "some more html...";

或使用某种模板代码。

Something like this

echo " ... <?php ... ?> ...";

will not work.

Instead you could "break in and out" of php, like:

...
?>
<div class="grid-box" onclick="location.href='<?php echo get_page_link($post->ID) ?>';" style="cursor: pointer;">
<div class="phase-1"> ... <?php

Or use s/printf

printf(
    '<div class="grid-box" onclick="location.href='%s';" style="cursor: pointer;">',
    get_page_link($post->ID)
);

Or use string concatenation:

echo "html ...", get_page_link($post->ID), "some more html...";

Or use some kind of templating code.

护你周全 2024-11-15 06:37:12

您不能在 echo 中使用 echo,您只需要正确连接字符串,如下所示:

     $Content =  "
        <div class=\"grid-box\" onclick=\"location.href='" . get_page_link($post->ID). "';\" style=\"cursor: pointer;\">
        <div class=\"phase-1\">
           <img class=\"grid-image\" src=\"" . $fields->thumb_image . "\" alt=\"" . $fields->company_name. "\" height=\"152\" width=\"210\" />
           <div class=\"grid-heading\">
                <h2> " . $fields->company_name . "</h2>
                <h3>" . implode(', ',get_field('categories')) . "</h3>
            </div> 
        </div>
        <div class=\"phase-2\">
            <div class=\"grid-info\">
                <h4>" . $fields->project_name . "</h4>
                <p>" . $fields->description . "</p>
            </div>
            <div class=\"grid-heading-hover\">
                <h2>" . $fields->company_name . "</h2>
                <h3>" . implode(', ',get_field('categories')). "</h3>
            </div> 
        </div>
    </div>";
  echo $Content;

You can't use echo within an echo, you just need to concatenate your strings properly like so:

     $Content =  "
        <div class=\"grid-box\" onclick=\"location.href='" . get_page_link($post->ID). "';\" style=\"cursor: pointer;\">
        <div class=\"phase-1\">
           <img class=\"grid-image\" src=\"" . $fields->thumb_image . "\" alt=\"" . $fields->company_name. "\" height=\"152\" width=\"210\" />
           <div class=\"grid-heading\">
                <h2> " . $fields->company_name . "</h2>
                <h3>" . implode(', ',get_field('categories')) . "</h3>
            </div> 
        </div>
        <div class=\"phase-2\">
            <div class=\"grid-info\">
                <h4>" . $fields->project_name . "</h4>
                <p>" . $fields->description . "</p>
            </div>
            <div class=\"grid-heading-hover\">
                <h2>" . $fields->company_name . "</h2>
                <h3>" . implode(', ',get_field('categories')). "</h3>
            </div> 
        </div>
    </div>";
  echo $Content;
醉生梦死 2024-11-15 06:37:12

你这里好像有一点误会。

在 PHP 中,您可以通过

  • 外部编写简单的 HTML 来输出 HTML,也可以
  • 使用 echo / < 等方式 在内部插入一些 PHP 代码。 code>print 输出 PHP 字符串包括 HTML

但你不能混淆两者。

所以这不好:

echo "... src=\"<?php echo $fields->thumb_image; ?>\" ...";

因为这是一个字符串,并且在字符串中您无法打开 部分。您应该这样做:

echo "... src=\"{$fields->thumb_image}\" ...";

这是在字符串中插入 PHP 变量的很酷的方法之一。

You seem to have a little misunderstanding here.

In PHP you can output HTML either by

  • writing simple HTML outside <?php...?>, and possibly inserting some PHP code inside
  • using something like echo / print to output a PHP string which includes HTML

But you cannot mix up the two.

So this is not good:

echo "... src=\"<?php echo $fields->thumb_image; ?>\" ...";

Because this is a string, and in a string you can not open a <?php section. You should do something like this instead:

echo "... src=\"{$fields->thumb_image}\" ...";

which is one of the cool ways to insert PHP variables inside a string.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文