Php question if u please

<!DOCTYPE html>
<html>
<body>

<?php
$array=array(1,2,3);
?>

<script>
 let i =0;
let y= "<?php echo $array[i] ?>";
document.write(y);

</script>

</body>
</html>

It works and print the value in array if i write $array[0] but it doesnt work or prints nothing if i say $array[i] which btw i is 0 so why?

The php code is executed on the web server when the page is requested. The javascript is executed in the browser when the page is rendered/displayed. The i javascript variable doesn’t exist at all in the php code, and php treats it as an (undefined) constant. If you have php’s error_reporting set to E_ALL and display_errors set to ON, you would be getting an undefined constant error from the php code, which may only be visible if you look in the ‘view source’ of the page in your browser.

Nah …my code is right what i did is importing the php code feom php to script tag using echo bit i should have write before $array …
json_encode($array)
Which get the array feom the php and pyt it inside y variable so it works well
Thank you for your response anyways

Firstly convert php array ($array) to javascript array. After that use it as your wish.

Sponsor our Newsletter | Privacy Policy | Terms of Service