0

这对你们中的许多人来说一定很简单。

<?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

4

4 回答 4

2

你会这样做:

<?php if (stripos($_SERVER['REQUEST_URI'], "/{$thisproduct['alias']}/") !== false) {echo 'This page content';} ?>

强调"/{$thisproduct['alias']}/"

于 2013-02-13T17:22:29.463 回答
1

所以你想通过使用来构造字符串.

stripos($_SERVER['REQUEST_URI'],'/'.$thisproduct['alias'].'/')

或双引号评估变量

stripos($_SERVER['REQUEST_URI'],"/{$thisproduct['alias']}/")
于 2013-02-13T17:22:16.373 回答
1

由于您使用的是单引号,因此您的变量被视为字符串,而不是变量实际包含的值。

有道理?

$array = array('foo' => 'bar);

echo $array;            //array()
echo $array['foo'];     //bar
echo '$array[\'foo\']'; //array['foo']
echo "{$array['foo']}"; //bar

ETC

如果您不是专门寻找/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';
}

如果你想要/别名/

于 2013-02-13T17:35:24.500 回答
-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()}";
?>
于 2013-02-13T17:23:33.047 回答