Jump to content

Function on a variable?


11Tami

Recommended Posts

I'm getting hung up on a few things because everytime I turn around the php wants me to use a variable $. I guess this is what I need.

You can retrn value from function and store in $t variable but not direct define.

 

How would I do that?  How would I return value from separate function and then store it in $t variable?

function a(){

echo "this is a";

}

a(); How to get a() to $t?

You must use "return" in your function, as in my earlier post.

 

function a(){
return "this is a";
}
$t = a();
echo $t;

 

Yes, if you want to define a variable using the function, but it's not a must to use "return" in the function. I have many functions that do not "return" a value, but are useful nonetheless. A simple example:

 

function showLoginForm(){
echo '<form action="'.$_SERVER['PHP_SELF'].'" method="POST">
Username: <input type="text" name="username" /><br>
Password: <input type="password" name="password" /><br>
<input type="submit" name="login" value="Log In!">
</form>';
}

// Show the login form
showLoginForm();

 

... but again, yes you can define a variable by using a function return value. Take the above example, modified to return a value:

 

function showLoginForm(){
$value = '<form action="'.$_SERVER['PHP_SELF'].'" method="POST">'
            .'Username: <input type="text" name="username" /><br>'
            .'Password: <input type="password" name="password" /><br>'
            .'<input type="submit" name="login" value="Log In!">'
            .'</form>';
return $value;
}

$t = showLoginForm();

// Show the login form
echo $t;

 

... not the most efficient way in this case though ...  :-\

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.