Javascript check if PHP variable is defined or not

Viewed 4920

I can not find an answer to my problem and I am also not sure if this is possible or not.

Is there any way to check if a PHP variable is defined or not with javascript?

This is an example:

var op = <?PHP echo json_encode($op); ?>;

if $op is not defined I got an error in javascript:

Events:362 Uncaught SyntaxError: Unexpected token )

I understand this is normal because this variable does not exist in PHP. But there is a way to avoid the error if the variable does not exist?

4 Answers
Related