Mike
Mike

Reputation: 605

How to assign multiple values to a PHP variable?

How do I go about assigning multiple values for a single variable in PHP?

I'm trying to do this

$IP = '111.111.111.111' || '222.222.222.222' || '333.333.333.333';

if ($_SERVER['REMOTE_ADDR'] != $IP) {
echo "Your IP is not on the list";

Is that even a proper way to do that?

Or should I put them in an array like this

$IP = array('111.111.111.111', '222.222.222.222', '333.333.333.333');

But then how would I go about checking if the $_SERVER['REMOTE_ADDR'] is one of the values inside the array?

Thanks

Upvotes: 0

Views: 9551

Answers (2)

Jonathan
Jonathan

Reputation: 2877

this is what you're looking for

/** my own array preference **/
$allowedIPS = [ '111.111.111.111', '222.222.222.222', '333.333.333.333' ];

/** alternate syntax **/
$allowedIPS = array ( '111.111.111.111', '222.222.222.222', '333.333.333.333' );

if (false === in_array($_SERVER['REMOTE_ADDR'], $allowedIPS)) {
    echo "Your IP is not on the list";
}

Upvotes: 1

potashin
potashin

Reputation: 44581

Yes, you should put it in array an use in_array() function instead of != operator:

if (!in_array($_SERVER['REMOTE_ADDR'], $IP))

Upvotes: 3

Related Questions