We help IT Professionals succeed at work.

PHP: Passing zero as function parameter

rmartes
rmartes asked
on
399 Views
Last Modified: 2014-08-09
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);
}

Open in new window


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

Open in new window


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

However, if I do this call:

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

Open in new window


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.
Comment
Watch Question

Most Valuable Expert 2011
Author of the Year 2014
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
CERTIFIED EXPERT
Expert of the Year 2014
Top Expert 2014

Commented:
Is this your real code
function1("1234","operator","")
function1("1234","operator","1")
function1("1234","operator","12") 
function1("1234","operator","whatever") 

Open in new window

Where is the ; after each line?
Dave BaldwinFixer of Problems
CERTIFIED EXPERT
Most Valuable Expert 2014

Commented:
I agree with @yodercm.  I tested a variation of his code and got the right results each time.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
 "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<title>Function Code Passing 0</title>
</head>
<body>
<h1>Function Code Passing 0</h1>
<pre>

<?php
function function1($num, $type, $code)
{
      $result = '';
    echo "Debug A $result<br>";
    switch ($code)
    {
     case "0":
      $result = 1;
    echo "Debug B $result<br>";
      break;
     default:
      $result = 2;
      echo "Debug C $result<br>";
      }
    echo "Debug D $result<br>";

    function2($result); // this function simply stores $result into database
    echo "Debug E $result<br>";
 return $result;
}

function function2($result)
{
 echo "Debug F $result<br>";
}

$result = function1("1234","operator","");
echo "Debug G $result<br><br>";
$result = function1("1234","operator","0");
echo "Debug G $result<br><br>";
$result = function1("1234","operator","1");
echo "Debug G $result<br><br>";
$result = function1("1234","operator","2");
echo "Debug G $result<br><br>";

//exit();
?>

</pre>
</body>
</html>

Open in new window

Commented:
I haven't set up a full MySQL table to test this, but I suspect the problem lies in your use of the variable $result within each function differently, and the fact that it is used quite differently in the different places.

Author

Commented:
Thanks you guys. Even though they are not the direct answers, they led me in the right direction. The above is not my actual code, its just the idea I was trying to accomplish. The issue was not passing in zero. My code sits behind a web service. I decided to take the code out and test the code separately even though the web service was not producing an error. I immediately saw errors when I did so. There were actually some variables that I was not instantiating when the case was "0". I got it working now. Thanks again.
Most Valuable Expert 2011
Author of the Year 2014

Commented:
Glad to help, ~Ray
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.