2013-02-13 95 views
0

對於你們中的許多人來說,這一定很簡單。在php字符串中包含一個php變量

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

但我想insted的「thispage」有這個「$ thisproduct ['別名']」如何做到這一點?

我嘗試添加像這樣:

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

,但它給這個錯誤:解析錯誤:語法錯誤,意想不到的T_STRING

回答

2

你會怎麼做:

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

強調"/{$thisproduct['alias']}/"

1

所以,你要使用.

構建字符串
stripos($_SERVER['REQUEST_URI'],'/'.$thisproduct['alias'].'/') 

或雙引號評估變量

stripos($_SERVER['REQUEST_URI'],"/{$thisproduct['alias']}/") 
-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()}"; 
?> 
1

由於您使用單引號,你的變量被視爲 字符串而不是變量實際上包含的值。

有意義嗎?

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

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

最好此處理,如果你不是專門找/別名/,而是隻是爲了尋找別名

// 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'; 
} 

如果你想/別名/