Possible Duplicate:
How to clear previously echoed items in PHP
I have this code:
<?php
$var = true;
echo 'testing';
if ($var)
{
echo 'Hey'; //The only thing that should appear.
}
?>
How would I do to only make "Hey" appear on the site?
Possible Duplicate:
How to clear previously echoed items in PHP
I have this code:
<?php
$var = true;
echo 'testing';
if ($var)
{
echo 'Hey'; //The only thing that should appear.
}
?>
How would I do to only make "Hey" appear on the site?
Without being too complicated... You can just assign your output to a variable and overwrite it.
$var = true;
$output = 'testing';
if($var)
{
$output = 'Hey';
}
echo $output;
Alternatively, (the more complex route), you can use buffers. Research ob_start
to get started learning about that.
You could use output buffering:
<?php
ob_start();
$var = true;
echo "testing";
if($var) {
ob_clean();
echo "Hey";
}
You can't, as it has been printed already.
Here is an alternative solution
<?php
$var = true;
if($var)
echo "Hey";
else
echo "testing"
?>