开发者

echo function output inside html

开发者 https://www.devze.com 2023-02-10 14:12 出处:网络
I have something like this in one field of my table(MySql): $data = \'<td>apple</td>\'; echo $data;

I have something like this in one field of my table(MySql):

$data = '<td>apple</td>';
echo $data;

I select this field and echo it into the page.I want to replace 'apple' word with a php function that return a word.So I thought

$data = '<td>myphp_function('fruit');</td>';
echo $data;

but what I see in the page is exactly the line above and not my function output. h开发者_开发问答ow can I do it? I am not sure if i could explain my mean clearly...

Edited.


According to your last edit, what you need is the following:

$data = '<td>' . myphp_function('fruit') . '</td>';
echo $data;

This is assuming your myphp_function() will return some kind of value.
If the function echoes the value, it will not work as expected!

You can only execute PHP when you open PHP tags. Other than that, it's just plain text/html.

<td>myphp_function('fruit');</td>

To execute your function you have to open PHP tags:

<td><?php myphp_function('fruit'); ?></td>


you have to insert some sort of placeholder into your text. Like this

<td>[fruit]</td>

and then do a replace before printing it out:

$fruit = 'apple';
$text  = str_replace('[fruit]',$fruit,$text);

Of course, for the real life usage there will be more complex solution.

So, you will do yourself enormous favor, if you post here your real task with real data example, not oversimplified and useless abstract question.

0

精彩评论

暂无评论...
验证码 换一张
取 消