简体   繁体   English

在PHP中的函数之间传递变量

[英]Passing variables between functions in PHP

I'm slowly converting my work mode to functions and as well, OOP. 我正在慢慢地将工作模式转换为函数,也就是OOP。

I have a file, functions.php which I call at the top of a log in page. 我有一个文件functions.php ,该文件在登录页面顶部调用。

When the form on the page is submitted, I use a function within the functions file to check details entered are correct. 提交页面上的表单后,我使用功能文件中的一个功能来检查输入的详细信息是否正确。 If not I do eg 如果没有,我会例如

if ($how_many<1){

    $error .= "<p>You typed the wrong email address and/or password.</p>";
    return $error;

}

I then have another function named anyerrors in the same functions file. 然后,我在同一函数文件中有了另一个名为anyerrors的函数。 This is: 这是:

function anyerrors($error){
    echo $error;
}

UPDATE: Then in the file I call anyerrors($error); 更新:然后在文件中,我称anyerrors($error); where I want the error to be shown. 我希望显示错误的地方。

But nothing is being shown? 但是什么都没有显示? I'm guessing it's because I'm creating the variable in one function and it's not getting to the other function. 我猜是因为我正在一个函数中创建变量,而没有到达另一个函数。

Am I way off in my logic? 我逻辑上有路吗?

For the record I'm an old school coder I guess and taking time to change my work flow radically one step at a time. 作为记录,我是一名老派编码员,我想并花时间一次从根本上改变我的工作流程。

Your $error variable scope is limited to place where you declare.it can be in the file at global level or inside a function. 您的$ error变量作用域仅限于声明的位置。它可以在全局文件中或函数内部。 function variables are primitive data types the life span is only till function exists. 函数变量是原始数据类型,其寿命只有在函数存在之前。 So if you declare $error in a function it will be a new variable. 因此,如果在函数中声明$ error,它将是一个新变量。 If you want to use a global variable you will have to call it as global. 如果要使用全局变量,则必须将其称为全局变量。

I would personally avoid using global but it will work.. 我个人将避免使用global,但是它将起作用。

read about globals 阅读有关全局变量的信息

$error = "";

if ($how_many<1){

    $error .= "<p>You typed the wrong email address and/or password.</p>";
    return $error;

}

function anyerrors($error){
    global $error;
    echo $error;
}

I would suggest create class which will make things easier. 我建议创建类,这将使事情变得更容易。

it's a wrong way to use it switch the echo in the function with return and the echo to the function like this : 这是错误的使用方式,将回声切换到函数中,并将回声切换到函数,如下所示:

function anyerrors($error){
    return $error;
}



if ($how_many<1){

    $error .= "<p>You typed the wrong email address and/or password.</p>";
    echo anyerrors($error);

}

or to keep it simple you can just print it after creating the error 或为了简单起见,您可以在产生错误后将其打印出来

if ($how_many<1){

    $error .= "<p>You typed the wrong email address and/or password.</p>";
    echo $error;

}

the answer above this just to give you right way to use function that return a value I hope my answer can help you and if you have another question about my answer feel free to ask in the comment :) 上面的答案只是为了给您正确的方法使用返回值的函数,我希望我的答案可以为您提供帮助,如果您对我的答案还有其他疑问,请随时在评论中提问:)

Looks like lackluster code it should be something like this 看起来代码乏味,应该是这样的

function checkForErrors($type)
    if ($type =='your type you want')
    {
        if ($how_many<1){

            $error .= "<p>You typed the wrong email address and/or password.</p>";
            return $error;
        }

    }
}
function anyerrors(){
    $errors = checkForErrors('your type you want');
    if($errors!="") {
          echo $errors;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM