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?

Link to comment
Share on other sites

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 ...  :-\

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.