You could use the following technique :
<?php
if($_SESSION['shouldbezero'] === 0) {
echo '<script src="shouldbezero.js"></script>';
}
?>
The echo
statement would run only if the value of $_SESSION['shouldbezero']
is equal to 0
, and thus the <script>
tag will only be added to your HTML code in that specific situation.
If you use this technique, you should also create a file named shouldbezero.js
, which should contain all the JavaScript code you only want to run if the value of $_SESSION['shouldbezero']
is equal to 0
.
Note :
If you only want to load CSS based on a certain value in your PHP code, could do the same thing with that CSS code.
For example, you could add echo <link rel="stylesheet" type="text/css" href="shouldbezero.css">';
right beneath the echo '<script src="shouldbezero.js"></script>';
, as demonstrated in the code example below :
<?php
if($_SESSION['shouldbezero'] === 0) {
echo '<script src="shouldbezero.js"></script>';
echo '<link rel="stylesheet" type="text/css" href="shouldbezero.css">';
}
?>
If you do this, you should also create a file named shouldbezero.css
, which should contain all the CSS code you only want to process if the value of $_SESSION['shouldbezero']
is equal to 0
.