Chris Chris - 5 months ago 8
PHP Question

PHP function not seeing value of second parameter

For some odd reason my PHP is not seeing the value of the second parameter.

My code:

PHP function:

public function getVacs($key, $id = null, $deleted = null, $deleted_key = null) {

if(!$id) {
$data = $this->_db->getAll('vacatures', $key);
} elseif(!empty($deleted)) {
$data = $this->_db->getAll('vacatures', $key, $id, $deleted, $deleted_key);
} else {
$data = $this->_db->getAll('vacatures', $key, $id);
}

if($data->count()) {
$this->_data = $data->results();
$this->_count = $data->count();
return true;
}
}


Calling the function:

} elseif(isset($_POST['all'])) {

$vacs = $v->getVacs('delete', '0');

echo json_encode($v->data());
exit();
}


The problem is, the function does not see the value of
$id
.

It's running the first
if
while it should be running the
else
.

Ray Ray
Answer

In php the string "0" evaluates to false.

This means your check if(!$id) will evaluate to true and by your logic id won't be set in $data.

If the string "0" is legitimate option, then check for null explicitly instead:

if(is_null($id)){ 

This will