How to Set PHP Variable in JavaScript With Example

To Set PHP Variable in JavaScript, we define a JS variable and assign PHP value using PHP tag with single or double quotes.

How to Set PHP Variable in JavaScript With Example

Most of the time you have to set PHP variable value in JavaScript. If there is only simple variable like string or any integer then you can simply echo on the JS variable with PHP tag.

For example if you have a string value on the PHP variable and want to assign it and console it on JS.

<?php

    $nrmlString = 'Lorem ipsum dolor sit amet';
?>
<script type="text/javascript">
    var normalText = '<?php echo $nrmlString; ?>';
    console.log(normalText);
</script>

Output:

Lorem ipsum dolor sit amet

If you have any array value in PHP and you want to get that value on the same JavaScript variable then,

<?php
    $stArray = array('Lorem', 'ipsum', array('dolor', 'amet'));
?>
<script type="text/javascript">
    var phpArray = <?php echo json_encode($stArray); ?>;
    console.log(phpArray);
</script>

Output on Console:

(3) [“Lorem”, “ipsum”, Array(2)]

0: “Lorem”

1: “ipsum”

2: (2) [“dolor”, “amet”]

Complete Example to Set PHP Variable in JavaScript

<?php

    $nrmlString = 'Lorem ipsum dolor sit amet';
    $stArray = array('Lorem', 'ipsum', array('dolor', 'amet'));
?>
<script type="text/javascript">
    var normalText = '<?php echo $nrmlString; ?>';
    console.log(normalText);
    var phpArray = <?php echo json_encode($stArray); ?>;
    console.log(phpArray);
</script>

Output:

How to Set PHP Variable in JavaScript With Example
Example of access PHP variables in JavaScript

Here is the complete example, How to define PHP variable in JavaScript.

Pass PHP Variable as Parameter in JavaScript Function

<?php
    $nrmlString = 5;
?>
<script type="text/javascript">
    var normalText = '<?php echo $nrmlString; ?>';
    
    function printaddition(userPHPNumber){
        alert(parseInt(userPHPNumber)+5);
    }
    printaddition(normalText);
</script>

Output:

Alert 10

In this example Pass PHP Variable as Parameter in JavaScript Function,

  • First, we define a PHP integer and then we create a JS function and
  • Take the PHP variable on our JS variable.
  • Then we call that function with JS variable as a parameter, Which alerts the addition of numbers.

I think this is the complete guide about access PHP variables in JavaScript and also set PHP variable value to JS variable.

We also use parseInt() to convert string to integer in JS. You also read know more from here https://www.w3schools.com/jsref/jsref_parseint.asp

Also Check and Run Live on https://phpcoder.tech/php-online-editor/

Also Check:

Happy Coding..!

Was this article helpful?
YesNo
My name is Bikash Kr. Panda. I own and operate PHPCODER.TECH, my native place in Odisha. I am a Web Programmer by profession and working on more than 50 projects to date. My passion is working on the web-based project using PHP and relate to all CMS and frameworks which is based on PHP.
Posts created 163

2 thoughts on “How to Set PHP Variable in JavaScript With Example

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts

Begin typing your search term above and press enter to search. Press ESC to cancel.

Back To Top
mariobet - supertotobet - escort -

takip7.com

We are now accepting Guest Posting and creation of Backlinks
This is default text for notification bar