Callback function returning false CodeIgniter - php

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;
}

Related

Return an empty but tell php it means `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.

"array_key_exists()" not working properly

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;
}
}

PHP : Function - Return 2 variables (Boolean + $variable)

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.

Can't check if $_POST variable has left empty

I need to validate a form therefore I am writing a php class that does just that. I need to check if a $_POST variable has been set or not in order to determine whether display an error message. So I have implemented two methods which don't seem to work as I expect, because even if I leave my form blank, it is processed as if data has been filled in, and I just don't understand.
private function isSubmitted($field) {
if (!array_key_exists($field, $_POST)) {
return false;
} else {
return true;
}
}
private function hasContent($field) {
if (!empty($_POST[$field])) {
return false;
} else {
return true;
}
}
Even when a field is left empty, it is submitted with "" (an empty string) as its content. Therefore, array_key_exists will return true.
if not empty return false is the opposite logic of what you're trying to do.
Abbreviating your boolean returns to return array_key_exists($field, $_POST); should be considered saner, at the very least more concise.
I think you can try something like this
private function isSubmitted($field) {
return isset($_POST[$field]);
}
private function hasContent($field) {
return !empty($_POST[$field]);
}
private function hasContent($field) {
if (!empty($_POST[$field])) {
return true;
} else {
return false;
}
}
I think you should do a minor change.you should also check whether the existed array have value or is it empty.

PHP return false with a string

I am looking for the correct way to handle a return statement with a bool/string. For example I do all my checking inside the function and return true if it all passes. However if something went wrong I would like to return a string of what went wrong rather than just return false; with a general string. Does php assume false if a var is set to anything besides true? What is the correct way to handle this? Here's an example of what I'm doing
<?php
$a = 2;
$result = CheckVar($a);
if ($result)
{
echo 'Correct!';
}
else
{
echo $result;
}
function CheckVar($var)
{
if ($var == 1)
{
return true;
}
else
{
return 'This is not the correct answer. You supplied '.$var;
}
}
?>
It seems this method works, however is this good programming etiquette? Or is there another way I should be doing this? Thank you for your time.
Does php assume false if a var is set to anything besides true?
Not at all. PHP will return whatever the variable was set to. And actually since you have a non-empty string, that's a "truthy" value (ie: true in a boolean context). Since you used if ($result) as your check and you return a "truthy" value, the condition is always true. You need to change that check to:
if ($result === true) {
...
What is the correct way to handle this?
I think it's a good enough way to handle it. An alternative would be to pass an error string variable by reference, and have the fail part of your code fill that, eg:
function check($var, &$error) {
if ($var == 1) {
return true;
} else {
$error = 'This is not the correct answer. You supplied ' . $var;
return false;
}
}
Some native PHP functions behave like this (eg: exec().) Yet another alternative is to return an array with the errors, like Jared suggested. I personally use this option when I expect multiple errors (eg: a form validation routine):
function check_stuff($stuff) {
$errors = array();
if (!$condition1) {
$errors[] = 'Condition 1 failed';
}
if (!$condition2) {
$errors[] = 'Condition 2 failed';
}
return $errors;
}
Now you can also take advantage of the fact that empty arrays are falsy:
$errors = check_stuff($your_stuff);
if (!$errors) {
echo 'No errors!';
} else {
print_r($errors);
}
You can use === to check if the returned value is boolean true. === checks the type as well the value.
if ($result === true)
{
echo 'Correct!';
}
else
{
echo $result;
}
I came up against this recently, my function would either return an error message as a string or return true like this:
function check_something(){
if(condition){
return 'error message';
}
// if we got this far all is good!
return true;
}
I would call it and check the outcome like this:
$var = check_something();
if($var !== true){
// $var is not boolean true, so it must be a string
echo $var;
}
This checks that the outcome of the function is not just a truthy string, but is explicitly a boolean true
This could be useful to someone returning true or returning false as a string.
if (is_bool($result))
{
echo 'Result is a true bool';
}
else
{
echo $result.'returning a string';
}

Categories