1

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

1
  • dtlan does not return a string. Commented Sep 22, 2012 at 10:11

2 Answers 2

2

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.

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

1 Comment

ol how much you help me :). now i know whats meens return, now i fix my code and and and thanks so much. you made my day ^^
0

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

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.