PHP-语法[数组/变量]
我想使用PHP-语法[数组/变量],php,arrays,variables,Php,Arrays,Variables,我想使用stripos()来过滤变量$background,如果找到的字符返回键值为$background if(stripos($background,$color)!==false){$background=$color中键的值}否则{$background='No color Found'} $color = array ( 'R' => "Red", 'Y' => "Yellow", '$bgcolor' => ar
stripos()
来过滤变量$background
,如果找到的字符返回键值为$background
if(stripos($background,$color)!==false){$background=$color中键的值}否则{$background='No color Found'}代码>
$color = array (
'R' => "Red",
'Y' => "Yellow",
'$bgcolor' => array (
'#ffffd0' => "Yellow",
'#ddffdd' => "Green", ));
变量$bgcolor先前已定义,并返回十六进制颜色代码。
上述语法正确吗?对于变量键
使用双引号
。带有单引号的字符串将忽略变量
<?php
header('Content-Type: text/plain;');
$x = 'check';
$str = "$x";
echo $str, PHP_EOL;
$str = '$x';
echo $str, PHP_EOL;
?>
对于您提供的代码的其余部分,我想您需要如下内容:
<?php
//header('Content-Type: text/plain;');
$background = '#ffffd0';
$color = array (
'R' => "Red",
'Y' => "Yellow",
'#ffffd0' => "Yellow",
'#ddffdd' => "Green"
);
$result = array_key_exists($background, $color) ? $color[$background] : 'not set' ;
echo $result;
?>
谢谢行。。对“$bgcolor”(一个变量)使用双引号将起作用?else的if条件在哪里。可能会有错误。
<?php
//header('Content-Type: text/plain;');
$background = '#ffffd0';
$color = array (
'R' => "Red",
'Y' => "Yellow",
'#ffffd0' => "Yellow",
'#ddffdd' => "Green"
);
$result = array_key_exists($background, $color) ? $color[$background] : 'not set' ;
echo $result;
?>
Yellow