PHP - Syntax [array / variable] -
i want use stripos()
filter variable $background
, if characters found return keys value $background
;
if(stripos($background, $color) !== false) { $background = value of keys in $color } else { $background = 'no color found'};
$color = array ( 'r' => "red", 'y' => "yellow", '$bgcolor' => array ( '#ffffd0' => "yellow", '#ddffdd' => "green", ));
the variable $bgcolor defined , returns hexadecimal color code. above syntax correct ?
use double quotes
variable key
. strings single quotes
ignoring variables.
<?php header('content-type: text/plain;'); $x = 'check'; $str = "$x"; echo $str, php_eol; $str = '$x'; echo $str, php_eol; ?>
shows:
check $x
for rest part of code you`ve provided, guess want this:
<?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; ?>
shows:
yellow
Comments
Post a Comment