Asked  7 Months ago    Answers:  5   Viewed   38 times

I have a simple PHP function that outputs HTML.

<?php
function get_header() {
?>
<div id="header">
  <div class="page-width">
  <!-- And a lot more HTML after this line. -->
<?php
}
?>

So, when I call get_header(), the function outputs the HTML.

What is the simplest option to tweak this function to return the HTML as a string? Do I need to create a wrapper around this function? In other words, I'd like to be able to do e.g. var html_string = get_header_wrapper(), where html_string contains all the HTML above.

One thing I could think of is to duplicate the function and make it return a string. However, that would be so inefficient because it introduces a lot of code duplicate.

<?php
function get_header_wrapper() {
  var ret = <<<EOD
  <div id="header">
    <div class="page-width">
    <!-- And a lot more HTML after this line. -->
  ...
  EOD;

  return ret;
}
?>

 Answers

96

You can make use of output bufferingDocs to get the output of that function:

ob_start();
get_header();
$html = ob_get_clean();

If you need that more than once, you can wrap it into a function of it's own:

/**
 * call a function and return it's output as string.
 * 
 * @param callback $function
 * @param array $arguments (optional)
 * @param var $return (optional) the return value of the callback
 * @return string function output
 */
function ob_get_call($function, array $arguments = array(), &$return = NULL)
{
    ob_start();
    $return = call_user_func_array($function, $arguments);
    $buffer = ob_get_clean();
    return $buffer;
}

Usage:

$html = ob_get_call('get_header');

As the answer is that popular today, here is another function to get the output of an include:

/**
 * include a file and return it's output as string.
 * 
 * @param string $file
 * @param array $variables (optional) keys as variable names and values as variable values
 * @param var $includeReturn (optional) the return value of the include
 * @return string function output
 */
function ob_get_include($file, array $variables = array(), &$includeReturn = NULL)
{
    $includeFilename = $file;
    unset($file);
    extract($variables);
    unset($variables);
    ob_start();
    $includeReturn = include($includeFilename);
    return ob_get_clean();
}

Usage:

include.php:

<div class="greeting">
    Hello <em><?php echo htmlspecialchars($name); ?></em>!
</div>

Using:

$variables = array(
    'name' => 'Marianne',
);
$html = ob_get_include('include.php', $vars);

Related:

  • Answer to *Load result of php code instead of the code as a string
  • Answer to Is include()/require() with “side effects” a bad practice?
Wednesday, March 31, 2021
 
PedroKTFC
answered 7 Months ago
90
$str    = 'bar';
$str[1] = 'A';
echo $str; // prints bAr

or you could use the library function substr_replace as:

$str = substr_replace($str,$char,$pos,1);
Wednesday, March 31, 2021
 
toesslab
answered 7 Months ago
15
function round_up($value, $precision=0) {
    $power = pow(10,$precision);
    return ceil($value*$power)/$power;
}
function round_down($value, $precision=0) {
    $power = pow(10,$precision);
    return floor($value*$power)/$power;
}
Saturday, May 29, 2021
 
RenegadeAndy
answered 5 Months ago
66

You should simply store the return value in a variable:

$deliveryPrice = getDeliveryPrice(12);
echo $deliveryPrice; // will print 20

The $deliveryPrice variable above is a different variable than the $deliveryPrice inside the function. The latter is not visible outside the function because of variable scope.

Thursday, August 5, 2021
 
dimitarvp
answered 3 Months ago
22

add these code into the top of your script

@set_magic_quotes_runtime(false);
ini_set('magic_quotes_runtime', 0);
Wednesday, August 18, 2021
 
Warrior
answered 2 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :