Skip to main content

Can we store php variable in javascript?

Hey guys,


Today, We will see how to store php variable in javascript. Is this possible?

The answer is yes. 

 Scenario 1: If value to be stored is an integer

<?php

 $value=1; 

 echo "<script> a=" . $value . ";  console.log(a); </script>"; 

?>

Explanation for Integer value:

1. We have to store an integer value in php variable "$value".

2. We have to keep javascript code inside a string.

3. Now assign "$value" to javascript variable "a" using this syntax " . $value . "

4. Output the javascript variable "a" in console .

Scenario 2: If value to be stored is a string

<?php

 $value="Hello"; 

 echo "<script> a=' " . $value . " ' ; console.log(a); </script>"; 

?> 

Explanation for String value:

1. It is similar to the first one, But only difference is, we will be keeping apostrophe ( ' ) before and after php variable "$value" using this syntax ' " . $value . " '

Scenario 3: If value to be stored is an array

<?php

 $value=array(1,2); 

 $js_array_struct=implode("," , $value);

 echo "<script> a=[" . $js_array_struct . "];  console.log(a[0]); </script>"; 

?>

Explanation for Array value:

1.Store array of values in php variable "$value".

2.To form the javascript array structure, convert that array into string with separated by commas using implode function, store the value in "$js_array_struct".

3.Now assign "$js_array_struct" to javascript variable "a" using this syntax  [" . $js_array_struct . "]

4.Output the first value of javascript array "a" in console.

Hope this finds helpful, Please do comment, if you have any doubts or facing problems in javascript or html, css, php, mysql.

Thanks!


Comments

Post a Comment

If you have any doubts, Please do comment

Popular posts from this blog

How to get data from api javascript?

Hey guys, Fetching data from api involves 2 steps: 1.Add jquery CDN script to support jquery function <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script> 2.Use $.getJSON() function to fetch data. <script>  $.getJSON("API URL",function(response) {   console.log(response);  } );   </script> Explanation: In the above function, first parameter is the api url which you want to fetch data and second parameter is function to get the response of that api. The parameter passed to that function has the output stored in that variable on success. Then you can play around with that data. In this way, we fetch the api data using javascript. Hope this finds helpful, Please do comment, if you have any doubts or facing problems in javascript or html, css, php, mysql, will do a post on it. Thanks!

How to display all left table rows & only matched rows in right table using mysql left join?

Hey, guys Today, We will see how to display all left table rows &  only  matched rows in the right table using the MySQL  LEFT JOIN  keyword. SELECT  TABLE1.NAME ,  TABLE2.ID  FROM  TABLE1  LEFT JOIN  TABLE2  ON TABLE1.ID = TABLE2.ID Explanation: 1.Now we have two tables TABLE1 and TABLE2 . TABLE1 :  TABLE2 : 2.Use of LEFT JOIN Keyword: It will display all left table rows and  ONLY  matching rows in the right table. If there are no matching rows for a left table in the right table, it will display as  NULL . OUTPUT: 4.As we don't have any matching IDs ( 1,4 ) in the right table  TABLE2  for left table  TABLE1 , we got the output as null in that rows, but we got all rows in left table  TABLE1. Hope this finds helpful, Please do comment, if you have any doubts or facing problems in javascript or Html, CSS, PHP, MySQL. Thanks!