Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a php variable called $usershours that contains an array, and I want to echo it inside Javascript code. How do I do that?

I tried This, but it didn't work:

$usershours = "
data.addRow([ new Date(2012,4,1),50119]).+'<br>+'.
data.addRow([ new Date(2012,4,2),242575])";

<script type="text/javascript">
document.write($arrayholder);
</script>

I want the output to be like this:

data.addRow([ new Date(2012,4,1),50119]) 
data.addRow([ new Date(2012,4,2),242575])

PS - I need this format because This is the format that Google charts API accept to render charts correctly.

share|improve this question
3  
PHP is executing on the server and sending the browser HTML and JavaScript. Where are you expecting this to take place? – databyss Aug 22 '12 at 2:01
1  
<?php echo $userhours ?> – locrizak Aug 22 '12 at 2:04
2  
This isn't a game. $usershours = "data.addRow([ new Date(2012,4,1),50119]) data.addRow([ new Date(2012,4,2),242575]) "; echo $usersHours; is the output you asked for. You will not see anything on the screen, as the resulting JavaScript just calls a function on the data object. If there is more you are expecting, you need to write a different question. – Dan Grossman Aug 22 '12 at 2:15
2  
It's not tricky at all. Put that between <script> and </script> tags. – Dan Grossman Aug 22 '12 at 2:18
1  
Then wrap <script> tags around it: ?><script>data.addRow([ new Date(2012,4,1),50119]) data.addRow([ new Date(2012,4,2),242575])</script><?php – meagar Aug 22 '12 at 2:34
show 16 more comments

closed as too localized by meagar, jeroen, Jack, rdlowrey, bažmegakapa Aug 23 '12 at 1:27

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

up vote 4 down vote accepted

From what I gather, you want this code run by javascript on the client machine:

data.addRow([ new Date(2012,4,1),50119]) 
data.addRow([ new Date(2012,4,2),242575])

Simply:

<script type="text/javascript">
<?php 
$usershours = "
data.addRow([ new Date(2012,4,1),50119])
data.addRow([ new Date(2012,4,2),242575])";

echo $usershours;
?>
</script>
share|improve this answer
Thanks a lot for trying, but it gaves me empty result. – mongotop Aug 22 '12 at 2:23
Please define "empty result", because those words obviously mean something different to you than to everyone else. This code does exactly what you asked. We're not "trying" at some game; your question has been answered. – Dan Grossman Aug 22 '12 at 2:24
@DanGrossman, you are correct, but what I meant with my question was to display it on the webpage, because I will use the result for google chart API to display some graphs. thanks again man! – mongotop Aug 22 '12 at 2:31
@DanGrossman Empty result mean, white page, but you will find the code in the source code. – mongotop Aug 22 '12 at 2:41
A white page, with the data.addRow statements inside some JavaScript, is exactly what you asked for. You didn't ask for any text or HTML to be written. – Dan Grossman Aug 22 '12 at 2:42
show 1 more comment

Though messing PHP with JS is far from an optimal approach, this will keep your code a bit tidier as well.

Let's pretend your data comes from an array [fetched from a database or a file] such composed:

$data = array(
    array("date" => "2012-04-01", "id" => 50119),
    array("date" => "2012-04-02", "id" => 242575)
);

Through json_encode($data) you get this output:

[
  { date : "2012-04-01", id : 50119},
  { date : "2012-04-02", id : 242575}
]

Now you know how to translate your data from a PHP structure to a Javascript one, it's just a matter of writing:

<script>

  var serverData = <?php echo json_encode($data) ?>;

  for(var i = 0; i < serverData.length; i++)
  {

     data.addRow([ new Date( serverData[i].date ), serverData[i].id ]);

  }

</script>

Granted that data.addRow() is a function declared somewhere in you JS code.

share|improve this answer

Simply use var arrayholder = <?php json_encode($usershours);?>; if arrayholder has to be output as string (and not as code)

share|improve this answer
2  
Semicolons are not required in JavaScript as long as you avoid a few ambiguous syntax gotchas. – Dan Grossman Aug 22 '12 at 2:13
1. and 2. are irrelevant and false respectively. – meagar Aug 22 '12 at 2:14
@Dan Grossman: Yeah as far as I know only expressions need that semicolon ;(...), ;[...] and ;/.../, and also "use strict"; for some reason. – elclanrs Aug 22 '12 at 2:18
Corrected. Thanks for ponting my errors out. – Cranio Aug 22 '12 at 2:59

General advice: do not try to render valid JavaScript from PHP. It will eventually bite you, some alternate solutions: embedding the data in an HTML element and extracting it via JavaScript and Ajax

Having said that

?>
(function () {
   console.log(<?= json_encode($phpVar); ?>);
}());
<?

is how I do it when I'm ok with shooting myself in the foot

share|improve this answer
By bite you I mean render an unexpected error in the middle of your JavaScript, due to some configuration you can't control. – Lucas Green Aug 22 '12 at 2:22
Why would you want to embed the data in an HTML element when you can just echo it out in js? – jeroen Aug 22 '12 at 2:23
That's a pretty arbitrary rule to impose on yourself, also pretty irrelevant to the question. – meagar Aug 22 '12 at 2:24
3  
JavaScript crashing is no more or less damaging to your app than malformed HTML which your JavaScript depends on. If you are writing software which outputs PHP errors, it's because you're writing bad software, and you need to fix it regardless of whether the errors appear in your HTML or in your JavaScript. If you want your software to work you need to write error-free software, not try to hide the errors in HTML because "HTML doesn't crash". That is an utterly ridiculous attitude and an incredibly silly programming practice. – meagar Aug 22 '12 at 2:30
1  
@Mythril The solution is to write good code and make a reasonably modern installation of PHP a requirement for running your software. Writing software to run on broken hosts is incredibly self-defeating. I haven't said anything about your skill, but I will say that worrying about only mixing PHP with your HTML because "you can put escape sequences that prevent the DOM from being munged ( <![CDATA[]]> )" is a horrendous way of designing software. – meagar Aug 22 '12 at 2:51
show 14 more comments

Not the answer you're looking for? Browse other questions tagged or ask your own question.