Javascript as text
If your question is whether you can create a text that contains a JS code that will be inserted along with the HTML text you are generating with PHP. Yes, you can.
What PHP does is just this, generate a "linguing" text that will be sent to the HTTP server that it sends to the browser or whoever has requested it. It may be until it’s not an HTML page you’re making. Understand PHP code as a great word processor.
Of course it can do much more than that, but in the way it is usually used the end result will always be the text that can contain anything you want, can have HTML, JS, CSS, JSON, plain text, or any other format. Actually it doesn’t even have to be text, this is the type of data generated for the most common output. If the text has something wrong inside it, it’s not a PHP problem.
It may seem confusing because it is the code of a language within the code of another language. These codes have no relation to each other. They do not "talk", because the JS code is not executed by PHP, for it is just any text, only you know that is JS. When it is programmed think of PHP code independently of JS. The problem of one is not the problem of the other.
The echo
is just for this, everything you "print" in PHP will be sent to the HTTP server. At least this is the most common way to use PHP (it is possible to use it in other ways, but most programmers do not know how to do this and do not even know it is possible). Obviously where to put this echo
in the code is important to achieve the expected result.
Have you ever tried to see in the browser the result of a page generated with PHP? Do experiments trying to send in different ways and whenever you have specific questions, post here giving as much detail as possible of what you are doing and what the problem is.
Javascript running
If you want to know if you can insert this JS code into the PHP code itself and execute on it, then the answer is no, you cannot do this, they are separate languages. There may even be some way to call a mechanism that runs the JS externally, but it’s something no one does and it’s really weird. I see no reason to do this.
Specific problem
Note that the ideal is that you do not have JS code inserted within an HTML page. It would be better to have all JS code in a separate file, and preferably this file would be static, that is, it would be fixed and not generated on time.
Dynamically generating JS code via PHP usually makes little sense most of the time. Insert it into the JS is not usually necessary either, although it can be used smoothly.
After editing the question I see that the code is doing what you want, or not? Put it to run? It appeared in the browser? That’s it. Of course this code assumes that there are other HTML or even JS parts on the same page or additional file.
But I have my doubts if you need to generate all this JS code in PHP. I think you can leave the JS separate and configure it to manipulate the color by parameter and only the color to be inserted in the call of this code. You need to see a bigger context. But I reinforce what I said before, in general there is no reason to generate JS code by PHP.
If the JS is not generated ok and does not work, the problem is another one described in the question, then you should open another question with its specific problem. Post the rest of the code of the page in this new question. I believe it is not necessary anything PHP, even because this was not put effectively.
Yeah, you’re making a mistake with that?
– Jéf Bueno
Javascript does not work I must have something wrong then
– Amadeu Antunes
I don’t think this is a good idea. But you can do a nice little thing using ob_start
– Wallace Maxters
I ask you to consider my answer as an example to improve these practices. I believe @bigown’s response has already been enlightening to you.
– Wallace Maxters