I have the code :
function insertChamado($id, $area = 2)
{
if ($area != 2 && $area != 4)
$area = 2;
How Can I adjust this code to not accept 0 condition as show in the log bellow :
[12-May-2016 16:58:28 America/Sao_Paulo] id = 36445, area = 0
[12-May-2016 16:59:00 America/Sao_Paulo] id = 14635, area = 0
[12-May-2016 17:00:02 America/Sao_Paulo] id = 18599, area = 0
After reading carefully your question i think that your best solution is a simple switch
.
function insertChamado($id, $area = 2){
switch ($area) {
case 2:
echo "area equals 2
";
break;
case 4:
echo "area equals 4
";
break;
default:
echo "area is always 2 other wise
";
}
}
insertChamado('id',0); // will output "area is always 2 other wise"
insertChamado('id'); // will output "area equals 2"
Just add a conditional to check for it... Not sure what is hard about it unless we are missing something.
function insertChamado($id, $area = 2)
{
if ($area == 0) die("Ruh-Rohh");
if ($area != 2 && $area != 4)
$area = 2;
}
Or if you expect it to be 2 if it is 0:
function insertChamado($id, $area = 2)
{
if (($area != 2 && $area != 4) || $area == 0) // Though || $area == 0 actually does nothing here as 0 already meets the previous condition.
$area = 2;
}
It occurs to me after the fact, that $area could never be 0 even in your original code as 0 != 2 and 0 != 4 thus $area = 2. I suspect an implementation issue, if this does not help I suggest you edit your ques to include more code.
Could be a scope issue, as you are not using a global $area and are not returning a value, the changed $area may not be breaking out of the function.
Try one of the se implementations:
Using global
$area = 0; // for testing only
function insertChamado($id)
{
global $area;
if ($area != 2 && $area != 4)
$area = 2;
}
Or using a return:
$area = insertChamado(0,0);
function insertChamado($id, $area = 2)
{
if ($area != 2 && $area != 4)
$area = 2;
return $area;
}
The incomplete code you supplied does not help as I have no idea what the implementation of id
is.