变量“$name”的使用在一个函数中
我喜欢认为我对 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
从问题下面的评论来看 - 有两个名为
$name
的参数,第二个参数设置为NULL
:From comments below the question - there were two arguments named
$name
, with second one being set toNULL
:我无法重现OP的现象,至少不能在OP发布的代码范围内。
I can't reproduce OP's phenomenon, at least not within the extent of the code OP has posted.
$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.你怎么调用代码?由于您正在进行常规相等测试 (
==
),请记住 PHP 会自动为您转换值,并且有相当多的值等于空字符串。例如
仍会触发返回,因为在 PHP 领域
0
相当于相等测试中的''
。 (0 == ''
为 TRUE)。使用严格相等测试来强制检查值和类型:这将按预期工作,因为当
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.
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:This will work as expected, because while
0 == ''
, the strict check invisibly tacks on a anint == string
check, which evaluates to FALSE.