0

I know the undefined offset gives out warning instead of error but I want to know is there any possible way to avoid this kind of warning to pop out? I intend to write a function to test the undefined offset because I think that writing multiple similar if-else condition to test offset could be much work to be done.

function testOffset($item){
        if(isset($item)){
            return $item;
        }else{
            return "Nothing here!";
        }
    }
$array1[] = "Hello!";
echo testOffset($array1[1]);

In this case the function is work well but the warning will also pop out the moment I assign the unset element into function. Anyway to work around with it?


I purposely set the checking index to 1 to prove the function is working well

2

6 Answers 6

4

If your using PHP 7+ you can use null coalesce to make the whole thing just a one liner...

echo $array1[1] ?? "Nothing here!";
Sign up to request clarification or add additional context in comments.

Comments

4

You can try function like this

function issetOr($arr, $key)
{
    if (isset($arr[$key])) {
        return $arr[$key];
    } else {
        return "Nothing here!";
    }
}
$array1[] = "Hello!";
echo issetOr($array1, 1);

Or if you wan't to check for key existence use

function issetOr($arr, $key)
{
    if (\array_key_exists($key, $arr)) {
        return $arr[$key];
    } else {
        return "Nothing here!";
    }
}
$array1[] = "Hello!";
echo issetOr($array1, 1);

Demo online

4 Comments

Much better than using @ also.
I realize this is better than using @ although both way achieve the same result I was looking for in the early. @ means to suppress the warning notice but it is hard to tell that only undefined offset is the only error in other case.
Good @gitguddoge
You can use this native function php.net/manual/fr/function.array-key-exists.php its more better if you want other solution
3

There are multiple ways of handling this problem, but depending on what you are trying to do you could do:

foreach($array as $key => $val) {}

If you are just trying to get 1 element based on the key, you should just go with array_key_exists:

$array1[] = "Hello!";
echo (array_key_exists(1, $array1)) ? $array[1] : "No key";

2 Comments

I guess I didn't consider well into this because I used this in the same project by checking whether the string-based key exists in the array or not... Thanks ya!
If they are string based keys, you just add " " around the key
1

For escape warning and notice in php you can use simply an @

function testOffset($item){
    if(isset($item)){
        return $item;
    }else{
        return "Nothing here!";
    }
}
$array1[] = "Hello!";
echo @testOffset($array1[1]);

6 Comments

Thanks! This is exactly the answer I was looking for. I knew it would come out the warning notice but I just want to simply ignore it!
Using @ is not a good practice since it suppresses the error or warning which will be hard to debug for developer.
You are welcome, @AfrazAhmad here the developper assume the responsability, its an focntionallity who offer PHP for a lot of situations
There is other solution like array_key_exists or other @gitguddoge if you want we can chat and i will give you other solution.
@MohamedElMrabet really thanks for this answer but mleko's answer is better in this way. I'll still upvote for your answer anyway!
|
0

Because you are using offset 1, which is not exist,

you can do a simple code fixing by using the below:

function testOffset($item){
        if(isset($item)){
            return $item;
        }else{
            return "Nothing here!";
        }
    }
$array1[] = "Hello!";
echo testOffset($array1[0]);

1 Comment

thanks for the answer but i intend to do it in this way to try the function
-2

You can handle this by php array_values builtin function:

  1. $yourArray = array_values($yourArray);

This will rearrange your array indices e.g. You have a array like this:

 $a[0] = '...'
 $a[4] = '...'
 $a[7] = '...'
 $a[8] = '...'
 $a[9] = '...'

after using array_values

 $a[0] = '...'
 $a[1] = '...'
 $a[2] = '...'
 $a[3] = '...'
 $a[4] = '...'

2 Comments

By the mean stops the script execution means it will stops the script if this is in the loop right?
the warning does not actually stops the script as I tested earlier just now tho

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.