回声/返回在变量前添加空格 [英] Echo/return is adding whitespace before variable
问题描述
我有一些代码,该代码从数据库中获取信息,然后创建图像的路径并返回它,并将其用作另一页上的img src.
I have some code, which grabs information from a database, then creates a path to an image and returns it, which I use as the img src on another page.
功能如下:
function getMainImage ()
{
$query = "SELECT * FROM pictures WHERE username = '$_SESSION[username]' AND main = 'y' LIMIT 1";
include 'connect.php';
$result = mysql_query($query) or die (mysql_error());
mysql_close($dbhandle);
$row = mysql_fetch_assoc($result);
$path = "images/t_$row[username]_$row[number].$row[ext]";
return $path;
}
<img src="<?php echo getMainImage(); ?>" width="40" height="40" />
查看页面源时的输出为:
The output when looking at the page source is:
<img src=" images/t_image_3.jpg" width="40" height="40" />
该函数在"images/"之前添加了一个额外的空间.我试过修剪路径,甚至在调用它时都修剪函数,但是都没有用.有什么想法吗?
There is an extra space being added from the function before "images/". I've tried trimming the path, and even trimming the function when calling it, but neither worked. Any ideas?
推荐答案
包括
您几乎可以看到的空白来自此行:
include
The whitespace you're seeing almost certainly comes from this line:
include 'connect.php';
检查文件connect.php
中的内容,并在打开<?php
之前删除所有空格,如果它具有?>
,则只需将其删除.
Check what you have in the file connect.php
, and delete any whitespace before the opening <?php
, and if it has a ?>
, simply delete it.
这篇关于回声/返回在变量前添加空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!