4

is it possible to use values that are obtained within a loop/function out side the loop/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;
    }
}

Im calling it to a php file which has html tags. Is it possible to get the values that are returned by the function into the rows that are marked by XXXX to display ?

<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 is there a way to display without including the html tags within the function.

Any help will be appreciated.

Thanks

6 Answers 6

2

Well, you cannot do looping without PHP, so your HTML has to be inside a loop. But you can return a simplified array from your function, if that 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 builds an array of the results to be returned back to the caller. Then you can just loop through that 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 having HTML inside your function, which is always a good thing to do – keep your logic separate from the presentation.

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

1 Comment

Why the downvote? This is exactly what the OP asked for, with an clear example.
2

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

Comments

2

Yes, you can get the result of a 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 as it will 'exit' the function once you return. If you need more data than just a single variable you can return an array and pick that up on the other side.

Comments

0

There are three ways:

1., Either you save result of each loop turnaround into an array and then loop through array

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

2., or you echo each row through the loop (in that off function)

3., or you start a buffer with ob_start and then save it to some variable ($result = ob_get_contents())

Either way, you'll need more loops and some echos.

Comments

0

You need to return the value out of the function:

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

Any time I call foo(), I'll be able to catch it's return value:

$items = foo(); // $items is now array("bar");

foreach($items as $item) {
  print "<td>" . $item . "</td>"; // <td>bar</td>
}

Comments

-1

Not without making each of the function variables global

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

Note: for the above to work, short tags must be enabled, otherwise you have to do <?php echo $var1 ?>

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.