concatenating strings in php, why doesn't this work?

GeX

GeX

Soldato
Joined
17 Dec 2002
Posts
6,981
Location
Manchester
Code:
<?
    $dir = "img/whittingham";

    if (is_dir($dir)) {
        $fd = @opendir($dir);

        if($fd) {
            while (($part = @readdir($fd)) == true) {
                if ($part != "." && $part != "..") {
                    $file_array[] = $part;
                }
            }
        }
    }


for ($x=0; $x < sizeof($file_array); $x++) {
      echo "<img src=\"". $dir."/".$file_array[$x] ."\"><br />";

}
?>

right.. that does what it is supposed to, scans the dir and outputs all the images. now suppose i want to add an alt elemenent to the img tag, surely it would be

Code:
."alt="moo""

meaning the output line is;

Code:
echo "<img src=\"". $dir."/".$file_array[$x] .alt"moo""."\"><br />";

but if i do that, i get a parser error. i'm a newbie at php, and i can't see where i'm going wrong here.
 
yeah, you need to escape your double quotes as you're using them as your string delimiter:

Code:
."alt="moo""

goes to

Code:
."alt=\"moo\""

Code:
echo "<img src=\"". $dir."/".$file_array[$x] ."alt=\"moo\""."\"><br />";

if you're breaking out of quotes to add in variables, I'd use single quotes for strings as they're quicker to parse and less confusing if you're using double quotes in html:

Code:
echo '<img src="'. $dir.'/'.$file_array[$x] .'" alt="moo"'.'><br />';
 
Last edited:
first of all, i'd use a foreach loop instead of counting the array....

Code:
foreach($file_array as $file) {

and when echoing html, open/close with single quotes. now you don't have to escape the double quotes..... eg

Code:
echo '<img src="blah">';

so in the end you end up with this....

Code:
foreach($file_array as $file) {
    echo '<img src="' . $file . '" alt="moo" /><br />';
}

edit: only 5 minutes too late. :o :D
 
Last edited:
It's not working because PHP thinks the quotes around the 'moo' attribute value are delimiting the string itself. Either add an escape character (\), as you have done for the src attribute, or use single quotes instead. It's usually best to use single quotes when constructing HTML output because it avoids this sort of problem.

Also, it's more concise to use a foreach loop when iterating over an array like that:
Code:
foreach ($file_array as $file)
{
    echo '<img src="' . $dir . '/' . $file .'" alt="moo"><br />';
}

Too late :(
 
I'd normally hold back from recommending this because it's more of an "I do it this way" type thing, but screw it; I'd do it this way:

Code:
printf('<img src="%s/%s" alt="moo"><br />',$dir,$file);

makes it much easier to read instead of having those dirty .'s everywhere
 
thanks for the help guys, so many different ways of doing the same thing eh. i'll modify the script to use foreach instead, and pick my new favourite way of making strings :D
 
heredoc is dirty. I always take a shower after using it. Even in the office.
Hehe. Yeah, I can't actually recall a time when I've needed to use it over any other method. The requirement of having the closing delimiter at the very start of a new line has always bothered me; I like neatly indented code too much :D.
 
Back
Top Bottom