dougaojue8185 2018-01-01 15:10
浏览 101
已采纳

如何将$ _SESSION变量传递给函数

I am trying to pass my $_SESSION variables into a database query function for WordPress. If I define the $_SESSION variables within the function it works fine. But when I define them on a global level and try to pass them in, they do not pass through. Please view below for examples.

This will pass through to the function below

$_SESSION['pages'] = $_POST['pages'];

But when I add

$pages = $_SESSION['pages'];

$pages will not pass through to the function.

    $_SESSION['pages'] = $_POST['pages']; //passes
    $pages = $_SESSION['pages']; //does not pass

function insertLocalBusinessSchema()
    {
        //include global config  
        include_once($_SERVER['DOCUMENT_ROOT'].'/stage/wp-config.php' );

        global $wpdb;

        // if I try to define this outside of the function it doesn't pass through.
        $pages = implode(',', $_SESSION['pages']);

        $paymentAccepted = implode(',', $_SESSION['paymentAccepted']);

        $table_name = $wpdb->prefix . "schemaLocalBusiness";

        $insert = "UPDATE ".$table_name." SET addressLocality = '".$_SESSION['addressLocality']."', addressRegion = '".$_SESSION['addressRegion']."', postalCode = '".$_SESSION['postalCode']."', streetAddress = '".$_SESSION['streetAddress']."', pages = '".$pages."', paymentAccepted = '".$paymentAccepted."' WHERE id = 1";

        $wpdb->query($insert);

    }

Thank you for the help in advance!

  • 写回答

1条回答 默认 最新

  • duanfu6160 2018-01-01 15:14
    关注

    $_SESSION is a global variable and so you can call it from anywhere you wish, while $pages is not defined globally so you would need to pass it as a parameter to the function like this:

    function insertLocalBusinessSchema($pages)
    {
        echo $pages; // the variable is now in the function's scope
    }
    

    You would then call the function passing the parameter for $pages:

    insertLocalBusinessSchema($pages);
    

    If you wish to use the variable $pages inside the function without passing its value as a parameter, you can do it by using the $GLOBALS PHP super global variable like this:

    // super global variable GLOBALS
    $GLOBALS['pages'] = $_POST['pages'];
    
    function insertLocalBusinessSchema()
    {
        echo $GLOBALS['pages']; // this also works
    }
    

    more here.

    Or you can use the global keyword like this:

    $pages = $_POST['pages'];
    
    function insertLocalBusinessSchema()
    {
        global $pages;  // $pages becomes global
        echo $pages;    // this also works
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵