簡體   English   中英

如何將函數()中的變量訪問到另一個 php 文件

[英]How to access variable inside a function() to another php file

I included the login_auth.php file to the form_login.php and now, I want to access two variable inside the function adminAutentication to the form_login.php I tried it but did not works.

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 中,您必須聲明全局變量:

function adminAuthentication() {
   global $psswd_err, $uname_err ;
   $psswd_err = "The password you entered was not valid!";
   $uname_err = "No account found with that username!";
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM