I'm going to elaborate a bit on your situation. Let's look at your code:
class squad {
function getsquad($url){
$html = file_get_html($url);
foreach ($html->find('td[align=left]') as $element) {
if ($element->children(0)) { // work only when children exists
return $element->children(0)->innertext.'<br>';
}
}
}
getsquad('$site');
}
So first of all, you can't declare any logic inside classes unless it it inside functions. All you can do outside functions is make declarations for properties that class will use. Let's move past this and assume you've changed it to something like this:
$squad = new squad();
$get_squad = $squad->getsquad();
Now, the logic pattern you are using in your getsquad() function is baaaad. Returning during a loop? Bad bad Nic. From looking at this function, I can assume what you're doing is looping through an HTML file until you find a child, then you return it (end of function). That's cool, but that's not how you should do it - anyone will tell you that you should always return at the end of a function.
function getsquad($url){
$html = file_get_html($url);
foreach ($html->find('td[align=left]') as $element) {
if ($element->children(0)) { // work only when children exists
return $element->children(0)->innertext.'<br>';
}
}
}
So here's what you should do instead:
function getsquad($url){
$html = file_get_html($url);
$found_child = null;
foreach ($html->find('td[align=left]') as $element) {
if ($element->children(0)) { // work only when children exists
$found_child = $element->children(0)->innertext . '<br>';
break; // break ends your foreach loop
}
}
return $found_child;
}
Also, as far as good coding practice is concerned, you shouldn't be formatting any of the data coming from getsquad() - I know this might seem overkill for what you're doing, but this is how you should do it:
function getsquad($url) {
$html = file_get_html($url);
$found_child = null;
foreach ($html->find('td[align=left]') as $element) {
if ($element->children(0)) { // work only when children exists
$found_child = $element->children(0)->innertext;
break; // break ends your foreach loop
}
}
return $this->format_squad($found_child);
}
private function format_squad($found_child) {
return $found_child . '<br>';
}
Doing this will keep your data and your formatting completely seperate. It's easy to maintain, read and write.
So your final usage (note that you need to assign a variable to store the return of your function [missing from your question and answers]):
$squad = new squad();
// store it in a variable since it's returning data not outputting
$squad_info = $squad->getsquad($url);