This question already has an answer here:
I have a class
$page=new page;
with function,
header() {
echo 'blah blah';
}
now if I concat two strings together like,
$string= "";
$string .= " 1234";
$string .= $page->header();
echo $string;
results in,
blah blah 1234
how can I get it in the correct order?
</div>
You're just echoing the string.
your header()
function should return a string:
function header() {
return 'blah blah';
}
The echo
statement in the function is executed immediately upon the calling of that function. That's why you see it first. If you want to append it to as string you need to return that value:
public function header() {
return 'blah blah';
}
You're not returning a value for the string concatenation in your function, you're simply echoing "blah blah". Your code is equivalent to:
$string = "";
$string .= "1234";
echo "blah blah";
$string .= "";
echo $string;
You need to return a string from your function to use in concatenation like:
function header() { return "blah blah"; }