Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在php字符串中包含一个php变量_Php - Fatal编程技术网

在php字符串中包含一个php变量

在php字符串中包含一个php变量,php,Php,这对你们中的许多人来说一定很简单 <?php if (stripos($_SERVER['REQUEST_URI'],'/thispage/') !== false) {echo 'This page content';} ?> 但是我想用“$thisproduct['alias']”代替“thispage”,怎么做 我试着这样添加它: <?php if (stripos($_SERVER['REQUEST_URI'],'/$thisproduct['alias']/')

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

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

但是我想用“$thisproduct['alias']”代替“thispage”,怎么做

我试着这样添加它:

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


但是它给出了这个错误:Parse error:syntax error,意外的T_字符串

,因此您希望使用

stripos($_SERVER['REQUEST_URI'],'/'.$thisproduct['alias'].'/')
或双引号用于计算变量

stripos($_SERVER['REQUEST_URI'],"/{$thisproduct['alias']}/")

因此,您希望使用

stripos($_SERVER['REQUEST_URI'],'/'.$thisproduct['alias'].'/')
或双引号用于计算变量

stripos($_SERVER['REQUEST_URI'],"/{$thisproduct['alias']}/")
你会做:

<?php if (stripos($_SERVER['REQUEST_URI'], "/{$thisproduct['alias']}/") !== false) {echo 'This page content';} ?>
您将执行以下操作:

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

"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';} ?>
尝试以下操作:

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

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

有道理吗

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

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

如果您不是专门查找/alias/而只是查找alias,则最好使用此选项

否则

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

如果由于使用单引号而需要/alias/

,则变量将被视为 字符串,而不是变量实际包含的值

有道理吗

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

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

如果您不是专门查找/alias/而只是查找alias,则最好使用此选项

否则

if (FALSE !== stripos($_SERVER['REQUEST_URI'], "/{$thisproduct['alias']}/")) 
{
    echo 'This page content';
}
如果您需要/alias/