2013-02-13 5 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';} ?> 

, но он дает эту ошибку: Parse ошибка: ошибка синтаксиса, неожиданный 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'; 
} 

Если вы хотите /алиас/

Смежные вопросы