How can variables be properly concatenated with file names in PHP to display images correctly?

When concatenating variables with file names in PHP to display images correctly, it is important to ensure that the file path is properly constructed. This can be done by using the concatenation operator (.) to combine the directory path with the variable containing the file name. Additionally, it is crucial to pay attention to the file extension to ensure that the correct image type is displayed.

$directory = "images/";
$imageName = "example.jpg";
$imagePath = $directory . $imageName;

echo '<img src="' . $imagePath . '" alt="Example Image">';