1

I would like to know if this code is correct or not...

<?php foreach($ids as $id) { ?> 
  <script>
    loadMetadata(<?$id?>);
  </script> 
<?php }?>

or

<script>
  <?php foreach($ids as $id) ; ?> 
    loadMetadata(<?$id?>);
  <?php } ?>
</script>

Thanks

2
  • 1
    Same effect. First one generates more HTML. Both bad ways of doing things. Use AJAX. Commented Jan 9, 2015 at 12:15
  • <?$id?> doesnt makes any sense since variable itself return nothing you should use either echo or <?= $id; ?> Commented Jan 9, 2015 at 12:17

1 Answer 1

3

You missed the echo there. Your code will end up like this:

<script>
loadMetadata();
</script>

Add an echo before the $id. Also, not entirely sure if blocks work like that. I'd do it like this:

<?php foreach($ids as $id):?>
<script>
loadMetadata(<?php echo $id;?>)
</script>
<?php endforeach;?>

You can read more about this here: Alternative syntax for control structures ¶

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.