Can php skip part of my function by some (server) error?

The name of the picture


Can php skip part of my function by some (server) error?



I have searched the site for a question that is the same or similar, but I cannot find what I am looking for. Perhaps this is because my question is somewhat strange or unlogical (but I hope you'll be able to clear that up).



Here's the case: I have a function to test to see if something is not in an array. I have to make absolutely sure this function returns only true if what I look for is not in the array. I have recreated a simplified function so you understand what I mean. In this function I want to make sure there is not any 'A' in my array, one or more.


'A'


$array = ["A", "F", "B", "G"]

function check($array) {
$noAs = false;
$n = 0;

foreach ($array as $letter) {
if ($letter == "A") {
$n++;
}
}

if ($n == 0) {
$noAs = true;
};

return $noAs;
}



I then can show the result:


if (check($array) == true) {
echo "the are no A's";
} else {
echo "there are A's";
}



The above function work fine, but as you can see with $noAs = true; I, by default, set the return value to true. My fear is, that if by some server error the function is not executed properly, the function will return true even though there are A's in my array. Am I making sense or is this never gonna happen?


$noAs = true;



In order to set the default $noAs = false; I created this function which also works:


$noAs = false;


function check2($array)
{
$noAs = false;
$n = 0;

foreach ($array as $letter) {
if ($letter == "A") {
$n++;
}
}

if ($n == 0) {
$noAs = true;
}$array = ["A", "F", "B", "G"];

return $noAs;
}



So my question is, was it wise to have created this second function instead of the first one, or does it really not matter at all?





Why not just use is_array()?
– Jay Blanchard
5 mins ago


is_array()





Or rather in_array(), actually. But anyway, if there is an error, no more code will be executed, nowhere, so what do you fear exactly?
– Blackhole
1 min ago


in_array()





Is this normal your first function doesn't return anything ? you could do something bit sorther with return count(array_filter($array, function($e) { return $e === "A"; })) === 0; if you don't want do use native function
– Zyigh
1 min ago




return count(array_filter($array, function($e) { return $e === "A"; })) === 0;





Thx for the suggestion, but my question is not about how to find something in an array (I know I could us is_array()) but if (in any function really) it is best to assume a response as true/false depending on what you test. I hope this makes sense...
– Dirk J. Faber
1 min ago


is_array()





@DirkJ.Faber I think if there's a server error, the whole code will fail, no matters how many time you looped over your array
– Zyigh
15 secs ago




1 Answer
1



To answer your worries first. No there will be no point where a server error can cause your function return the opposite of expected result, as long as "A" is inside your array.



However, you seem to have done way too much coding for something that already exists.



You should use in_array:


$array = ["A", "F", "B", "G"]
if( in_array( "A", $array ) ) {
// Do stuff
} else {
// Do something else
}



If you want it to be case insensitive:


$array = ["A", "F", "B", "G"]
if( in_array( strtolower("A"), array_map('strtolower', $array) ) ) {
// Do stuff
} else {
// Do something else
}






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Arduino Mega cannot recieve any sketches, stk500_recv() programmer is not responding

Visual Studio Code: How to configure includePath for better IntelliSense results

Future solutions