syntax error - php - echo image (name and extension)

276 views Asked by At

I have 2 variables - $imgs which holds the name of an image, and $ex which is the image extension.

I am trying to echo the image.

I am facing a syntax error.

  echo '<div align = "center"><img src ="'.$imgs.'"."'.$ex" /></div>';
3

There are 3 answers

0
Albzi On BEST ANSWER

Try this:

echo '<div align = "center"><img src ="'.$imgs.'"."'.$ex.'" /></div>';

You weren't closing your ' properly or using . in the echo when concatenating. Make sure you use a proper syntax highlighting tool, such as Sublime Text or Notepad++. This makes spotting errors like this much easier.

0
kero On
"'.$imgs.'"."'.$ex"

needs to be

"'.$imgs.'.'.$ex.'"

You must properly concatenate the variable here, also you added " around the . which would have resulted in foo"."jpg for example. You should have been able to see this from the syntax highlighting (of SO or your editor).

0
ventsi.slav On

You're messing the " ' with the . First of all:Try to write with some style! For example every time when I have to concatenate something I`m using the empty space for more clear view.

'<div align = "center"><img src ="' . $imgs . '.' . $ex . '"/></div>';

That way it`s really more clear where ' or " starts and ends.