Jump to content

php to javascript


Recommended Posts

hello I have a javascript code snippet
I would like to add a php variable in the place where now is: abcd1234

original code: 

     window.dataLayer = window.dataLayer || [];
     function func_name(){dataLayer.push(arguments);}
     func_name('js', new Date());
     func_name('config', 'abcd1234');

I tried this :

     <?php $var = 'abcd1234' ?>
     var my_var = <?php echo $var; ?>


but I don't know how to put the code in this line:


func_name('config', what here? );





Link to comment
Share on other sites

Yes! the quotes are part of the JavaScript code that requires a string value within those quotes, which is provided by echoing php variable. Without JavaScript quotes you would have

func_name('config', abcd1234 )

which would be treated as a JavaScript argument variable and because it has not been defined or has no value associated with it would throw a undefined error.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...