微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在PHP中调用函数

我一直在学习如何创建会话对象的一些课程,这很好,如果我把完整的代码放到PHP文件上,一切都很棒!

我想做的是将它放在另一个模块(PHP文件)中,只需使用一行(或等效的)来执行此操作,例如GetSessiondata();

<?PHP
$sqlCon = new DBConnect("Databaselocation","Database","Usr","Pss");                     
$UserDataSet = $sqlCon->GetUserList("SELECT * FROM Users");

echo "<br /><br />";
echo "<br /><br />";

if ($UserDataSet)          
{               
    echo "<table>" . "<thead>" ;
    echo "<tr><th scope=\"col\">" . 'Usr' . "</th>";
    echo "<th scope=\"col\">" . 'Lvl' . "</th></tr></thead><tbody>";                
    foreach($UserDataSet as $data)
    {                                  
        echo "<td>" .$data->GetUsrName()."</td>" ;
        echo "<td>" .$data->GetUsrLevel()."</td></tr>" ;                                       
    }
    echo "<tfoot><tr><th scope=\"row\" colspan=\"2\">" . 'Total Users = 2' . "</th></tr></tfoot>";
    echo "</tbody>" . "</table>" ;  
}
else
    echo "nothing Found in DB!";
?>

解决方法:

您需要“需要”您要使用它的文件.

这是一个例子

Working with classes:

Whatever.PHP

class Whatever {
  public function __construct() {
    // Ever when the code is instantiated, this will be called too!
  }
  public function myMethod() {
    echo 'hello';
  }
}

的index.PHP

require_once('./Whatever.PHP');
$whatever = new Whatever();
$whatever->myMethod();

Without classes:

的functions.PHP

function whatever(){ echo 'hello'; }

index.PHP文件

require_once('./functions.PHP');
whatever();

阅读更多:

要求:http://php.net/manual/es/function.require.php

Require_once:http://php.net/manual/es/function.require-once.php

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐