• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 338
  • Last Modified:

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.
0
rmartes
Asked:
rmartes
2 Solutions
 
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
 
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now