Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am very new to PHP and JavaScript. I am currently using echo in PHP to run some JavaScript on the page. I need to make a new javascript array and a new variable that are equal to an existing PHP array and variable, so I did this:

var messages = <?php print_r($messages)?>
var list = <?php echo $message['user_name'].': '.$message['text'].' ('.date('d/m/Y H:i:s', $message['date']).')'.'<hr />'; ?>

However, there is a problem caused by my using echo to echo script containing echo. How would I solve this. I would like to echo it because it is only about 4 lines long, so is there an alternative.

Thank you in advance.

Edit: This is the whole JavaScript. It is for a messaging system. $messages is declared from another PHP function, and the basic aim of this code is to 'refresh' the echo every few seconds so that the user can see new messages without having to refresh their page:

    echo '<script type="text/javascript">;';
    echo 'var messages = <?php print_r($messages)?';
    echo 'var list = <?php echo $message['user_name'].': '.$message['text'].' ('.date('d/m/Y H:i:s', $message['date']).')'.'<hr />'; ?>';
    echo 'setInterval(function(){console.log("hello")},3000);';
    echo '</script>';
share|improve this question
    
again? this question is asked many times. Search Google / Stack Overflow! –  Raptor Feb 19 at 7:32
    
    
I did but I couldn't find a similar question –  user3315726 Feb 19 at 7:33
    
From what I can see, that doesn't really solve the double echo problem, it just explains the difference between code executed on the server and code executed on the client –  user3315726 Feb 19 at 7:35
    
yes, it's what you don't understand. –  Raptor Feb 19 at 7:58

2 Answers 2

Not getting what you want,but if you want to use php array in javascript than make it javascript array

<script>
    <?php $test_arr = array('apple','banana','mango');?>
    var js_array = <?php echo json_encode($test_arr);?>;
</script>

output

<script>
    var js_array = ["apple","banana","mango"];
</script>
share|improve this answer

Your Javascript will execute on the client, not on the server. This means that foo is not evaluated on the server side and therefore its value can't be written to a file on the server.

The best way to think about this process is as if you're generating a text file dynamically. The text you're generating only becomes executable code once the browser interprets it. Only what you place between <?php tags is evaluated on the server.

By the way, making a habit of embedding random pieces of PHP logic in HTML or Javascript can lead to seriously convoluted code. I speak from painful experience.

share|improve this answer
    
This answer is a direct duplicate of this. –  halfer Apr 26 at 16:15
    
Hi there. As per my note on one of your other post, we prefer copied work to be attributed, so that the OP here can thank the author - often via an upvote. However it is even better, in my opinion, to just add a link under the question. I believe you need 50 rep to do that, but if you were so inclined, you can get that by answering just a few questions. –  halfer Apr 26 at 16:29

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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