0

Example: $array = ['name', 'address'];

It contains $array[0] and $array[1] only.

If i try $array[2] i should receive an error.

So, i want to avoid these errors by checking it in a if statement. How can i check if an array contains a value in certain index?

0

2 Answers 2

1

You can use the isset function:

if(isset($array[2])) {
Sign up to request clarification or add additional context in comments.

Comments

0

The second way to make it work is check how big is an array:

if(count($array) >= 3) { ... } // three or more elements: 0, 1, 2...

Comments

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.