-4

i have following java script in my php program for Given integers n, l and r, find the number of ways to represent n as a sum of two integers A and B such that l ≤ A ≤ B ≤ r. and my code is...

<script>
function count(n, l, r) {
var result = 0;
for(var a = l; a <= r; a++){
    for(var b = a; a <= r; b++){
        if(a + b == n){
            result++;
        }
    }
}
return result;
}
</script>
<?php
echo "<script>count(6,2,4);</script>";
?>

and i want to run this code on page load and call a function count() on load and give output. i don't know what to do.

Divyesh Jesadiya
  • 1,105
  • 4
  • 30
  • 68

3 Answers3

1

You need to echo content that you want written to the resulting page.

Right now you're trying to run Javascript in your PHP script which makes zero sense.

Jonathon Reinhart
  • 132,704
  • 33
  • 254
  • 328
0

take a look how to get onload event https://stackoverflow.com/a/807895/1074179 or you can add jquery lib to your code and then do like https://api.jquery.com/ready/

Community
  • 1
  • 1
Alexandr
  • 5,460
  • 4
  • 40
  • 70
0

you can't run directly a HTML tag tag inside a php script. You must have to print or echo that html. for example if you want print <h1>Hello World</h1> then you have echo the html as <?php echo "<h1>Hello World</h1>"; ?> like this you have write <?php echo "<script>alert('hi');</script>";?>

  • 1
    Change your code to: count(6,2,4);"; ?> – Biswanath Sahoo Jun 22 '16 at 10:32
  • i change my code but now it's continues running not showing any output. – Divyesh Jesadiya Jun 22 '16 at 10:43