dsz90288 2014-10-09 14:02
浏览 79
已采纳

PHP 5.3 $ GLOBALS无法访问

I'm having problems using the PHP GLOBALS array in a Joomla website. When the form is submitted, the function form_submit is called where the form information is checked for validity. For some reason, I can access my variables correctly outside the function, but when I try to access them through the GLOBALS array, nothing is found.

<?php
//THIS CODE CREATES THE ADD COURSE FORM
 //CONNECT TO SERVER
require('../database2/includes/connect.php');

//GET LOGGED IN USER INFO
$user = JFactory::getUser();
$user_id = $user->id;
$user_name = $user->name;

$category_query = $conn->query('SELECT * FROM category');
$category_query->setFetchMode(PDO::FETCH_ASSOC);


$name = $_POST['name'];
$description = $_POST['description'];
$category_id = $_POST['dropdown'];
$crn = $_POST['crn'];
$password_init = $_POST['password_init'];
$password_rt = $_POST['password_rt'];
$password = md5($password_init);


function form_submit()
{
    var_dump($GLOBALS['name']); //Dumps null
    global $name //Doesn't work either

    if (empty($name) || empty($description) || empty($crn) || empty($password_init) || empty($password_rt))
    {
        echo "<b style='color:red'>* $name</b><br>";
        echo "<b style='color:red'>* $description</b><br>";
        echo "<b style='color:red'>* $crn</b><br>";
        echo "<b style='color:red'>* $password_init</b><br>";
        echo "<b style='color:red'>* $password_rt</b><br>";
    }
}


if(isset($_POST['Submit']))
{
    var_dump($name); //Dumps correct value
    form_submit();
}

?>

var_dump($name) prints the correct value, but $GLOBALS['name'] inside the form_submit does not. What is wrong with my code?

展开全部

  • 写回答

1条回答 默认 最新

  • douyinbo3361 2014-10-09 14:23
    关注

    Given your mention of Joomla, and the code's mention of a class JFactory which must be defined elsewhere, I suspect that this file is not the direct entry point of the browser, but is included by the framework.

    The reason that matters is that if require/include are used inside a function, then the code in the included file is considered to be inside that function as well.

    So your mentions of $name in this file all refer to the same local variable, in the scope of whatever function this file is included from. But they don't refer to the global variable $name. Function declarations, incidentally, still create global functions, because PHP has no such thing as nested/local functions.

    The simplest solution is to get out of the habit of using global variables, and then you won't have to worry about this problem. In this case, you're defining a function, so you can pass that function as much information as it needs; then, if you need to call it based on a different combination, you can, rather than having to redefine a global variable to suit each case.

    function form_submit($name, $description, $crn, $password_init, $password_rt)
    {
        if (empty($name) || empty($description) || empty($crn) || empty($password_init) || empty($password_rt))
        {
            echo "<b style='color:red'>* $name</b><br>";
            echo "<b style='color:red'>* $description</b><br>";
            echo "<b style='color:red'>* $crn</b><br>";
            echo "<b style='color:red'>* $password_init</b><br>";
            echo "<b style='color:red'>* $password_rt</b><br>";
        }
    }
    
    if(isset($_POST['Submit']))
    {
        form_submit($name, $description, $crn, $password_init, $password_rt);
    }
    

    Or even:

    if(isset($_POST['Submit']))
    {
        form_submit($_POST['name'], $_POST['description'], $_POST['crn'], $_POST['password_init'], $_POST['password_rt']);
    }
    

    展开全部

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部