A
A
Alexey2020-05-17 18:28:49
PHP
Alexey, 2020-05-17 18:28:49

How to limit the options for the value of an argument in a method?

Hello! I need to make it so that in the function

function getContent($arg){
//.....
}

The $arg argument has been limited to a few string options. For example "posts", "news" and "comments".
I understand that I can do a check on the in_array() type, but I don't like this option. Once, either in a book or in some code, I came across a variant with constants and I can’t remember it.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Sergey Nikolaevich, 2020-05-17
@dzheka3d

if the input is an associative array, then for example like this

$arg = array_intersect_assoc($arg, array_flip(['post', 'news', 'comments']))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question