简体   繁体   English

PHP 5.3 $ GLOBALS无法访问

[英]PHP 5.3 $GLOBALS Not Accessible

I'm having problems using the PHP GLOBALS array in a Joomla website. 我在Joomla网站中使用PHP GLOBALS数组时遇到问题。 When the form is submitted, the function form_submit is called where the form information is checked for validity. 提交表单后,将调用form_submit函数,其中将检查表单信息的有效性。 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. 出于某种原因,我可以在函数外部正确访问变量,但是当我尝试通过GLOBALS数组访问变量时,什么也没找到。

<?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. var_dump($ name)打印正确的值,但form_submit内部的$ GLOBALS ['name']不打印。 What is wrong with my code? 我的代码有什么问题?

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. 鉴于您提到了Joomla,并且代码中提到了必须在其他地方定义的JFactory类,我怀疑该文件不是浏览器的直接入口点,而是包含在框架中。

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. 重要的原因是,如果在一个函数内部使用 require / include ,那么包含文件中的代码也被认为是在该函数内部。

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. 因此,在此文件包含的任何函数的范围内,您在此文件中提到的$name都引用相同的局部变量。 But they don't refer to the global variable $name . 但是它们没有引用全局变量$name Function declarations, incidentally, still create global functions, because PHP has no such thing as nested/local functions. 顺便说一下,函数声明仍然会创建全局函数,因为PHP没有嵌套/局部函数之类的东西。

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']);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM