php 如何用php回显图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12552457/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:47:02  来源:igfitidea点击:

How to echo an image with php

phpwordpressechoimage

提问by user1636968

I'm trying to echo an image from my '_images' folder, but if I write the following code, it is only for my website:

我正在尝试从我的“_images”文件夹中回显图像,但如果我编写以下代码,它仅适用于我的网站:

echo '<img src="http://mywebsite.com/mytheme/wp-content/themes/my theme/_images/project3image1.jpg">';

I've changed it to this so when someone else uses my theme, it takes them to their own website directory:

我已将其更改为这样,因此当其他人使用我的主题时,会将他们带到他们自己的网站目录:

echo '<img src="<?php bloginfo('stylesheet_directory'); ?>/_images/project3image1.jpg">'; 

But there must be something wrong with this code when I put that and preview my website it gives me this error:

但是,当我放置并预览我的网站时,此代码肯定有问题,它给了我这个错误:

Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in /home/content/32/9825632/html/bomb/wp-content/themes/bomb/header.php on line 101

I'm a php noob so it's probably an obvious mistake!

我是一个 php noob,所以这可能是一个明显的错误!

回答by jeroen

You are already in php / echoing things out, so you do not need to use php tags:

你已经在 php 中/回显了,所以你不需要使用 php 标签:

echo '<img src="' . bloginfo('stylesheet_directory') . '/_images/project3image1.jpg">';

回答by Marcin Orlowski

Do not mix markup with code. You are asking for troubles and it all looses readability. Use printflike this:

不要将标记与代码混合使用。你是在自找麻烦,这一切都失去了可读性。printf像这样使用:

printf( '<img src="%s/_images/project3image1.jpg">', bloginfo('stylesheet_directory'));

But your real problem was nested quotation marks. If you use 'to delimit string boundaries, like:

但你真正的问题是嵌套引号。如果'用于分隔字符串边界,例如:

'foo'

then if string contentcontains 'too:

那么如果字符串内容也包含'

'foo'bar'

it have to be escaped like this:

它必须像这样逃脱:

'foo\'bar'

otherwise your content quotation mark would be considered closing quotation for the string. Or you can use "instead:

否则您的内容引号将被视为字符串的结束引号。或者您可以"改用:

"foo'bar"

but again - if your string content contains "then it have to be escaped:

但同样 - 如果您的字符串内容包含,"则必须对其进行转义:

"foo\"bar"

if string contains both "and 'then you need to escape only that one which is string delimiter, i.e.:

如果字符串包含两者"'那么您只需要转义字符串分隔符,即:

'foo"bar\'foo'

or

或者

"foo'bar\"foo"

回答by Manolis Agkopian

To use the second style of code you have first to close the php tags:

要使用第二种风格的代码,你必须先关闭 php 标签:

    //...your php code before echoing the img

    //here you close the tag 
    ?>

    <img src="<?=bloginfo('stylesheet_directory')?>/_images/project3image1.jpg" />

    <?php //here you open it again

    //your rest php code...

Anywhere in your php code if you want to echo some html, just close the php tags, write your html and open php tags again. But it is better to not mix html and php a lot. Try to write all your php code at the begining of you files and after it all your html. In your php code make variables with the dynamic values and inject php code inside html to output them inside any atribute or tag content.

如果你想在你的 php 代码中的任何地方回显一些 html,只需关闭 php 标签,编写你的 html 并再次打开 php 标签。但是最好不要经常混用 html 和 php。尝试在文件开头编写所有 php 代码,然后编写所有 html。在您的 php 代码中,使用动态值创建变量,并在 html 中注入 php 代码以将它们输出到任何属性或标签内容中。