0

this must be simple for many of you.

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

but i want insted of "thispage" to have this "$thisproduct['alias']" how to do this?

I tried to add it like this:

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

but it gives this error: Parse error: syntax error, unexpected T_STRING

4 Answers 4

2

you would do:

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

emphasis on "/{$thisproduct['alias']}/"

Sign up to request clarification or add additional context in comments.

Comments

1

So you want to construct the string by using .

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

or double quotes evaluate variables

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

Comments

1

Since you're using Single Quotes, your variable is being treated as a string instead of the value the variable actually contains.

Make sense?

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

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

etc

Best handled by this if you aren't specifically looking for /alias/ and instead just looking for 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';
}

Otherwise

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

If you want /alias/

Comments

-1

try the below :

"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 ; 

So for your case you can do this :

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

The below is from : 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()}";
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.