Page 1 of 1
simple append string not working
Posted: Fri Aug 13, 2004 10:15 pm
by shayta
Code: Select all
<?php
$temp = "test";
$nottemp = "nottemp";
function featureAvail($x)
{
if($x == "test") {
echo "test yes";
} else {
echo "test no";
}
}
$stuff = "This here is a test: " . featureAvail($temp) . ". That concludes the test.";
echo $stuff;
?>
How come this outputs "test yesThis here is a test: . That concludes the test."
and not
"This here is a test: test yes. That concludes the test."
?? And how do I get it to ouput the second result?
Posted: Fri Aug 13, 2004 10:31 pm
by d3ad1ysp0rk
You run the function before echoing it, not WHILE echoing it, meaning you would need to return something to make it work how you want. Or split the echo into 3 parts.
Code: Select all
<?php
$temp = "test";
$nottemp = "nottemp";
function featureAvail($x)
{
if($x == "test") {
return "test yes";
} else {
return "test no";
}
}
$value = featureAvail($temp);
$stuff = "This here is a test: " . $value . ". That concludes the test.";
echo $stuff;
?>
OR
Code: Select all
<?php
$temp = "test";
$nottemp = "nottemp";
function featureAvail($x)
{
if($x == "test") {
echo "test yes";
} else {
echo "test no";
}
}
echo "This here is a test: ";
featureAvail($temp);
echo ". That concludes the test.";
?>
Posted: Sat Aug 14, 2004 7:19 am
by m3mn0n
Indeed.
There is a huge different between "return" and "echo" in function variables.
"return" gives you the option to assign the function to a value, such as:
Code: Select all
$test = TestFunc (); // $test is set to: hi
While echo simply disaplays the data once the PHP Parser gets to that function, eg: