0

I'd like to mix php with javascript Here is my code:

<?php
function sayHi(){


 echo 'Good Morning';

}
echo 'hello';
for($i=0;$i<10;$i++){
?>



<script>
    document.write('bingo');
    <?php
  sayHi();
 }
 ?>

</script>

It stopped working when it finished echo 'hello'; How can I make it work fine? could you please help me

2 Answers2

3

Your sayHi() function outputs something invalid inside a <script> tag, so it won't work...

Damien Legros
  • 519
  • 3
  • 7
2

What are you trying to do?

The code you have prints as..

<script>
    document.write('bingo');
    Good Morning
</script>

The reason it broke is because Javascript is very touchy and Good Morning isn't valid javascript.

If you want to print "good morning" you have to do this.

<script>
    document.write('<?php sayHi(); ?>');
</script>

Which prints as

<script>
    document.write('Good Morning');
</script>
I wrestled a bear once.
  • 22,983
  • 19
  • 69
  • 116