Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在PHP中创建和显示图像_Php_Html_Image_Function - Fatal编程技术网

如何在PHP中创建和显示图像

如何在PHP中创建和显示图像,php,html,image,function,Php,Html,Image,Function,我想用PHP显示一个图像。这是我尝试过的,但不起作用 <!DOCTYPE html> <html> <head> <title>Title</title> </head> <body> <div class="container"> <?php $img_name = echo $row['img_name']; //I get it from the database img_block(

我想用PHP显示一个图像。这是我尝试过的,但不起作用

<!DOCTYPE html>
<html>
<head>
<title>Title</title>
</head>
<body>

<div class="container">
<?php
$img_name = echo  $row['img_name']; //I get it from the database
img_block($img_name); //Display the image here.

//Function to display image
function img_block(img_src) {
    // e.g. img_src = cat.jpg;
    $img_input = "images/" . img_src;
    $set_img = '<img class="media-object-ph" src="'.$img_input.'" width="380" height="290" alt="...">';
    return $set_img;
}
?>
</div>
</body>
</html>

标题

提前谢谢。

评论时间太长了。。。您有许多错误:

$img_name = echo  $row['img_name'];
应该是:

$img_name = $row['img_name'];
echo img_block($img_name);
function img_block($img_src) {
    // e.g. img_src = cat.jpg;
    $img_input = "images/" . $img_src;
    $set_img = '<img class="media-object-ph" src="'.$img_input.'" width="380" height="290" alt="...">';
    return $set_img;
}
您正在调用函数,但没有使用返回值执行任何操作,您需要回显它:

img_block($img_name);
应该是:

$img_name = $row['img_name'];
echo img_block($img_name);
function img_block($img_src) {
    // e.g. img_src = cat.jpg;
    $img_input = "images/" . $img_src;
    $set_img = '<img class="media-object-ph" src="'.$img_input.'" width="380" height="290" alt="...">';
    return $set_img;
}
最后,您没有将所需的
$
放在函数中的
img\u src
变量上;它的定义应该是:

$img_name = $row['img_name'];
echo img_block($img_name);
function img_block($img_src) {
    // e.g. img_src = cat.jpg;
    $img_input = "images/" . $img_src;
    $set_img = '<img class="media-object-ph" src="'.$img_input.'" width="380" height="290" alt="...">';
    return $set_img;
}

尝试将
$img\u input
更改为
$img\u input=“./images/…”