1

I have a function which provides a HTML for me. Something like this:

function user_profile ($name, $age, $location){
    return "<div class='myclass' style='color:red'>
                <span class='title'>user's profile</span>
                <ul>
                    <li>Name: $name</li>
                    <li>Age: $age</li>
                    <li>location: $location</li>
                </ul>
            </div>";
} 

echo user_profile ($name, $age, $location);

Function above is a simplified of my real function. In reality, that function has 14 arguments and the HTML is much more longer.

Anyway, I want to know can I make it more clean? I mean, can I make an array of all arguments and just pass it (the array)? In that case how can I use it into the function?

Again, in reality my code is much bigger and the above one is just a sample.

4
  • you can pass an array or object as a parameter to your function which would make accessing each of the individual parameters much easier and more maintainable Commented Sep 16, 2016 at 21:55
  • Are all elements will go under the <ul> tag ? Commented Sep 16, 2016 at 21:55
  • @developer Not really, in reality there is some dynamic variables which are wrapped into other tags. Commented Sep 16, 2016 at 21:56
  • You can leave the function as is and use call_user_func_array(). Commented Sep 16, 2016 at 23:08

2 Answers 2

3

The answer is yes, you can pass an array as an argument. In your code it would look something like this:

function user_profile ($array){
    return "<div class='myclass' style='color:red'>
                <span class='title'>user's profile</span>
                <ul>
                    <li>Name: $array[0]</li>
                    <li>Age: $array[1]</li>
                    <li>location: $array[2]</li>
                </ul>
            </div>";
} 

//variables in the following array are defined elsewhere in script - not revelant here

$array = array($name, $age, $location);
echo user_profile($array);

A more attractive way to do this would be to use key-value pairs via an associative array:

function user_profile ($array){
    return "<div class='myclass' style='color:red'>
                <span class='title'>user's profile</span>
                <ul>
                    <li>Name: " . $array['Name'] . "</li>
                    <li>Age: " . $array['Age'] . "</li>
                    <li>location: " . $array['Location'] . "</li>
                </ul>
            </div>";
} 

//variables in the following array are defined elsewhere in script - not revelant here

$array = array('Name' => $name, 'Age' => $age, 'Location' => $location);
echo user_profile($array);

This method, using associative arrays, would allow you to more easily match array keys with the HTML list item content.

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

4 Comments

The OP wanted to make it "more clean". So the question is if these numbered array[0], array[1]... make it cleaner than those original named parameters. I don't think so.
@Al Kepp, OP wanted to know how to pass an array - it's in the title of the post. Clean or not is definitely based on the opinion of each user
@AlKepp Is right .. maybe it would be better if you define some names for each key of that array separately. thank you anyway .. upote
Was just writing an answer using array with keys but you fixed it so upvote and also this should be the answer.
0

In my opinion, Passing array as an argument is the only clean way to do as The One and Only ChemistryBlob said. But can be improved by let it be more dynamic as array can be passed with any number of information.

function user_profile ($array){
    $return ="<div class='myclass' style='color:red'>
                <span class='title'>user's profile</span>
                <ul>".PHP_EOL;
    foreach($array as $key=> $value){
         $return .="<li>".$key.": ".$value."</li>".PHP_EOL;
    }
    $return.="</ul>
            </div>";
    return $return;
} 

$try =['Name' => $name, 
      'Age' => $age, 
      'Location' => $location];

echo user_profile($try);

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.