对于你们许多人来说这一定很简单。
<?php if (stripos($_SERVER['REQUEST_URI'],'/thispage/') !== false) {echo 'This page content';} ?>
但是我想把“thispage”放到这个“$ thisproduct ['alias']”这个怎么办?
我试着像这样添加它:
<?php if (stripos($_SERVER['REQUEST_URI'],'/$thisproduct['alias']/') !== false) {echo 'This page content';} ?>
但是它给出了这个错误:解析错误:语法错误,意外的T_STRING
答案 0 :(得分:2)
你会这样做:
<?php if (stripos($_SERVER['REQUEST_URI'], "/{$thisproduct['alias']}/") !== false) {echo 'This page content';} ?>
强调"/{$thisproduct['alias']}/"
答案 1 :(得分:1)
因此,您希望使用.
stripos($_SERVER['REQUEST_URI'],'/'.$thisproduct['alias'].'/')
或双引号评估变量
stripos($_SERVER['REQUEST_URI'],"/{$thisproduct['alias']}/")
答案 2 :(得分:1)
由于您使用单引号,因此您的变量将被视为 字符串而不是变量实际包含的值。
有意义吗?
$array = array('foo' => 'bar);
echo $array; //array()
echo $array['foo']; //bar
echo '$array[\'foo\']'; //array['foo']
echo "{$array['foo']}"; //bar
等
如果您不是专门寻找 / alias / 而只是寻找别名
,最好由此处理// were $thisproduct['alias'] is now treated as a variable, not a string
if (FALSE !== stripos($_SERVER['REQUEST_URI'], $thisproduct['alias']))
{
echo 'This page content';
}
否则
if (FALSE !== stripos($_SERVER['REQUEST_URI'], "/{$thisproduct['alias']}/"))
{
echo 'This page content';
}
如果您想要 / alias /
答案 3 :(得分:-1)
尝试以下方法:
"this is a text and this is a $variable " ; // using double quotes or
"this is a test and this is a {$variable} "; // or
"this is a test and this is a ". $variable ;
因此,对于您的情况,您可以这样做:
<?php if (stripos($_SERVER['REQUEST_URI'], "/{$thisproduct['alias']}/") !== false) {echo 'This page content';} ?>
以下内容来自:http://php.net/manual/en/language.types.string.php
<?php
// Show all errors
error_reporting(E_ALL);
$great = 'fantastic';
// Won't work, outputs: This is { fantastic}
echo "This is { $great}";
// Works, outputs: This is fantastic
echo "This is {$great}";
echo "This is ${great}";
// Works
echo "This square is {$square->width}00 centimeters broad.";
// Works, quoted keys only work using the curly brace syntax
echo "This works: {$arr['key']}";
// Works
echo "This works: {$arr[4][3]}";
// This is wrong for the same reason as $foo[bar] is wrong outside a string.
// In other words, it will still work, but only because PHP first looks for a
// constant named foo; an error of level E_NOTICE (undefined constant) will be
// thrown.
echo "This is wrong: {$arr[foo][3]}";
// Works. When using multi-dimensional arrays, always use braces around arrays
// when inside of strings
echo "This works: {$arr['foo'][3]}";
// Works.
echo "This works: " . $arr['foo'][3];
echo "This works too: {$obj->values[3]->name}";
echo "This is the value of the var named $name: {${$name}}";
echo "This is the value of the var named by the return value of getName(): {${getName()}}";
echo "This is the value of the var named by the return value of \$object->getName(): {${$object->getName()}}";
// Won't work, outputs: This is the return value of getName(): {getName()}
echo "This is the return value of getName(): {getName()}";
?>