php - 如何将函数()中的变量访问到另一个php文件
问题描述
我将 login_auth.php 文件包含到 form_login.php 中,现在,我想将函数 adminAutentication 中的两个变量访问到 form_login.php 我尝试过但没有用。
login_auth.php
$uname_err= '';
$psswd_err = '';
function adminAuthentication() {
$psswd_err = "The password you entered was not valid!";
$uname_err = "No account found with that username!";
}
form_login.php
<?php include 'login_auth.php'; ?>
<form method="post">
<div class="form-group">
<span class="text-danger"><?php echo $uname_err ?></span> // problem here cannot access the variable
</div>
<div class="form-group">
<span class="text-danger"><?php echo $psswd_err; ?></span> // problem here cannot access the variable
</div>
<button type="submit" name="submit">Login</button>
</form>
解决方案
在 adminAuthentication() 函数中,您必须声明全局变量:
function adminAuthentication() {
global $psswd_err, $uname_err ;
$psswd_err = "The password you entered was not valid!";
$uname_err = "No account found with that username!";
}
推荐阅读
- python-2.7 - 在 Python 多处理中,当子进程干净地存在时,stdout 会发生什么?
- c# - 如何在不使用 Min() Max() 的情况下使用 LINQ 从列表中获取最大和最小的数字?
- java - 如何锁定 couchbase 数据库中的文档并处理 TemporaryLockFailureException
- c++ - 如何修复 LU 分解?
- sqlite - 相对于家庭或环境变量的 Sqlalchemy sqlite url
- rstudio - Rstudio Rmarkdown编织到多个pdf?
- postgresql - 如何在不先连接所有内容的情况下增量散列postgresql中的聚合结果
- http4k - 是否可以使用 http4k 流式传输长响应?
- c# - 如何将两个搜索栏与不显眼的验证合二为一
- heroku - Heroku 应用程序之间的服务器客户端通信