A secure way for a PHP variable to be used in JavaScript
<p>My code is as follows (simplified):</p>

<pre><code>$items_per_view = 5;
$items_per_scroll = 1;

$script = "&lt;script type='text/javascript'&gt;
                jQuery( document ).ready( function($) {         
                    $(document).ready(function() {                            
                       $('#$uniqid').slick({                      
                          slide: 'li',
                          slidesToShow: $items_per_view,
                          slidesToScroll: $items_per_scroll,                     
                        });                 
                    });
                }
             );
            &lt;/script&gt;";     

    $output .= $script;
</code></pre>

<p>Now I've been reading a lot about using <em>Escaping Attributes</em> as best practice. However when I tried to use <code>json_encode( $items_per_view )</code> or <code>esc_js( $items_per_view )</code>, it stops working and in Chrome I see a message saying "json_encode is not defined". I also tried using <code>wp_json_encode</code> but that didn't worked either.</p>

<p>Any help is much appreciated. :)</p>