PHP echo 显示图片HTML

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26065495/
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 18:07:34  来源:igfitidea点击:

PHP echo to display image HTML

phphtmlimageecho

提问by petehallw

I am trying to display an image on my webpage using a PHP script to determine which image is displayed. The image link is as follows:

我正在尝试使用 PHP 脚本在我的网页上显示图像以确定显示哪个图像。图片链接如下:

<a href="gallery.php?image=image01">......</a>

My PHP script is thus:

我的 PHP 脚本是这样的:

<?php 
$result = $_GET['image'];
echo '<img src="images/gallery/'.$result.'.jpg">'; 
?>

So what I am trying to achieve in terms of HTML is:

所以我试图在 HTML 方面实现的是:

<img src="images/gallery/image01.jpg">

The result I am getting is '"; ?>' displayed on the page. Any help would be much appreciated!

我得到的结果是 '"; ?>' 显示在页面上。任何帮助将不胜感激!

采纳答案by Robbiegod

I would change the gallery.php to this:

我会将 gallery.php 更改为:

<?php $result = $_GET['image']; ?>
<img src="images/gallery/<?php echo $result; ?>.jpg">

That would simply it a little bit. You should echo out the result to see what you are getting when the variable is passed to the gallery page.

那只是一点点。您应该回显结果以查看将变量传递到图库页面时得到的结果。

回答by Lal

You have to change your code like this

你必须像这样改变你的代码

<?php
$result = $_GET['image'];
?>
<img src="images/gallery/<?php echo $result; ?>.jpg">

回答by Eugene

<?php
    $result = filter_input ( INPUT_GET , 'image' );
    if (isset($result) && !empty($result)) {
        echo '<img src="images/gallery/'.$result.'.jpg">';
    }
?>

回答by ExCluSiv3

You used echo wrong, here is how you should use it.

您使用 echo 错误,这是您应该如何使用它。

<?php 
$result = $_GET['image'];
?>

<img src="images/gallery/<?php echo $result ?>.jpg">

回答by Billie Jean

echo"<img src='{$image}'>";

$image = uploads/myImage.jpg

$image = uploads/myImage.jpg

I think this is the simplest code. To use a php variable while echoing out html, use curly {}brackets to insert any php variable. For instance, a file upload...

我认为这是最简单的代码。要在回显 html 时使用 php 变量,请使用大{}括号插入任何 php 变量。例如,文件上传...

<?php

    if(isset($_POST['submit'])){

        $filename=$_FILES['file']['name'];
        $temp_dir=$_FILES['file']['tmp_name'];
        $image = "img/".$filename;
        }
?>

回答by Rakesh Maurya

 <?php if($row2['pack1']==1){ echo "<img src=".BASE_URL."images/1seo.png";  } ?>