【问题标题】:GET parameter is not null, but i can't use it on a PHP functionGET 参数不为空,但我不能在 PHP 函数上使用它
【发布时间】:2020-08-18 11:50:32
【问题描述】:

主文件结构(index.php)

<?php
include('functions.inc.php');
include('page.php');
?>

在functions.inc.php文件中我定义了一个函数:

function getServiceAddonButtons($product)
        {     
                global $db; 
                $service = getServiceVar($product);
                $svaddon = $service['svdo_addon'];
                if (file_exists('content/addons/'.$svaddon.'/service_buttons/buttons.php') && addon_activated($svaddon) == 'YES') {
                    include('content/addons/'.$svaddon.'/service_buttons/buttons.php'); 
                }
            }

(此文件上的其他函数已经定义并且运行良好)

在 page.php 中我使用了这个函数:

<div class='buttons'>
<?php getServiceAddonButtons($_GET['service_id']); ?>
</div>

当我在 index.php 上时(显示 page.php 因为包含在内) 我使用网址https://example.com/index.php?manage_service=1&amp;service_id=218

问题:似乎每次我在这个页面上,函数getServiceAddonButtons($product)的参数都是空的,但我使用$_GET['service_id']

有人知道如何解决这个问题吗?

【问题讨论】:

  • 您是否尝试回显 GET 变量以查看结果?
  • 是的,在 page.php 中返回正确的值 (218)
  • 您说 'getServiceAddonButtons($product) 为 null' 因为不返回包含 true?或函数中的 $service 变量不起作用?尝试在函数的所有部分中使用 echo 来测试是否需要您想要的变量。你确定存在 $svaddon 文件夹吗?
  • include没有问题,你看:函数中有一个$product参数,即使我用function($_GET['service_id'])定义它也是null
  • 如果你在函数前回显$_GET['service_id']并正确显示(218)不可能为null,问题出在函数中。

标签: php parameters get include parameter-passing


【解决方案1】:

您只需要在变量声明之后进行“包含”。

<?php
$variable = $_GET['service_id'];
include('functions.inc.php');
include('page.php');
?>

此链接包含有关您的问题的所有详细信息:PHP pass variable to include

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-04
    • 2021-04-02
    • 1970-01-01
    • 2021-04-06
    • 1970-01-01
    相关资源
    最近更新 更多