A. Korolev A. Korolev - 5 months ago 10
PHP Question

How to create a list of variables in php?

I'm sorry for silly questiton but I can't understand, how to create a list of variables that will be used in if/else loop. I have a variable $category_id and different values of this variable (1,2,3, etc). I want to make a statment like this

if (isset($category_id) && $category_id == '1') {
some code here

but I need to specify more values of this variable like

if (isset($category_id) && $category_id == '1,2,5,11') {
some code here

How can I do this?


Define the options you want to check for, then use in_array to check if one of them is used:

$categoryIds = array(1, 2, 5, 11);
if (isset($categoryId) && in_array($categoryId, $categoryIds)) {
    // some code here

This way when you add more IDs, only the array gets bigger rather than your code.