Array error array_push () expects parameter 1 to be array

< H2 > an error occurred on line 20 < / H2 >

here is the complete code for this php file:

<?php
header("content-type:text/html;charset=utf-8");
date_default_timezone_set("PRC");
$file="123.txt";
$msgs=[];
//
if (file_exists($file)){
    $string=file_get_contents($file);            //
    if (strlen($string)>0){                      //
        $msgs=unserialize($string);              //
    };
};
//
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);/*      
           Warning: array_push() expects parameter 1 to be array, null given in / on line 20
                                    */
    $msgs=serialize($msgs);                      //
    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 href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.1/css/bootstrap.min.css" 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>:</th>
            <th>:</th>
            <th>:</th>
            <th>:</th>
        </tr>
        <tbody>
        <?php $i=1;foreach($msgs as $val):?>
            <tr class="success">
                <td>
                    <?php echo $iPP;?>
                </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="-sharp" method="post">
            <fieldset>
                <legend></legend>
                <label>:</label><input type="text" name="username" placeholder="" required>
                <label>:</label><input type="text" name="title" placeholder="" required>
                <label>:</label><textarea name="content" rows="1"></textarea>
                <input type="submit" name="subpop" value="">
            </fieldset>
        </form>
    </div>
</div>
</body>
</html>
Php
Mar.19,2021

$msg No array error message wrote array_push the first parameter to array you gave null

Menu