BrainPill Posted March 17, 2019 Share Posted March 17, 2019 hello I have a javascript code snippet I would like to add a php variable in the place where now is: abcd1234 original code: <script> window.dataLayer = window.dataLayer || []; function func_name(){dataLayer.push(arguments);} func_name('js', new Date()); func_name('config', 'abcd1234'); </script> 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 More sharing options...
Ingolme Posted March 17, 2019 Share Posted March 17, 2019 Just echo the value there, but be sure to wrap it in Javascript quotes. Link to comment Share on other sites More sharing options...
BrainPill Posted March 19, 2019 Author Share Posted March 19, 2019 On 3/17/2019 at 5:53 PM, Ingolme said: Just echo the value there, but be sure to wrap it in Javascript quotes. I dont understand. How should it be done? And what are Javascript quotes? And where to place them? Link to comment Share on other sites More sharing options...
BrainPill Posted March 19, 2019 Author Share Posted March 19, 2019 I solved the issue meanwhile by doing this: "<?php echo $var ?>" using the double quote . Is that what you meant with javascript quotes? Link to comment Share on other sites More sharing options...
dsonesuk Posted March 19, 2019 Share Posted March 19, 2019 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now