Using values ​​inside a function outside the side

is it possible to use the values ​​obtained within the cycle / function outside the cycle / function, where it is used or called?

below is my function

function off($limit) { $string=file_get_contents("feed.xml"); $xml=simplexml_load_string($string); foreach ($xml->FOUND->CAT as $place) { $limits = $place->CORDS; $rate=$place->STARS; $phone=$place->PHONE; } } 

I am calling it in a php file with html tags. Is it possible to get the values ​​returned by the function in the strings marked with the symbol XXXX?

 <html> <body> <?php off('57854'); ?> <table width="200" border="1"> <tr> <td>XXXXX</td> <td>XXXXX</td> <td>XXXXX</td> </tr> <tr> <td>XXXXX</td> <td>XXXXX</td> <td>XXXXX</td> </tr> <tr> <td>XXXXX</td> <td>XXXXX</td> <td>XXXXX</td> </tr> </table> </body> </html> 

I would like to know if there is a way to display without including html tags inside this function.

Any help would be appreciated.

thanks

+4
source share
6 answers

Well, you cannot do a loop without PHP, so your HTML should be inside the loop. But you can return a simplified array from your function if this helps:

 function off($limit) { $string = file_get_contents("feed.xml"); $xml = simplexml_load_string($string); $return = array(); foreach ($xml->FOUND->CAT as $place) { $return[] = array( 'limits' => $place->CORDS, 'rate' => $place->STARS, 'phone' => $place->PHONE ) } return $return; } 

The function now creates an array of results returned back to the caller. Then you can just skip this array:

 <html> <body> <?php $arr = off('57854'); ?> <table width="200" border="1"> <?php foreach($arr as $row): ?> <tr> <td><?php echo $row['limits']; ?></td> <td><?php echo $row['rate']; ?></td> <td><?php echo $row['phone']; ?></td> </tr> <?php endforeach; ?> </table> </body> </html> 

This way you can avoid HTML inside your function, which is always good to do - keep your logic separate from the presentation.

+2
source

you put return in your function. then you can use it "outside"

+2
source

Yes, you can get the result of the function and use it outside. Example:

 function addSomething($a, $b) { $sum = $a + $b; return $sum; } $mySum = addSomething(15, 45); print $mySum; // Will show 60 

You can only "return" one variable, since it will "exit" the function after it is returned. If you need more data than just one variable, you can return an array and select it on the other hand.

+2
source

You need to return the value from the function:

 function foo() { return array("bar"); } 

Each time I call foo() , I can catch its return value:

 $items = foo(); // $items is now array("bar"); foreach($items as $item) { print "<td>" . $item . "</td>"; // <td>bar</td> } 
0
source

There are three ways:

1. Or you save the result of each rotation of the loop into an array, and then iterate over the array

 <table> <?php foreach ($array as $a){ echo "<tr><td>$a</td></tr>"; } ?> </table> 

2., or you echo each line through a loop (in this function off )

3., or you run the buffer using ob_start and then save it in some variable ( $result = ob_get_contents() )

In any case, you will need more loops and a few echo s.

0
source

Not forcing each of the functional variables to be global

 function func() { global $var1, $var2, ...; ... } ?> <td><?=$var1?></td> 

Note: to perform the above actions, you must enable short tags, otherwise you must do <?php echo $var1 ?>

-1
source

Source: https://habr.com/ru/post/1300071/


All Articles