简体   繁体   中英

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

是的,您应该使用use in_array()函数而不是!=运算符将其放在数组中:

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

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";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM