简体   繁体   English

在 function 中使用变量“$name”

[英]Use of variable '$name' in a function

I like to think I'm quite knowledgeable on php, but this has baffled me.我喜欢认为我对 php 非常了解,但这让我感到困惑。

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.如果代码使用变量名$name ,则它不起作用。 Using another variable name instead, like $pname , it works.改用另一个变量名,如$pname ,它可以工作。 If I use the variable name $name , it returns false.如果我使用变量名$name ,它会返回 false。

Any ideas as to why this is happening?关于为什么会发生这种情况的任何想法?

Calling the function调用 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);
?>

I can't reproduce OP's phenomenon, at least not within the extent of the code OP has posted.我无法重现 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

?>

From comments below the question - there were two arguments named $name , with second one being set to NULL :根据问题下方的评论 - 有两个名为$name的 arguments ,第二个设置为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)
{
    // ...
}

$name is not a special variable name, php only reserves names starting with __ (and there are a few inherited predefined variables). $name不是特殊的变量名,php 只保留__开头的名称(并且有一些继承的预定义变量)。 I couldn't find a single program where $name is handled differently.我找不到一个以不同方式处理$name的程序。 Can you provide a complete example?你能提供一个完整的例子吗?

Note that you're missing a semicolon after return false though.请注意,您在return false之后缺少分号。 Turn on error debugging to see these errors.打开错误调试以查看这些错误。

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.由于您正在进行常规相等测试 ( == ),请记住 PHP 将为您自动转换值,并且有很多值等于空字符串。

eg例如

bla(0, ...);

will still trigger the return, because in PHP-land 0 is equivalent to '' in an equality test.仍然会触发返回,因为在 PHP-land 0中等于''在相等测试中。 ( 0 == '' is TRUE). 0 == ''为真)。 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.这将按预期工作,因为当0 == ''时,严格检查无形地附加了一个int == string检查,其计算结果为 FALSE。

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

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