msgno返回0,而它应该加起来

I might be doing something entirely wrong but I am still learning. For some reason, imap_msgno returns 0 in my code which I will add below:

for ($i = 1; $i <= $message_count; ++$i)
   {
       $header = imap_header($imap, $i);
       $body = imap_fetchbody($imap, $i,2);
       $prettydate = date("jS F Y", $header->udate);
       $nom = imap_msgno($imap, $i);

       if (isset($header->from[0]->personal)) {
           $personal = $header->from[0]->personal;
       } else {
           $personal = $header->from[0]->mailbox;
       }

$subject=$header->Subject;

$email = "$personal <{$header->from[0]->mailbox}@{$header->from[0]->host}>";

        echo $nom;
        echo '<br><br>';
        echo "On $prettydate, $email said \"$body\".
";
        echo '<br><br>';
   }

So the variable this is about is $nom, as far as I see it shouldn't return 0 but somehow it does. Can anyone help me out?