-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);
?>
Abe Petrillo
  • 2,368
  • 23
  • 34
  • 6
    could you give a bigger part of your code, maybe whole the function's code? Also, have you forgot the semicolon in `return false`? – insumity Jun 13 '11 at 14:37
  • @Luzhin: +1, it sounds like there's a name collision causing a problem. – Jonah Jun 13 '11 at 14:39
  • 1
    Does PHP have variadic functions now?! – Lightness Races in Orbit Jun 13 '11 at 14:42
  • 1
    @Tomalak Geret'kal, I've used variadic functions in PHP for years (since version 4). See [`func_num_args()`](http://www.php.net/func_num_args), [`func_get_arg()`](http://www.php.net/func_get_arg) and [`func_get_args()`](http://www.php.net/func_get_args), as well as "[Default argument values](http://www.php.net/manual/en/functions.arguments.php#functions.arguments.default)". If talking about `, $bla....` part, it is obvious that it is just a *pseudo code*. – binaryLV Jun 13 '11 at 14:46
  • Edited the answer to include the full function – Abe Petrillo Jun 13 '11 at 14:57
  • And for the record, why the -1? I kept it basic to see if the more experienced had obvious answers like $name being a keyword. People really are trigger happy with the down arrow! – Abe Petrillo Jun 13 '11 at 15:03
  • 1
    In original version, you had `$name` as first argument and you compared it with `''`. In edited version, you have `$name` as *n-th* argument, but comparison is being done with `$pname`, which is the first argument. Ideologically, it is not the same case. – binaryLV Jun 13 '11 at 15:04
  • ah! Apologies, I didn't realise I had two $name variables. Much appreciated binaryLV – Abe Petrillo Jun 13 '11 at 15:09
  • @binaryLV: Ah, well I would appreciate specific code samples not thinly-veiled pseudo-code! – Lightness Races in Orbit Jun 13 '11 at 15:54
  • @Abe, was that the reason why `$name` did not work? – binaryLV Jun 13 '11 at 16:37
  • yes, as the second $name was set to null – Abe Petrillo Jun 13 '11 at 18:02

4 Answers4

1

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

?>
miku
  • 181,842
  • 47
  • 306
  • 310
1

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)
{
    // ...
}
binaryLV
  • 9,002
  • 2
  • 40
  • 42
0

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

phihag
  • 278,196
  • 72
  • 453
  • 469
0

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.

Marc B
  • 356,200
  • 43
  • 426
  • 500