2

I'm using a function inside a php script to return a text field containing a value. This value is retrieved from a variable. How can I make this possible?

This is the code that I tried to use but it didn't work.

function display_name()
{   
    return '<input type="text" maxlength="50" readonly="readonly" value=" '. $current_user->display_name .'" name="CreatedBy" style="width:100%">';
}
1
  • can you show me where you want to display or echo this return value? Commented Feb 8, 2012 at 8:11

2 Answers 2

5

By handing the function the variable as a parameter.

function display_name($current_user)
{   
    return '<input type="text" maxlength="50" readonly="readonly" value=" '. $current_user->display_name .'" name="CreatedBy" style="width:100%">';
}

then calling it like

display_name($current_user);
Sign up to request clarification or add additional context in comments.

Comments

1

I see absolutely no point in having such a function.
It will make your code bloated and hard to maintain.

Why not just have this piece of HTML just along other HTML codes in the template?

<input type="text" maxlength="50" readonly="readonly" value="<?=$current_user->display_name?>" name="CreatedBy" style="width:100%">

or at least make it more generalized, to make it call like this

html_input("text",'CreatedBy',$current_user->display_name,
           array("maxlength=>"50",readonly=>"readonly",style=>"width:100%"));

to use such a function for the every input on the site.

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.