troubleshooting Question

PHP: Passing zero as function parameter

Avatar of rmartes
rmartes asked on
PHP
7 Comments2 Solutions403 ViewsLast Modified:
Hi Experts,

I'm getting weird code execution when passing "0" as a function parameter. I've been hacking at it for a couple days now, but am totally stumped. I thought it was code logic, but cannot figure it out.

I have these simple functions:

function function1($num, $type, $code) {
 switch ($code) {
  case "0":
   $result = 1;
   break;
  default:
   $result = 2;
 }
 function2($result); // this function simply stores $result into database
 return $result;
}

function function2($result) {
 $sql = "insert into tbl (result) values ('$result')";
 $result = mysql_query($sql, $conn);
}

If I do the following calls:

function1("1234","operator","")
function1("1234","operator","1")
function1("1234","operator","12") 
function1("1234","operator","whatever") 
// or any call where $code is not "0"...

function2 is called, $result is stored into the database and $result is returned.

However, if I do this call:

function1("1234","operator","0")

function2 is never called, $result is never stored into database, BUT $result is returned.

There is nothing in function1 or function2 restricting this when "0" is passed. I am not getting any errors from PHP or MySql, I am stumped. Please help.

Thanks in advance.
ASKER CERTIFIED SOLUTION
Cornelia Yoder
Artist

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 2 Answers and 7 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 2 Answers and 7 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros