📜  如何将 javascript 添加到 php 变量 - PHP 代码示例

📅  最后修改于: 2022-03-11 14:53:49.080000             🧑  作者: Mango

代码示例1
//this is my code 

$value = <<

        
       data;

so the php variable $value will not store the string "hello world" inside it,
so the php variable is just a temporary container for the javascript code and nothing else,
and if we echo the vairable like this:-
  
echo $value;
then hello world will be printed in the browser page,
and it is the same as using echo to excute a javascript code, so it is as:-
  
echo ""
  
  
  
//using console instead of document.write
  
$value = <<
         console.log("hello world");
        
       inConsole;


but the value will not be printed in the console unless we echo the variable, 
like this:-

echo $value
then hello world will be printed in the console
  
  
  
//other examples such as storing data to localstorage
  
$local = <<
         localStorage.setItem("anything", "hello");
        
        localstorage;


but this code will not work until we echo the variable such as:-
echo $local

and if we open the console and write localstorage then the item anything
with its value hello will appear
  
  
  
  
  
  
and if we want to remove the item from localstorage then use this code:-

$local = <<
         localStorage.removeItem("anything");
        
        localstorage;

        echo $local;

note: never forget to use the echo statement or the code will not work
  
  
  
  
and if we want to get data from localstorage, we use this code

$local = <<
         let a = localStorage.getItem("anything");
         document.write(a);
        
        localstorage;

        echo $local;

then the value of the varaible a will be print in the webpage,