变量“$name”的使用在一个函数中

发布于 2024-11-15 16:53:26 字数 1546 浏览 2 评论 0原文

我喜欢认为我对 php 非常了解,但这让我感到困惑。

保持基本我有:

function req_new($pname, $use=null, $depID=null, $manID=null, $manName=null, $suppID=null, $suppName=null, $cat=null, $brand=null, $name, $email, $custom_code, $user=null, $method=null)
{
    //validation
    if($pname == ''){return false;}

    if($manID==null AND $manName==null){return false;}

    foreach(func_get_args() as $arg)
    {
        $arg = fquery_sanitize($arg);
    }

    //submit new request
    $sql = "insert into sds_product_requests ".
           "(prodName, produse, depID, reqDate, manID, manName, suppID, suppName, prodCat, prodBrand, Name, Email, InternalC, `user`, method) ".
           "VALUES ".
           "('$pname','$use','$depID', NOW(),'$manID', '$manName', '$suppID', '$suppName', '$cat', '$brand', '$name', '$email', '$custom_code', '$user', $method)";
    $result = fquery_db($sql);
    if($result>1)
    {return true;}
    else
    {return false;}
}

如果代码使用变量名 $name,它不起作用。使用另一个变量名称,例如 $pname,它可以工作。如果我使用变量名 $name,它会返回 false。

关于为什么会发生这种情况有什么想法吗?

调用函数

    <?php

     $name = getPOST('name');
     $depID = getPOST('depID');
     $cat = getPOST('cat');
     $supp = getPOST('supp');
     $suppID = getPOST('suppID');
     $man = getPOST('man');
     $manID = getPOST('manID');
    $confirm = req_new('THIS IS A NAME', null, $depID, $manID, $man, $suppID, $supp, $cat, null, null, null, null, fauth_GetUserID(), 1);
?>

I like to think I'm quite knowledgeable on php, but this has baffled me.

Keeping it basic I have:

function req_new($pname, $use=null, $depID=null, $manID=null, $manName=null, $suppID=null, $suppName=null, $cat=null, $brand=null, $name, $email, $custom_code, $user=null, $method=null)
{
    //validation
    if($pname == ''){return false;}

    if($manID==null AND $manName==null){return false;}

    foreach(func_get_args() as $arg)
    {
        $arg = fquery_sanitize($arg);
    }

    //submit new request
    $sql = "insert into sds_product_requests ".
           "(prodName, produse, depID, reqDate, manID, manName, suppID, suppName, prodCat, prodBrand, Name, Email, InternalC, `user`, method) ".
           "VALUES ".
           "('$pname','$use','$depID', NOW(),'$manID', '$manName', '$suppID', '$suppName', '$cat', '$brand', '$name', '$email', '$custom_code', '$user', $method)";
    $result = fquery_db($sql);
    if($result>1)
    {return true;}
    else
    {return false;}
}

If the code uses the variable name $name, it does not work. Using another variable name instead, like $pname, it works. If I use the variable name $name, it returns false.

Any ideas as to why this is happening?

Calling the function

    <?php

     $name = getPOST('name');
     $depID = getPOST('depID');
     $cat = getPOST('cat');
     $supp = getPOST('supp');
     $suppID = getPOST('suppID');
     $man = getPOST('man');
     $manID = getPOST('manID');
    $confirm = req_new('THIS IS A NAME', null, $depID, $manID, $man, $suppID, $supp, $cat, null, null, null, null, fauth_GetUserID(), 1);
?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

删除会话 2024-11-22 16:53:26

从问题下面的评论来看 - 有两个名为 $name 的参数,第二个参数设置为 NULL

function req_new(
    $pname, /* first $name, wich started to work after renaming to $pname */
    $use=null, $depID=null, $manID=null, $manName=null, $suppID=null,
    $suppName=null, $cat=null, $brand=null,
    $name, /* second $name, which was set to NULL and overrode first argument */
    $email, $custom_code, $user=null, $method=null)
{
    // ...
}

From comments below the question - there were two arguments named $name, with second one being set to NULL:

function req_new(
    $pname, /* first $name, wich started to work after renaming to $pname */
    $use=null, $depID=null, $manID=null, $manName=null, $suppID=null,
    $suppName=null, $cat=null, $brand=null,
    $name, /* second $name, which was set to NULL and overrode first argument */
    $email, $custom_code, $user=null, $method=null)
{
    // ...
}
阳光①夏 2024-11-22 16:53:26

我无法重现OP的现象,至少不能在OP发布的代码范围内。

<?php

function bla($name, $whatever, $bla)
{
    if ($name == '') { return false; }
    return true;
}

$name = "ORLY?";
echo bla($name, null, null) . "\n"; // prints 1, as expected

?>

I can't reproduce OP's phenomenon, at least not within the extent of the code OP has posted.

<?php

function bla($name, $whatever, $bla)
{
    if ($name == '') { return false; }
    return true;
}

$name = "ORLY?";
echo bla($name, null, null) . "\n"; // prints 1, as expected

?>
猫瑾少女 2024-11-22 16:53:26

$name不是特殊的变量名,php仅保留名称< /a> 以 __ 开头(并且有一些继承的预定义变量)。我找不到一个以不同方式处理 $name 的程序。你能提供一个完整的例子吗?

请注意,您在 return false 之后缺少一个分号。打开错误调试来查看这些错误。

$name is not a special variable name, php only reserves names starting with __ (and there are a few inherited predefined variables). I couldn't find a single program where $name is handled differently. Can you provide a complete example?

Note that you're missing a semicolon after return false though. Turn on error debugging to see these errors.

荒芜了季节 2024-11-22 16:53:26

你怎么调用代码?由于您正在进行常规相等测试 (==),请记住 PHP 会自动为您转换值,并且有相当多的值等于空字符串。

例如

bla(0, ...);

仍会触发返回,因为在 PHP 领域 0 相当于相等测试中的 ''。 (0 == '' 为 TRUE)。使用严格相等测试来强制检查值和类型:

if ($blah === '') {
    return false;
}

这将按预期工作,因为当 0 == '' 时,严格检查会无形地附加到 int == string code> check,其计算结果为 FALSE。

How are you calling the code? Since you're doing a regular equality test (==), remember that PHP will auto-convert values for you, and there are quite a few values that are equal to an empty string.

e.g.

bla(0, ...);

will still trigger the return, because in PHP-land 0 is equivalent to '' in an equality test. (0 == '' is TRUE). Use the strict equality test to force checking value AND type:

if ($blah === '') {
    return false;
}

This will work as expected, because while 0 == '', the strict check invisibly tacks on a an int == string check, which evaluates to FALSE.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文