为什么我的echo在头函数头之前不起作用(location:home.php);

why my echo doesn't work here in this code?It goes directly to the function header().

if($result){
    echo("<script> alert('User Successfull Added')</script>");
    header('location:home.php');
}

Actually thy echo is working. but we cant see the message because at the same time the page redirects to the home.php page.

If you want to alert to display the alert message use jquery post. or php session variable

Sorry, I can't add comment here. But I think what you want to do needs Java and replace alert() with confirm()

var r=confirm("User Successfull Added")
  if (r==true) // if user click OK
    {
      window.location.assign("home.php")
    }
      else // user click Cancel
    {

    }

In order to make your code example work, you need to put it inside an if/else condition like so, if you want it to work, but since I only see what you posted as code, am putting this in as an example:

<?php

if($result){
    echo("<script>alert('User Successfully Added')</script>");

    echo("<script>window.location = 'home.php';</script>");

}

else {

    header('Location: elsewhere.php');

}