简体   繁体   中英

Function echo location php

i'm pretty new to php and I'm trying to figure out how to display errors to the page.

I have this function made.

 function error($errorreason){
  echo("<div id='error1' class='alert text-bold alert-danger'>" . $errorreason . "<span id='error1' class='clickable pull-right'><i class='ion-close-round'></i></span></div> ");
 }

And I'll call it when-ever I need to display an error with error("blah blah"); but the issue I'm having is that it echos the error at the line where the function is called at, which is at the wrong location as it displays the error inside an element and not on the top of the page where the function is located. I want it to display the error at the same location as the function itself is at. What should I do? Should I use a different method?

Your function do a RETURN (no a echo) after this you affect $errorreason add the param error to your function and call by a ECHO error("error blabla");

    function error($errorreason){
        return ("<div id='error1' class='alert text-bold alert-danger'>" . $errorreason . "<span id='error1' class='clickable pull-right'><i class='ion-close-round'></i></span></div> ");
    }

    $errorreason = "What's the plan?";
    echo error($errorreason);

When you call a function, you're also executing it. Simply call the function when you want to execute it (and subsequently run the echo ). If you want the output at the top of the page, simply call the function directly after the function declaration. If you're only running the function once, this can be simplified by simply taking the echo statement out of the function entirely.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM