I am getting a number that is either 1 or 2 from a php site. If else statements below always return 7. I checked the var z function it works well and returns the numbers. I checked if returned values are actually seen as numbers and thats true. Tried both == and === in the statements. Tried putting '' for values after =. Nothing worked. What could be the issue?
var z = '<?php echo $x; ?>';
if (z == 1){
q = 5;
} else if (z == 2){
q = 6;
} else {
q = 7;
}
EDIT, MORE CODE for variable £x:
<?php
$x = (mt_rand(1,2)); ?>
<script>
var z = '<?php echo $x; ?>';
</script>
My Answer:
';
if (z == 1) {
console.log('Number is 1');
} else if (z == 2) {
console.log('Number is 2');
} else {
console.log('Number is neither 1 nor 2');
}
Rate this post
3 of 5 based on 2623 votesComments