Skip to content
Advertisement

How to echo in PHP [closed]

I am trying to display output from following code. The script ish creating cookie policy pop-up.

<?php
    if (some statement) {
    echo '<script> long script here </script>';
    } else {
    echo '<script> some other script </script>';
} ?>

I am definitely making some syntax mistake in echo '<script></script>'; part, because if statement itself is working (I am able to echo simple <h1><h1> text. And the script itself is also working.

I have tried with echo '<script language="javascript" type="text/javascript">'; and also

echo '<script language="javascript" type="text/javascript">';
echo '[script here]';
echo '</script>';

I dont’t get any errors. Simply nothing shows up.

Advertisement

Answer

Break out of PHP, or dont put clientside scripting inside serverside echos, instead abstract that code to clientside.

<?php if (some statement) {?>
<script> long script here </script>
<?php } else { ?>
<script> some other script </script>
<?php } ?>
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement