问题
I was making a school assignment with involves a shoutbox. A found great tutorial wich uses jquery,ajax,mysql and php. Now i run into a little problem with the following sentence:
$result .= "<li><strong>".$row['user']."</strong><img src="\" alt="\"-\"" />".$row['message']." <span class="\"date\"">".$row['date']."</span></li>";}
I was wondering if anybody could find out why it gives errors. So far I came to this conclusion $row['message']
and then it thinks the rest of the code as a string. So it probably is a apostrophe problem.
回答1:
Just for the sake of making your life easier: use '
for the php and "
for html like this:
$result .= '<li><strong>'.$row['user'].'</strong><img src="" alt=""/>'.$row['message'].' <span class="date">'.$row['date'].'</span></li>';
Pretty sure you should get the idea.
回答2:
$result .= "<li><strong>{$row['user']}</strong><img src='http://www.' alt='My Alt Tag' />{$row['message']}<span class='date'>{$row['date']}</span></li>";
You're confusing yourself by coming in and out of quotations - you can wrap variables with {} to force the interpolation in such cases.
回答3:
$result .= "<li><strong>".$row['user']."</strong><img src='' alt='-'/>".$row['message']." <span class='date'>".$row['date']."</span></li>";}
Avoid using " inside of the string - it is easy to forget about escaping special chars. Instead of " use '. Besides - you use " only when there is any PHP parsing necessary within this string. E.g.
$var1 = 1;
$test = "$var1"; //evaluates to '1'
$test = '$var1'; //evaluates to '$var1'
回答4:
It appears that you are attempting to escape quotes and making your job harder. A great feature in PHP for HTML output is using quoted strings so that you don't have to worry about escaping double quotes. Please reference the PHP Manual for Strings.
In other words your line becomes:
$result .= '<li><strong>' . $row['user'] . '</strong><img src="" alt="-" />' . $row['message'] .
'<span class="date">' . $row['date'] . '</span></li>' .
'<li><strong>' . $row['user'] . '</strong><img src="" alt="-" />' . $row['message'] .
'<span class="date">' . $row['date'] . '</span></li>';
来源:https://stackoverflow.com/questions/10608595/apostrophe-php-issue