How do guys suggest to share a constants file between PHP and JavaScript, in order not to repeat code? XML file? I am assuming mixing up javascipt inside PHP would not be the right solution!? Thanks
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
http://php.net/manual/en/book.json.php
I would say use json. It is native to the javascript and there is a parser library for the php.
consider the following:
json:
and then read it into php:
This gives you the object $const with properties like $const->{‘var1’} returning “value 1”.
in JavaScript this would be:
and would give you const.constants.var1 == “value 1”.
Easiest implementation in real terms for js is:
When added the html output you instantly have a constants object with the other objects as its children.