easyquestions easyquestions - 11 days ago 5
PHP Question

strpos code then add value to title

I'm trying to get

strpos
working the way I want, but stumbled over a few problems here.

The deal is I want to add a text to the title, if the title includes a specific work.

My title is represented as:

<h4><?php echo $article->name; ?></h4>


Then I want to use a code like this:

$a = '$article->name';

if (strpos($a, 'banana') !== false) {
echo 'is good';
}


However it it fails.

Does anyone know how I can get
$a
to read my title (which is a gode, not just a text)?

How do I replace the echo
is good
with a picture? (same issue here as my problem is that I dont understand how to get the code working withi the
''
). I know the
img src=""
, but just don't how to get it work in this code.

Answer

It seems that you are having trouble with variable interpolation in strings. Always good to check what is in the manual.

Basically, if you use single quotes the characters will be taken literally. So, to interpret a variable value inside a string, use double quotes.

Additionally, there is obviously no need to interpolate a single value:

$a = "$article->name"; # not needed
$a = $article->name;   # better

And you can also concatenate values instead of interpolating them:

echo "<img src=\"$url\" />";
echo '<img src="' . $url . '" />';