I'm trying to pass a PHP variable to Javascript like this..

```
var url = <?php echo $urlArray[0]; ?>;

```

The contents of $urlArray[0] is a string from a json decoded array 

> 
> "baby"
> 
> 
> 

When I run the code I get the error..

> 
> Uncaught ReferenceError: baby is not defined
> 
> 
>
['https://Stackoverflow.com/questions/17077190', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1714301/']
```
var url = "<?php echo $urlArray[0]; ?>";

```

you forgot the quotes.

If you need to export more complicated data structure, you may need [json\_encode](http://php.net/manual/en/function.json-encode.php). This might be helpful if exporting arrays and/or objects.
`json_encode` is your friend - use to wrap anything you're trying to pass to javascript.

<http://php.net/manual/en/function.json-encode.php>

```
var url = <?php echo json_encode($urlArray[0]); ?>;

```