【PHP】调试
我将把学习内容作为备忘录整理一下 PHP。
关于调试
我认为在开发过程中发生错误时,应该进行调试。
在其中,我将记录调试方法。
输出变量的详细信息
可以在代码中使用它来检查值。
例如,我们来看看下面员工登录的代码。
#staff_login.php
<form method="post"action="staff_login_cheak.php">
スタッフコード<br />
<input type="text" name="code"><br />
パスワード<br />
<input type="password" name="pass"><br />
#staff_mypage.php
$staff_code=$_POST['code'];
$staff_pass=$_POST['pass'];
var_dump($staff_code);
※实际上应该有从数据库中获取相应用户的处理,但这里省略了。
这样编写后,staff_login_cheak.php会显示在员工代码表单中输入的代码编号。
![image.png](https://cdn.silicloud.com/blog-img/blog/img/657d06a037434c4406bdc657/7-0.png)
点击登录
![image.png](https://cdn.silicloud.com/blog-img/blog/img/657d06a037434c4406bdc657/9-0.png)
以另一个文件来管理调试。
在使用ajax等进行调试时,使用上述方法无法显示。
由于后端处理,所以我们将采取将调试信息输出到文件中的方法。
我的建议是创建一个名为_debug()的函数,并将要检查的值作为参数传入,然后执行显示的操作。
#_debug()関数を作成
function _debug( $data, $clear_log = false ) {
$uri_debug_file = $_SERVER['DOCUMENT_ROOT'] . '/debug.txt';
if( $clear_log ){
file_put_contents($uri_debug_file, print_r($data, true));
}
file_put_contents($uri_debug_file, print_r($data,true), FILE_APPEND);
}
请在您理解的范围内创建上述函数。
然后,我们将在“点赞”功能的ajax文件中查看点赞时的调试。
if(isset($_POST)){
$current_user = get_user($_SESSION['staff_code']);
$page_id = $_POST['page_id'];
$post_id = $_POST['post_id'];
$profile_user_id = $_POST['page_id'] ?: $current_user['id'];
if(check_favolite_duplicate($current_user['code'],$post_id)){
$action = '解除';
$sql = "DELETE
FROM favorite
WHERE :user_id = user_id AND :post_id = post_id";
}else{
$action = '登録';
$sql = "INSERT INTO favorite(user_id,post_id)
VALUES(:user_id,:post_id)";
}
_debug($action); //ここを追加
如果按下了“赞”按钮,然后在设定的debug.txt文档中找到$action的数值,那就表示调试成功了。
![image.png](https://cdn.silicloud.com/blog-img/blog/img/657d06a037434c4406bdc657/17-1.png)
![image.png](https://cdn.silicloud.com/blog-img/blog/img/657d06a037434c4406bdc657/18-1.png)
解除已被添加。
_debug()函数被定义为_debug(”,true),以使debug.txt变为空的状态。