not able call sweetalert from PHP function

I try to integrate sweetalert in my PHP function to show session status,but sweet alert is not working.please help me on this.

function alertmessage()
{
    if(isset($_SESSION['status']) && $_SESSION['status'] !='')
{
  ?>
  <script>
  swal.fire({
    text: "<?=$_SESSION['status']?>",
    icon: "<?=$_SESSION['status_code']?>",
    confirmButtonColor: "#008B8B",
    confirmButtonText: "Ok!",
    showClass: {
    popup: `
      animate__animated
      animate__fadeInUp
      animate__faster
    `
  },
  hideClass: {
    popup: `
      animate__animated
      animate__fadeOutDown
      animate__faster
    `
  }
  });
  </script>
  <?php
        
        unset($_SESSION['status']);
    }
}

i called the function in required page as
<?= alertmessage() ?>

it showing session status message if i didnt add sweetalert.