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;
}
}
}
Related
I have this function:
protected function compare_something($found_item, $saved_items)
{
foreach($saved_items as item) {
$compare_x = false;
$compare_y = false;
$compare_z = false;
$compare_x = strpos($found_item,$item->x) !== false ? true: false;
$compare_y = strpos($found_item,$item->y) !== false ? true: false;
$compare_z = strpos($found_item,$item->z) !== false ? true: false;
if($compare_x && $compare_y && $compare_z){
return $item;
}
}
return false;
}
I expect $item to be the object where $compare_x, $compare_y and $compare_z are true, however this function simply returns the entire eloquent object ($saved_items) instead of the compared one.
I am not sure why is this happening. I can always return:
$item->id
And then do Item::where("id",$item->id)->first();
But shouldn't just return $item just deliver me the chosen item?
Eloquent queries always return a collection, so you can easily use the filter function for this. After that call the first function to get the first element. If there are no elements in the collection, null will be returned.
For example:
return $saved_items->filter(function ($item) use ($found_item) {
return strpos($found_item, $item->x) !== false &&
strpos($found_item, $item->y) !== false &&
strpos($found_item, $item->z) !== false;
})->first();
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;
}
How can I use foreach to return true or false.
For example, this isn't working.
function checkArray($myArray) {
foreach( $myArray as $key=>$value ) {
if(!$value == $condition) {
return false;
}
else {
return true;
}
}
if ($checkArray == true) {
// do something
}
What is the correct syntax for using foreach as true/false function? If you can't do it, could you use it to change an existing variable to true/false instead?
You would return true if the element was found/condition is true. And after the loop return false - if it had been found, this statement wouldn't have been reached.
function checkArray($myArray) {
foreach($myArray as $key => $value) {
if ($value == $condition) {
return true;
}
}
return false;
}
if (checkArray($array) === true) {
// ...
}
Of course true and false are interchangeable, depending on what output you expect.
If you're just trying to find a value you can use
if (in_array($lookup, $arrayToSearch)) { }
It'll be more efficient than enumerating the whole array.
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;
}
}
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.