Code
$descriptionArr = array( "uk/page"=>"", "uk/page-two"=>"description of page 2");
function getDescription($uri){
if (array_key_exists($uri, $descriptionArr)) {
return $descriptionArr[$uri];
} else {
return false;
}
}
Situation
When i call the function with argument "uk/page-two" it returns the description
When I call the function with argument "uk/page" it returns false instead of the empty string
Issue
I would like it to return the empty string and only return false when the argument passed does not exist as key in the array.
This should work:
$descriptionArr = array( "uk/page"=>"", "uk/page-two"=>"description of page 2");
function getDescription($uri, $descriptionArr){
if (false !== array_key_exists($uri, $descriptionArr)) {
return $descriptionArr[$uri];
} else {
return false;
}
}
You can change your function to the following:
function getDescription($uri) {
if (isset($descriptionArr[$uri])) {
return $descriptionArr[$uri];
} else {
return false;
}
}
Related
Why is this my code returning false in my if condition at my callback code. I tried to var_dump every each of their values and
Here is the output of the var_dump
var_dump($old_password_hash); = string(32) "25d55ad283aa400af464c76d713c07ad"
var_dump($old_password_db_hash); = object(stdClass)#24 (1) { ["user_password"]=> string(32) "25d55ad283aa400af464c76d713c07ad" }
The two values doesnt satisfies in if($old_password_hash != $old_password_db_hash) {
Here is my full code
public function oldpassword_check($old_password){
$id = $this->input->post('userid');
$old_password_hash = md5($old_password);
$old_password_db_hash = $this->prof_model->fetch_pwrod($id);
//var_dump($old_password_hash);
var_dump($old_password_db_hash);
if($old_password_hash != $old_password_db_hash) {
$this->form_validation->set_message('oldpassword_check', 'Old password not match');
return FALSE;
} else {
return TRUE;
}
}
$old_password_hash is a string
$old_password_db_hash is an object
They will never be equal. string never equals object.
That's why $old_password_hash != $old_password_db_hash is always true. And therefore FALSE is returned.
Proper check is:
// take `user_password` property of an object
if ($old_password_hash != $old_password_db_hash->user_password) {
$this->form_validation->set_message('oldpassword_check', 'Old password not match');
return FALSE;
}
else {
return TRUE;
}
Getting this error on my site for line 113, not sure why this is happening or what needs to be done to correct the code. See the snippet below.
// Return the CDN object by name.
// If a section is defined. Only returns the object if it exists in that section
// if "Section" is not defined; returns an object if it exists in any section.
// Returns FALSE if not found.
public function CDN($name='',$section=''){
if (empty($name)) return FALSE;
else
foreach ($this->CDNS as $CDN){
if (!empty($section)) {
if (stripos($name, $CDN->Name()) !== FALSE && ($CDN->Position() == $section)) return $CDN;
}
else
if (stripos($name, $CDN->Name()) !== FALSE) return $CDN;
}
return FALSE;
}
It seems $this->CDNS is empty. Do like below:
public function CDN($name='',$section=''){
if (empty($name)) return FALSE;
else
{
if(!empty($this->CDNS))
{
foreach ($this->CDNS as $CDN){
if (!empty($section)) {
if (stripos($name, $CDN->Name()) !== FALSE && ($CDN->Position() == $section)) return $CDN;
}
else
if (stripos($name, $CDN->Name()) !== FALSE) return $CDN;
}
return FALSE;
}
}
}
Is it possible to return an array, but also tell php it's supposed to mean false?
Example:
if ($res = a_function()) {
// all good
}
else {
echo getErrorByNumber($res['err_no']);
}
a_function:
function a_function() {
// do fancy stuff
if (xy) return true;
return array('err_no' => 1);
}
I guess its not possible, since php will always take an array for return true, right?
Lot's of ways. Probably the preferred one, compare to true with type checking ===:
if(($res = a_function()) === true) {
// all good
}
else {
echo getErrorByNumber($res['err_no']);
}
A non-empty array will always be true:
if($res = a_function() && !is_array($res)) {
// all good
}
else {
echo getErrorByNumber($res['err_no']);
}
Or flip it around:
if(is_array($res)) { //or isset($res['err_no'])
echo getErrorByNumber($res['err_no']);
}
else {
// all good
}
I would solve this problem with a byref parameter:
function foo(&$errors)
{
if (allWentWell())
{
$errors = null;
return true;
}
else
{
$errors = array('err_no' => 007);
return false;
}
}
// call the function
if (foo($errors))
{
}
else
{
echo getErrorByNumber($errors['err_no']);
}
This way you do not have to distinguish between different possible return types and you will not run into type juggling problems. It is also more readable, you know what's inside the $errors variable without documentation. I wrote a small article explaining why mixed-typed return values can be so dangerous.
Issue Resolved : Here is the solution :
function testCoreq()
{
$coreqTest = makeCoreq();
if(empty($coreqTest))
{
return array(true);
break;
}
else
{
foreach ($coreqTest as $ctest)
{
if($ctest['value'] == "true")
{
return array(true);
break;
}
else
{
return array(false,$ctest['coreqID']);
}
}
}
}
if(testCoreq()[0])
{
//do something
}
else
{
return testCoreq()[1]
}
I'm doing a school project and hit kind of a bump.
I created a function and i want it to either return "true" (boolean) or "false" (boolean) + a variable.
I searched the net quite a bit but wan't able to find a simple way to do this .
Is there any way to this this ? //Thanks
The function is working properly but when it is returning the variable - it is also assuming that the function is returning "true" when i want it to return false + the value like :
else
{
return $ctest['coreqID'];
return false;
}
Here is the code :
function testCoreq()
{
$coreqTest = makeCoreq();
if(empty($coreqTest))
{
return true;
break;
}
else
{
foreach ($coreqTest as $ctest)
{
if($ctest['value'] == "true")
{
return true;
break;
}
else
{
return $ctest['coreqID'];
}
}
}
}
I am using it like this:
if (testCoreq())
{
// do something
}
else
{
// return the variable
}
but even if the first statement is false , then it is returning the variable - it is assuming the function is true.
You can try to return -1 on true and other for false to decrease amount of returning values. Next option is to return array of values. The other option would be to pass reference variable in the function.
I loop through the values of a form, to check that each field has 4 digits. My problem is currently it validates true or false only on the match for the first field $card1...
function cardcheck ($card1,$card2,$card3,$card4)
{
$cards = array($card1,$card2,$card3,$card4);
$regex = "/[0-9]{4}/";
for ($i=0;$i<4;$i++)
if (! preg_match ($regex,$cards[$i]))
{
return false;
}
else
{
return true;
}
}
You're returning (by using return ...) something in the first iteration every time (boolean condition with an else).
You need to put the return true outside the loop statement:
function cardcheck ($card1,$card2,$card3,$card4)
{
$cards = array($card1,$card2,$card3,$card4);
$regex = "/[0-9]{4}/";
for ($i=0;$i<4;$i++) {
if (! preg_match ($regex,$cards[$i])) {
return false;
}
}
return true;
}
function cardcheck ($card1,$card2,$card3,$card4)
{
$cards = array($card1,$card2,$card3,$card4);
$regex = "/[0-9]{4}/";
for ($i=0;$i<4;$i++)
if (! preg_match ($regex,$cards[$i]))
{
return false;
}
return true;
}