ob_get_contents()

General discussions related to php

Moderators: egami, macek, gesf

Post Reply
kepler
New php-forum User
New php-forum User
Posts: 7
Joined: Sun Jul 16, 2017 9:38 pm

Sat May 12, 2018 10:15 am

Hi.

I'm using a small routine in php to get the contents of an echo script. So I start with ob_start(), I echo the calculations and save it with ob_get_contents() to the variable $output. My "problem is that I must multiply $output by 2. But I get zero. If I make a var_dump of $output, I get string(48) "14"

How do I capture the 14 value? Tryed eval without any luck...

Kind regards

Kepler

chorn
php-forum Fan User
php-forum Fan User
Posts: 551
Joined: Fri Apr 01, 2016 2:18 am

Sat May 12, 2018 1:45 pm

your case is not replicable

Code: Select all

echo (string)"14" * 2; // 28

kepler
New php-forum User
New php-forum User
Posts: 7
Joined: Sun Jul 16, 2017 9:38 pm

Sat May 12, 2018 2:29 pm

Hi

Here's an example:

Code: Select all

<?php

?>
<html>
<head>
<script>
function mult(x){
x= 2*x;
document.write( '' + x);
}
</script>
</head>
<body>
<?php
$n = 7;
ob_start(); 
echo '<script type="text/javascript">',
     'mult('.$n.');',
     '</script>'
;
$output = ob_get_contents(); 
ob_end_clean();
$v = $output;
var_dump($v); //displays string(48) "14"

echo $output; //displays 14, correct

echo ($output * 2); //displays 0, wrong
$output has the script, code so it has a length of 48. But evaluates to 14 in a echo. I was wondering if there was a way of getting the result (14) to a variable - not likely....

Kepler

chorn
php-forum Fan User
php-forum Fan User
Posts: 551
Joined: Fri Apr 01, 2016 2:18 am

Sun May 13, 2018 4:22 am

Your assumptions are wrong, look at the sourcecode (ctrl+u), you are looking at messy results, because you are mixing PHP and JS (server- vs clientside scripting). The JS kicks in after all the PHP is already done.

Post Reply