1

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.

8
  • are you sure $my_array['index_of_keyword3'] is set? Commented Feb 15, 2013 at 17:49
  • Your code should work. Are you sure this is a php file, and it's getting parsed by the server? Commented Feb 15, 2013 at 17:50
  • @John yes. I am using WAMPServer, I don't think there is a problem with WAMP. Commented Feb 15, 2013 at 17:55
  • Did you try a simple <?php echo "Hello World!"; ?> - Did you try the extenstion .php5 instead of .php? Is the address http:// or file:///?? Commented Feb 15, 2013 at 17:58
  • edit your question to precise you're calling it right ;) Commented Feb 15, 2013 at 17:58

4 Answers 4

2

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

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

Sign up to request clarification or add additional context in comments.

6 Comments

Might be something to do with your server setup then.
So are you sure php SHOULD recognize <?php?> tags inside double quotes?
As long as the tags aren't wrapped around an echo/print statement then -yes.
I mean make sure there isn't something like: <?php echo ' <meta name="keywords" content="keyword1, keyword2, <?php echo $my_array['index_of_keyword3']; ?> ">';
|
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

5 Comments

I directly open "localhost/thisfile.php" in my browser. But I guess this is what PHP should do. It recognizes "<?php.. blabla" as a string. IF I would have done content=<?php echo "\"keyword1,etc\"";?> than it would interpret correctly, I guess. But what is the right way to do it inside double quotes?
won't work. You need a server like apache to ask php to run your file. CConsider using WAMP(windows), XAMP(Mac), LAMP(Linux)
I am using WAMP's localhost :)
So you need to type in your browser url localhost/ followed by the relative path to your wamp www/ dir. For instance if your script is in the www/ and named test type localhost/test.php
for debug purpose, run this file in your command line with php somepage.php and tell us what happen
1

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.

8 Comments

@CooPer If the problem was the file extension it would be the answer. Also I was trying to show a better way to do what he wants to do.
+1 as this answer does not deserve negative rating. It "could" be a possible answer even if it's not "the" answer
@Onimusha i think it cant be answer, question about why php code not execute , not fix this code !
Thanks for the answer but it has the exact same "problem" with my code: php tags inside double quotes. And I think that is wrong, and I'm seeking for a better way for it. Yet everybody is saying that it should work. Confused :/
@marvin it is not wrong , php work every where you open <?php tag
|
1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.