apostrophe php issue

半世苍凉 提交于 2019-12-24 01:15:09

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!