simple append string not working

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
shayta
Forum Newbie
Posts: 1
Joined: Fri Aug 13, 2004 10:15 pm

simple append string not working

Post 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?
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post 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.";

?>
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post 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:

Code: Select all

TestFunc (); // will echo: hi
Post Reply