PHP嵌套变量在回显的字符串中,最后包含一个HTML标记 [英] PHP nest variable in echoed string that contains a HTML tag in the end

查看:125
本文介绍了PHP嵌套变量在回显的字符串中,最后包含一个HTML标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

需要php帮助

我需要在html标签内添加if语句

I need to add the if statement inside the html tag

if( get_field($image) ):
    ?><img src="<?php the_field($image); ?>" alt="" /><?php
endif;

我想在下面的html标签中添加if语句代替img和a,是可能吗?

I want to add the if statement inside the html tag below in place of img and a, is that possible?

 echo 

    "<$html class=\"" .(!empty($this->options['class']) ? trim($thesis->api->esc($this->options['class'])) : ''). "\">  

 // I want this if statement to work inside here...how do i escape the html to make it work?

if( get_field($image) ):
        ?><img src="<?php the_field($image); ?>" alt="" /><?php
    endif;


    <img src=\"" .get_field($image)."\" alt=\"\" /> 
     <a href=\"".get_field($download). "\"> Download File</a> 

    </$html>\n";


推荐答案

将PHP运算符放入HTML



首先,不要使用 echo 语句来吐出大量的HTML,这会使代码很难维护和重新使用。这不容易吗?

Putting PHP operators in HTML

First off, don't use the echo statement to spit out huge chunks of HTML, it makes code very hard to maintain and re-use. Isn't this easier?

<a href='<?php echo $some_variable; ?>'>

在HTML块中使用PHP逻辑(一般)

你正在寻找这样的东西:

You're looking for something like this:

<?php if(!empty($image)): ?>
    <img src='<?php echo $image; ?>' alt='Some Stuff'>
<?php endif; ?>

这是一个名为三元运算符,可能更容易在代码中阅读:

This is a short-hand equivelant called a ternary operator which may be easier to read in code:

<?php echo empty($image) ? null : "<img src='$image' alt='Some Stuff'>"; ?>

如果 $ image 有一个值,如果没有,则没有。

This will echo an image tag if $image has a value, and nothing if it doesn't.

您的代码看起来像是故意混淆以混淆人们。学会缩进,不要在逻辑中嵌入逻辑。优先考虑可读性,您的代码将更容易维护。

Your code looks like it has been deliberately obfuscated to confuse people. Learn to indent, don't embed logic within logic. Prioritize readability and your code will be a lot easier to maintain.

if(!empty($text)) 
        echo 

"<$html class=\"" .(!empty($this->options['class']) ? trim($thesis->api->esc($this->options['class'])) : ''). "\">  

<img src=\"" .get_field($image)."\" alt=\"\" />  " .get_field($text)." 
<a href=\"".get_field($download). "\"> Download File</a> 

</$html>\n";

这里有很多可以改进的地方。首先,尽可能将业务逻辑从显示逻辑中分离出来:

There is a lot that can be improved here. First of all, separate business logic out from display logic as much as possible:

业务逻辑

<?php
    // This should be in another FILE ideally...
    $this->divClass = empty($this->options['class']) ? null : trim($thesis->api->esc($this->options['class']));
    $this->image    = the_field($image);
    $this->download = the_field($download);
    $this->text     = // I dont know how you're setting this.
?>

显示逻辑

接下来,丢失 get_field 函数,添加 null 返回 the_field 如果找不到,那么你的代码就更清晰了。然后,只需使用以下内容:

Next, lose the get_field functions, add a null return to the_field if it's not found, that way you have cleaner code. Then, just use something like this:

<?php if(!isset($this->text)): ?>
    <div class='<?php echo $divClass; ?>'>
    <?php if(!isset($this->image) && !isset($this->download)): ?>
        <img src='<?php echo $this->image; ?>'>
        <a href='<?php echo $this->download; ?>'>Download File</a>
    <?php endif; ?>
    </div>
<?php endif; ?>

<?php> 标签是它们可以帮助您,它们允许您使用HTML代码干净地插入PHP代码,大多数语言都必须采用丑陋的外部诱惑。使用它们,保持代码的可读性和可理解性,不要走捷径,因为它们会回来咬你。

The <?php> tags are there to help you, they allow you to cleanly interpolate PHP code with HTML code in a way that most languages have to resort to ugly external tempating for. Use them, keep your code readable and understandable, don't take shortcuts because they will come back to bite you.

这篇关于PHP嵌套变量在回显的字符串中,最后包含一个HTML标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆