admin管理员组

文章数量:1418103

I want to echo out a simple Facebook like button script in PHP, but it wont let me. Here's what the script would look like:

<?php
  echo "    <td>'<script src=".js#xfbml=1"></script><fb:like href="http://#" layout="button_count" show_faces="false" width="450" font=""></fb:like>'</td>\n" ;        
  echo "    <td>".$row['item_content']."</td>\n";    
?>

I want to echo out a simple Facebook like button script in PHP, but it wont let me. Here's what the script would look like:

<?php
  echo "    <td>'<script src="http://connect.facebook/en_US/all.js#xfbml=1"></script><fb:like href="http://#" layout="button_count" show_faces="false" width="450" font=""></fb:like>'</td>\n" ;        
  echo "    <td>".$row['item_content']."</td>\n";    
?>
Share Improve this question edited Jun 29, 2011 at 20:56 OMG Ponies 333k85 gold badges535 silver badges508 bronze badges asked Mar 12, 2011 at 19:56 user653480user653480 651 gold badge3 silver badges7 bronze badges 1
  • 3 You really have to be more specific than "it won't let me". That's not a very programmer-friendly term. – Jon Commented Mar 12, 2011 at 19:58
Add a ment  | 

3 Answers 3

Reset to default 3

Is this the plete code? Better is not to echo it at all:

<td>
    <script src="http://connect.facebook/en_US/all.js#xfbml=1"></script>
    <fb:like href="http://#" layout="button_count" show_faces="false" width="450" font=""></fb:like>
</td>
<td><?php echo $row['item_content']; ?></td>

Embed PHP in HTML, not vice versa.

You have to properly escape your quotation marks.

Everytime you are using a double-quote (") in a double-quoted string, you must prepend it with a backslash (\) as such:

echo "    <td>'<script src=\"http://connect.facebook/en_US/all.js#xfbml=1\"></script><fb:like href=\"http://#\" layout=\"button_count\" show_faces=\"false\" width=\"450\" font=\"\"></fb:like>'</td>\n";
echo "    <td>".$row['item_content']."</td>\n";

Alternatively, you could single-quote (') the whole string, but note that in single-quoted strings, the only escape sequences recognized are \' and \\. In-line variables are also not recognized.

echo '    <td>\'<script src="http://connect.facebook/en_US/all.js#xfbml=1"></script><fb:like href="http://#" layout="button_count" show_faces="false" width="450" font=""></fb:like>\'</td>' ;
echo "\n    <td>".$row['item_content']."</td>\n";

For more information, please read the PHP Documentation page on Strings:

PHP Documentation: Strings

Try this, you must escape some symbols

<?php
       echo '    <td>\'<script src="http://connect.facebook/en_US/all.js#xfbml=1"></script><fb:like href="http://#" layout="button_count" show_faces="false" width="450" font=""></fb:like>\'</td>\n';
echo "    <td>".$row['item_content']."</td>\n";

?>

本文标签: How to use javascript inside a PHP echo functionStack Overflow