2011-06-13 25 views
-1

我喜歡認爲我對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); 
?> 
+6

你可以給你的代碼中更大的一部分,也許整個函數的代碼?另外,你有沒有在'return false'中忘記分號? – insumity 2011-06-13 14:37:25

+0

用「$ namet」發佈你的代碼 – 2011-06-13 14:38:11

+0

@Luzhin:+1,這聽起來像是名稱衝突導致了一個問題。 – Jonah 2011-06-13 14:39:11

回答

1

從問題下面的評論 - 有兩個參數命名爲$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) 
{ 
    // ... 
} 
1

我無法重現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 

?> 
+2

這是OP未能呈現正確測試用例的演示,而不是他的問題的解決方案。 +1。 – 2011-06-13 14:43:11

0

$name不是一個特殊的變量名,PHP只reserves names__開始(也有一些繼承預定義變量)。我找不到一個程序,其中$name的處理方式不同。你能提供一個完整的例子嗎?

請注意,您在return false之後仍然缺少分號。打開error debugging查看這些錯誤。

0

你是怎麼調用代碼的?由於您正在進行常規的平等測試(==),請記住PHP會爲您自動轉換值,並且有相當多的值等於空字符串。

例如

bla(0, ...); 

仍然會觸發回,因爲在PHP-土地0相當於''在相等性試驗。 (0 == ''爲TRUE)。使用全等測試強制檢查的類型和值:

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

這將如預期,因爲儘管0 == '',嚴格檢查無形上的int == string檢查,這爲FALSE大頭針。

相關問題