Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialStu Cowley
26,287 PointsHow 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
thomascawthorn
22,986 PointsAre 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 :-)