1

I have a device that sends data to url, depending on situations 3 sometimes 4 parameters are received, when i try to get data for 4th parameter using

$a = $_GET["xyz"];

in the case when "xyz" does not exist the function

_GET['xyz'];

returns undefined. i tried using try catch and initialized $a to a empty string in catch part but it did not work either. How can that undefined exception be handled?

2

4 Answers 4

2

isset

  if(isset($_GET["xyz"])) {
        $a = $_GET["xyz"];
  }
Sign up to request clarification or add additional context in comments.

Comments

1

If you want to check if a parameter is passed in through the URL, you can use isset()

http://php.net/manual/en/function.isset.php

This will check to see if the parameter exists

// This will evaluate to TRUE so the text will be printed.
if (isset($var)) {
    echo "This var is set so I will print.";
}

Comments

1

You can use ternary operator

$a=(isset($_GET["xyz"]))?$_GET["xyz"]:'';   

Comments

1

To handle such errors, you should use php's library function,

set_error_handler

On having error such as undefined index, undefined variable etc.. will be handle by handleError function.

public function handleError($code,$message,$file,$line){
    // handling 
}

All the Best !!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.