简体   繁体   中英

PHP counter in Javascript for loop

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.

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; ?> <?php $totalMarkers=0; ?> <-- has no output

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

<?php $totalMarkers=$totalMarkers+1; ?> <?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>

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.

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.

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM