How to add php tags inside attribute value?

十年热恋 提交于 2019-12-14 02:33:44

问题


I'm trying to write meta keywords dynamically with php, so I have such a code , running in WAMP Server localhost/myfile.php :

<meta name="keywords" content="keyword1, keyword2,
<?php
echo $my_array['index_of_keyword3']; 
?>
">

well of course it results in this:

 <meta name="keywords" content="keyword1, keyword2,<?php echo $my_array['index_of_keyword3']; ?> ">

So what should I do to get

 <meta name="keywords" content="keyword1, keyword2,keyword3 ">

?

Thanks !

By the way, I know I could do this with something like

<?php echo "<meta name=\"keywords\" content=\"keyword1, keyword2,$myVariable\">"; ?> 

But I would like to simplify my code, by using php only for the variables.

Edit: omg, I solved the problem by seeing that I'm an idiot :)

I have wrote <?php$row=blabla..?> changing it to <?php $row=blabla..?> solved the issue. Thanks for all the answers. I will upvote them all.


回答1:


I think your problem is that you don't parse the file. How do you call this file because PHP doesn't seem to interpret it?

EDIT: In your wamp config try to disable short open tag




回答2:


I assume your page extention is .html and NOT .php

Change somepage.html to somepage.php and it should start working.




回答3:


Try this:

<meta name="keywords" content="<?php echo implode(",", $my_array);?>">

This way you can print all array content separated with commas. Also make sure your file extension is .php to have your code interpreted.




回答4:


Either your file doesn't have a .php extension or you're trying to open it directly from filesystem. For PHP to work you need a server, for example XAMPP for Windows of LAMP package for Linux.

When you have a server running, there will be a directory called htdocs in the server's directory. Put your files in there and type http://localhost/YourFilenameHere in your browser's address bar to access your files.



来源:https://stackoverflow.com/questions/14900370/how-to-add-php-tags-inside-attribute-value

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