Reputation: 728
I want to attach a php function for example: dtlan(text)
< into <li>
tag but i have one problem. i will show now my code and output:
echo "<a href=/".$array['id']."><li id='menu_".$array['id']."'>"
.dtlan($array['name'])."</li></a>";
output must be like this:
<a href='/2'> <li id='2'>wazaap </li></a>
actual output is:
wazaaap
<a href='/2'><li id='2'></li></a>
thats meens function runs first. and now my question how i can insert function into tags. thanks all and sorry for my ugly english :D
Upvotes: 1
Views: 1350
Reputation: 24740
PHP creates a new string with your HTML. Parsing proceeds from left to right, and PHP resolves all the variables. It then enters the function dtlan
(pushes it on the stack) and dtlan
appears to make a call to echo and does not return anything. The empty return is cast into an empty-string.
Then the rest of your markup is concatenated and is passed to the function echo
(which has a special syntax which makes brackets non-obligatory).
The resulting order of the echo invocations is:
echo("wazaaap");
echo("<a href='/2'><li id='2'></li></a>");
Upvotes: 0
Reputation: 116110
I think (or I am quite sure) that the function doesn't return the value, but echoes it instead.
Because the function is called during the process of building the string to echo. That means, the function has already echoed its value by the time the html is echoed. That's why the function result is displayed before the html.
Either make the function return the value instead of echo it:
function dtlan($x)
{
// echo $x; <- Not like this
return $x; // But like this.
}
Or do like this: First echo the first part. Then let the function echo its part. Then echo the closing part.
echo "<a href=/".$array['id']."><li id='menu_".$array['id']."'>";
dtlan($array['name']);
echo "</li></a>";
If you have the choice, I would choose the first.
Upvotes: 2