brain storm
brain storm

Reputation: 31242

How can I exit with an error message in PHP/HTML?

I am checking if a particular value is set using isset. I want to exit the script and display an error message like "Name not found, Enter a valid name". Currently I am checking like this:

if ((! isset($_GET["name"])) or (! is_dir($_GET["name"])) or (empty($_GET["name"]))) {

    Print "Name not found, Enter a valid name"
}

After the print, I want to exit without further execution of code. How can I exit with the my error message and no default error message from PHP?

Upvotes: 3

Views: 4302

Answers (3)

Jordan Schnur
Jordan Schnur

Reputation: 1353

You would use the die or exit methods. This will halt all PHP and HTML from being displayed or executed.

Example:

if ((! isset($_GET["name"])) or (! is_dir($_GET["name"])) or (empty($_GET["name"]))) {

    die("Name not found, Enter a valid name");
}

Upvotes: 1

Felix Lebel
Felix Lebel

Reputation: 563

 die("Name not found, Enter a valid name");

That should help!

Regards, Felix

Upvotes: 1

Zak
Zak

Reputation: 25205

Die or exit: see the php documentation

http://php.net/die

http://php.net/exit

die ("I'm dyin over here");

Upvotes: 3

Related Questions