-1

I'm trying to implement a javascript for loop in which a php variable is counting (phpVar++) each loop. However, the variable, which starts at 0, always ends up being 1, even though the loop loops multiple times.

Is this not possible?

<script>
    <?php  $totalMarkers=0; ?>
    for (var i = 0; i < markers.length; i++) {
      <?php $totalMarkers=$totalMarkers+1; ?>
    }
    <?php echo $totalMarkers ?>   //this always prints "1"
</script>

I'm trying to do this so that I can print $totalMarkers in different places in the Body of the HTML.

swl1020
  • 816
  • 14
  • 34

6 Answers6

7

Javascript is executed on the client computer, PHP is executed on the server. So, your loop does not run until the page is completely loaded in the user's browser -- at that point, no PHP will be executed.

When that page is rendered, this is the process:

<script> <-- gets output literally

<?php $totalMarkers=0; ?> <-- has no output

for (var i = 0; i < markers.length; i++) { <-- is output literally

<?php $totalMarkers=$totalMarkers+1; ?> <-- has no output

} < -- is output literally

<?php echo $totalMarkers ?> <-- outputs 1

</script> <-- is output literally

If you were to view source, you would see this:

<script>
    for (var i = 0; i < markers.length; i++) {
    }
    1   //this always prints "1"
</script>
Chris Baker
  • 49,926
  • 12
  • 96
  • 115
5

PHP runs on the server. JavaScript runs in the browser.

Your server runs the PHP, and will then send this to your browser:

<script>
        for (var i = 0; i < markers.length; i++) {
      }
    1</script>

The only way JavaScript can affect PHP is with AJAX or similar.

Chris Baker
  • 49,926
  • 12
  • 96
  • 115
Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
3

PHP runs server-side, JavaScript runs client-side (ignore Node.JS et.al.)

The HTML your browser receives will look like:

<script>
    for (var i = 0; i < markers.length; i++) {
    }
    1   //this always prints "1"
</script>

So, I'm not sure what you want to accomplish here, but you have to keep in mind that PHP and JavaScript can not interact directly.

Halcyon
  • 57,230
  • 10
  • 89
  • 128
2

You cannot mix PHP and JavaScript like that. It doesn't work that way.

The PHP is ran before the JavaScript is. So, the lines:

  • <?php $totalMarkers=0; ?>
  • <?php $totalMarkers=$totalMarkers+1; ?>
  • <?php echo $totalMarkers ?>

are ran before your browser sees it.

Your browser just sees:

for (var i = 0; i < markers.length; i++) {
}
1
gen_Eric
  • 223,194
  • 41
  • 299
  • 337
1

No, it is not possible - not without using a bunch of AJAX which seems totally inappropriate for you. PHP is executed way before any JavaScript can be executed. PHP is Server-sided, JavaScript is client-sided. Thus, JavaScript can not control PHP.

Simon Forsberg
  • 13,086
  • 10
  • 64
  • 108
1

You can't do that. PHP is a Hypertext Pre Processor. It is executed before any Javascript code. The code you actually have takes $totalMarkers and initializes it to 0. Then it adds one and then it echoes... It will always be "1" !

You need to count using a Javascript variable then send it to the server if you want to manipulate it with PHP.

William Fortin
  • 777
  • 1
  • 4
  • 17