PHP: Passing zero as function parameter

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.
rmartesAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ray PaseurCommented:
Add error_reporting(E_ALL) to the top of the script.  PHP has loose data typing.  We sometimes call this "duck typing" because if it walks like a duck and quacks like a duck, it must be a duck.  PHP variables can change shape without warning!  Zero is especially prone to un-noticed changes.  It can mean FALSE, NULL, Zero, empty(), etc.

You may want to get some foundation in how PHP works.  This article can help.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_11769-And-by-the-way-I-am-new-to-PHP.html

You will want to get off MySQL as soon as possible.  This article can help.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/PHP_Databases/A_11177-PHP-MySQL-Deprecated-as-of-PHP-5-5-0.html

Good luck and after you read the articles, if you are still not sure how to deal with the issues, please post back with any specific questions. ~Ray
0
Cornelia YoderArtistCommented:
The problem is not in your "0" because the attached code works and produces the output shown.

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

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

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

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

exit();
?>

Open in new window





Output:
Debug 1
Debug 2 1
Debug 4 1
Debug 6 1
Debug 5 1
Debug 8 1


So if you follow the debug statements, you see that flow is going through the Case for "0", and function2.

Did you use a variable for the return value when you called function1, as I did?
Are you sure using the same variable $result for both a single integer, and later as the result of a query is what you want to do?  That may well be contributing to your problem.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
GaryCommented:
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?
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

Dave BaldwinFixer of ProblemsCommented:
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

0
Cornelia YoderArtistCommented:
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.
0
rmartesAuthor 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.
0
Ray PaseurCommented:
Glad to help, ~Ray
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.