鍍金池/ 問(wèn)答/PHP/ 數(shù)組報(bào)錯(cuò) array_push() expects parameter 1 t

數(shù)組報(bào)錯(cuò) array_push() expects parameter 1 to be array

圖片描述

在第20行出現(xiàn)了報(bào)錯(cuò)

下面是這個(gè)php文件的完整代碼:

<?php
header('content-type:text/html;charset=utf-8');
date_default_timezone_set('PRC');
$file="123.txt";
$msgs=[];
//檢測(cè)文件是否存以及讀取
if (file_exists($file)){
    $string=file_get_contents($file);            //為真則讀取文件并賦值
    if (strlen($string)>0){                      //判斷獲取的文件是否有內(nèi)容
        $msgs=unserialize($string);              //有則轉(zhuǎn)為數(shù)組并賦值
    };
};
//判斷提交是否正常以及接收
if (isset($_POST['subpop'])){
    $username=($_POST['username']);
    $title=strip_tags($_POST['title']);
    $content=strip_tags($_POST['content']);
    $time=time();
    $data=compact('username','title','content','time');
    array_push($msgs,$data);/*      報(bào)錯(cuò)提示如下
           Warning: array_push() expects parameter 1 to be array, null given in / on line 20
                                    */
    $msgs=serialize($msgs);                      //重新轉(zhuǎn)換并賦值
    if(file_put_contents($file,$msgs)){                 //將$msgs壓入文件末行
        echo "<script>alert('留言成功!');</script>";
    }else {
        echo "<script>alert('留言失敗!');</script>";
    };
};
?>


<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link  rel="stylesheet" crossorigin="anonymous">
    <script type="text/javascript" src="http://www.francescomalagrino.com/BootstrapPageGenerator/3/js/jquery-2.0.0.min.js"></script>
    <script type="text/javascript" src="http://www.francescomalagrino.com/BootstrapPageGenerator/3/js/bootstrap.min.js"></script>
    <title>Document</title>
    <style>
        .Dcenter{
            position: fixed;
            bottom: 0;
        }
    </style>
</head>
<body>
<?php if(is_array($msgs)&&count($msgs)>0):?>

    <table cellspacing="0" cellpadding="0" width="70%" class="table table-sm table-bordered">
        <tr>
            <th>用戶(hù)名:</th>
            <th>標(biāo)題:</th>
            <th>內(nèi)容:</th>
            <th>時(shí)間:</th>
        </tr>
        <tbody>
        <?php $i=1;foreach($msgs as $val):?>
            <tr class="success">
                <td>
                    <?php echo $i++;?>
                </td>
                <td>
                    <?php echo $val['username'];?>
                </td>
                <td>
                    <?php echo $val['title'];?>
                </td>
                <td>
                    <?php echo date("m/d/Y H:i:s",$val['time']);?>
                </td>
                <td>
                    <?php echo $val['content'];?>
                </td>
            </tr>
        <?php endforeach;?>
        </tbody>
    </table>
<?php endif;?>

<div class="container">
    <div class="row Dcenter">
        <form action="#" method="post">
            <fieldset>
                <legend>留言板</legend>
                <label>用戶(hù)名:</label><input type="text" name="username" placeholder="請(qǐng)輸入用戶(hù)名" required>
                <label>標(biāo)題:</label><input type="text" name="title" placeholder="請(qǐng)輸入標(biāo)題" required>
                <label>內(nèi)容:</label><textarea name="content" rows="1"></textarea>
                <input type="submit" name="subpop" value="提交">
            </fieldset>
        </form>
    </div>
</div>
</body>
</html>
回答
編輯回答
近義詞

$msg沒(méi)數(shù)組 報(bào)錯(cuò)信息 寫(xiě)了 array_push第一個(gè)參數(shù)要 數(shù)組 你給了null

2017年8月29日 12:31