1

I know it seems like a daft question but I am getting synatx error var imagename = ;. in the code below.

 var imagename = <?php if (isset($_SESSION['imagename'])) echo json_encode($_SESSION['imagename']); ?>;

var imagename is a javascript variable but why am I getting this error? is it because ; is in the wrong place or am I messing a ; or have I added to many ;. I just can't seem to figure it out.

Thanks

1
  • BTW, you need quotes wrapped to the php block.
    – Taha Paksu
    Commented Apr 21, 2012 at 23:04

3 Answers 3

2

Perhaps you are looking for something like this:

var imagename = <?php echo json_encode(isset($_SESSION['imagename']) ? $_SESSION['imagename'] : null); ?>

See http://php.net/ternary#language.operators.comparison.ternary for more info on how the ternary operator works.

0
2

You have no else clause, so if that session variable isn't set, you end up generating your empty assignment.

var imagename = <?php if (isset($_SESSION['imagename'])) { echo json_encode($_SESSION['imagename']); } else { echo "''"; } ?>;
1
  • Good catch. I missed that he was json encoding the session var. Commented Apr 21, 2012 at 23:03
0

The error is probably occuring when $_SESSION['imagename'] does not exist. You should add an else clause for when this happens.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.