0

I want to echo out the result of a function within the initial echo.

echo "<div class='booking-item-rating'>" . stars($hotelSummary[$x]['hotelRating']) . "</div>";

The function:

function stars($stars){
for($i=1;$i<=$stars;$i++) {
    echo "<i class='fa fa-star'></i>";
}
if (strpos($stars,'.')) {
    echo "<i class='fa fa-star-half-empty'></i>";
    $i++;
}
while ($i<=5) {
    echo "<i class='fa fa-star-o'></i>";
    $i++;
}
}

The problem is that the result of the function is printed outside the initial echo field, outside of the div element. How do I get the result of the function to print out within the first echo ?

2
  • you want to use return inside the function not echo Commented Feb 26, 2015 at 23:33
  • 1
    in any context it is almost always a bad idea to have echo inside a function Commented Feb 26, 2015 at 23:35

1 Answer 1

2

return that content instead of echoing it:

function stars($stars){
    $string = '';
    for($i=1;$i<=$stars;$i++) {
        $string .= "<i class='fa fa-star'></i>";
    }
    if (strpos($stars,'.')) {
        $string .= "<i class='fa fa-star-half-empty'></i>";
        $i++;
    }
    while ($i<=5) {
        $string .= "<i class='fa fa-star-o'></i>";
        $i++;
    }
    return $string;
}
Sign up to request clarification or add additional context in comments.

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.