I am trying to echo an image within a tag plus the ID and EXT of the image but it does not seem to work.
This the line of code that produces the error:
echo '<div><img src="gallery/',$gimage['id'],'.',$gimage['ext']', height="auto" width="auto" border="2"></div>';
I tried using the exact query without the variable and it turns out to be ok D:
THIS ONE runs perfectly /
echo '<div><img src="gallery/13.png" height="auto" width="auto" border="2"></div>';
EDIT: The error message '( ! ) Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING, expecting ',' or ';' in C:\wamp\www\Final Project\view_gallery.php on line 22 '
The concatenation operator in PHP is .
and not ,
as you did.
Edit :
Seems I was wrong on the anwser. Your error was that you forgot a single quote in your code.
However I must add that multiple param echo is SLOW as hell, around 30% slower than echo with concatenation, and even without taking PHP ZendEngine's string caching in account.
You changed a ,
with a '
at one point:
echo '<div><img src="gallery/',$gimage['id'],'.',$gimage['ext'],' height="auto" width="auto" border="2"></div>';
---------------------------------------------------------------^^
Mathieu 'OtaK' Amiot is right... The concatenation operator in PHP is .
and not ,
- however, you could just leave it all in the string... Like this:
echo "<div><img src='gallery/$gimage['id'].$gimage['ext']' height='auto' width='auto' border='2'></div>";
PHP will put the variables into the string for you, but I believe breaking out and concatenating is more proper.
Consider using printf() or sprintf() for complex statements, since you can spot errors more easily:
printf(
'<div><img src="gallery/%s.%s" height="auto" width="auto" border="2"></div>',
$gimage['id'],
$gimage['ext']
);
Sprintf() will return a string, in case you need it for further processing.
You have a typo here:
echo '<div><img src="gallery/',$gimage['id'],'.',$gimage['ext']', height="auto" width="auto" border="2"></div>';
^
It should be:
echo '<div><img src="gallery/',$gimage['id'],'.',$gimage['ext'],' height="auto" width="auto" border="2"></div>';
You could also consider using printf()
:
printf('<div><img src="gallery/%s.%s" height="auto" width="auto" border="2"></div>',
$gimage['id'],
$gimage['ext']
);