dsz90288 2014-10-09 22: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 22: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']);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 adb连接不到手机是怎么回事?
  • ¥15 vs2022无法联网
  • ¥15 TCP的客户端和服务器的互联
  • ¥15 VB.NET操作免驱摄像头
  • ¥15 笔记本上移动热点开关状态查询
  • ¥85 类鸟群Boids——仿真鸟群避障的相关问题
  • ¥15 CFEDEM自带算例错误,如何解决?
  • ¥15 有没有会使用flac3d软件的家人
  • ¥20 360摄像头无法解绑使用,请教解绑当前账号绑定问题,
  • ¥15 docker实践项目