Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

PHP

How Can I Concatenate My Code?

Hey guys,

I'm wondering how I can concatenate the two functions in my code (see code below) as I feel that I'm breaking the DRY principal.

<?php

  function get_design_team_table_html($employee_id, $employee) {

    $output = "";
    $output = $output . "<tr>";
    $output = $output . '<td class="text-muted">';
    $output = $output . $des_team["name"];
    $output = $output . "</td>";
    $output = $output . '<td class="text-right text-muted"><span class="text-muted">$</span>';
    $output = $output . $des_team["age"];
    $output = $output . "</td>";
    $output = $output . "</tr>";

    return $output;

  }

  $designTeam = array();

  $des_team[101] = array( "name" => "Steve", "age" => 25 );
  $des_team[102] = array( "name" => "John", "age" => 38 );
  $des_team[103] = array( "name" => "Craig", "age" => 29 );


# Here's what I want concatenated with the above function.
  function get_get_development_team_table_html($dev_team_id, $dev_team) {

    $output = "";
    $output = $output . "<tr>";
    $output = $output . '<td class="text-muted">';
    $output = $output . $dev_team["name"];
    $output = $output . "</td>";
    $output = $output . '<td class="text-right text-muted"><span class="text-muted">$</span>';
    $output = $output . $dev_team["age"];
    $output = $output . "</td>";
    $output = $output . "</tr>";

    return $output;

  }

  $dev_team = array();

  $dev_team[101] = array( "name" => "Jason", "age" => 30 );
  $dev_team[102] = array( "name" => "Pete", "age" => 46 );
  $dev_team[103] = array( "name" => "Sam", "age" => 34 );

?>

Any advice on how I can concatenate these functions would be greatly appreciated!

Thanks in advance,

Stu : )

1 Answer

Are these not exactly the same function just with different names? It looks like you're already half way there with the arguments of your first function!

<?php

printEmployeeTableRow($employeeID, $employee)
{
    // do the code
}

Then you can call it from wherever!

Also this:

<?php

$designTeam

is MUCH better than this

<?php

$des_team

It's a few more characters but 1 million times more readable. Also watch out for starting a function name with 'get'. More often than not (especially in wordpress), functions that begin with 'get' return an array or object and don't echo anything to the screen. Just a little thing :-)