Php set variable to function return
I have a PHP script that returns a random password. How do I echo out the password that is generated? Show
asked Feb 20, 2011 at 23:28
user547794user547794 13.8k35 gold badges100 silver badges150 bronze badges 1 As prodigitalson mentioned, you can directly
answered Feb 20, 2011 at 23:31
err.. change
to
Or.. better yet:
answered Feb 20, 2011 at 23:30
ariefbayuariefbayu 21.4k12 gold badges70 silver badges92 bronze badges 3 Values are returned by using the optional return statement. Any type may be returned, including arrays and objects. This causes the function to end its execution immediately and pass control back to the line from which it was called. See return for more information.
Use of returnExample #1 Use of return
A function can not return multiple values, but similar results can be obtained by returning an array. Example #2 Returning an array to get multiple values
To return a reference from a function, use the reference operator & in both the function declaration and when assigning the returned value to a variable: Example #3 Returning a reference from a function
For more information on references, please check out References Explained. ryan dot jentzsch at gmail dot com ¶ 5 years ago
rstaveley at seseit dot com ¶ 12 years ago
obj_inc_x($obj) { bgalloway at citycarshare dot org ¶ 14 years ago
nick at itomic.com ¶ 19 years ago
testRet() === NULL) k-gun !! mail ¶ 5 years ago
ret_nullable() ?int { Berniev ¶ 4 years ago
ryan dot jentzsch at gmail dot com ¶ 7 years ago
add2ints(int $x, int $y):int zored dot box at gmail dot com ¶ 4 years ago
B extends A { Vidmantas Maskoliunas ¶ 6 years ago
php(@)genjo(DOT)fr ¶ 2 years ago
getChilds(): Child[] How can a function return a value in PHP?A function can return a value using the return statement in conjunction with a value or object. return stops the execution of the function and sends the value back to the calling code. You can return more than one value from a function using return array(1,2,3,4).
How can we store return value in PHP variable?You can't get a return value from that, but you can capture the contents by using the ob_functions: ob_start(); doStuff(); $output = ob_get_contents(); ob_end_clean(); Now $output contains the output of that function, rather than it having been printed.
Can you return a function in PHP?Definition and Usage
The return keyword ends a function and, optionally, uses the result of an expression as the return value of the function. If return is used outside of a function, it stops PHP code in the file from running.
How can I pass variable from one function to another in PHP?You need to instantiate (create) $newVar outside of the function first. Then it will be view-able by your other function. You see, scope determines what objects can be seen other objects. If you create a variable within a function, it will only be usable from within that function.
|