Question

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);
?>
Was it helpful?

Solution

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)
{
    // ...
}

OTHER TIPS

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

?>

$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.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top