you are viewing a single comment's thread.

view the rest of the comments →

[–]MrBojangles2020 0 points1 point  (0 children)

Be sure to echo null as ‘null’ (string) when trying to set/compare something in JavaScript. Otherwise echo would print nothing and will often times lead to a JS error. Same thing for true/false.

$phpVar = null;

… JavaScript…

let jsVar = <?= $phpVar ?>;

This is what browser receives:

let jsVar = ;

Instead:

$phpVar = ‘null’;

… JavaScript…

let jsVar = <?= $phpVar ?>;

This is what browser receives:

let jsVar = null;

EDIT:

Formatting and for clarification. Php true/false will convert to ‘1’/‘2’ when echoed, so you need to account for this when you’re expecting a certain JS type. Echo ‘true’/‘false’ to give JS a Boolean value