Reading and writing global variables across scripts in PHP

前端 未结 9 1856
悲&欢浪女
悲&欢浪女 2020-12-11 21:56

Does PHP have global variables that can be modified by one running script and read by another?

相关标签:
9条回答
  • 2020-12-11 22:22

    I made a tiny library (~2 KB; <100 lines) that allows you to do just this: varDx

    It has functions to write, read, modify, check and delete data. It implements serialization, and therefore supports all data types.

    Here's how you can use it:

    <?php
    require 'varDx.php';
    $dx = new \varDx\cDX; //create an object
    $dx->def('file.dat'); //define data file
    
    $val1 = "this is a string";
    $dx->write('data1', $val1); //writes key to file
    echo $dx->read('data1'); //returns key value from file
    
    0 讨论(0)
  • 2020-12-11 22:23

    You can use $_SESSION, i.e.:

    script1.php

    <?php
    session_start();
    $_SESSION['myVar'] = "something";
    ?>
    

    script2.php

    <?php
    session_start();
    echo $_SESSION['myVar'];
    //something
    ?>
    
    0 讨论(0)
  • 2020-12-11 22:26

    Another common substitution for global variables in PHP is the shared use of a database like MySQL (albeit not a perfect one)

    0 讨论(0)
提交回复
热议问题