Jump to content

help.function doesnt work


kenyi

Recommended Posts

[code]function f_ret_error($element) {

if ($error[$element] == 1) { echo $return_error[$element];}
}

function f_err_mem($element) {

if ($error[$element] == 0){
echo $profile[$element];}
else {
echo $_POST[$element];}

}

<input name='phone' type='text' maxlength="15" value="<? f_err_mem("phone"); ?>">
<? f_ret_error('phone');?>
[/code]

the code above works when they are not declared as function.
Link to comment
https://forums.phpfreaks.com/topic/33867-helpfunction-doesnt-work/
Share on other sites

You can't use a variable in a function if it is declared outside of it.
If you wish to use one, you have to set it as global, like this:

[code]function f_ret_error($element) {
global $error, $return_error; // This will tell PHP that $return_error declared outside the function should be used inside too

if ($error[$element] == 1) { echo $return_error[$element];}
}

function f_err_mem($element) {
global $error, $profile;

if ($error[$element] == 0){
echo $profile[$element];}
else {
echo $_POST[$element];}

}[/code]

Observe that $_POST ($_GET, $_FILES etc) are already global variables, so called Superglobals.

Good luck with further coding! :)

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.