0

I am parsing JSON using the following example php syntax

$carModel = strip_tags($_REQUEST['car']['model']);

The only problem is that some times the "model" array is missing from the provided JSON. When this is the case my php script shuts down when it reaches that line. Can any one recommend a way to check for the model array before parsing so that my php scrip will still run if "model" isn't present.

2 Answers 2

2

Just check to see if it's there. If not assign a default value to it:

$carModel = (isset($_REQUEST['car']['model'])) ? strip_tags($_REQUEST['car']['model']) : '';
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. Is it good practice to add this to every line when parsing JSON?
It's good practice for any input that you don't have full control over
2

I'm not sure how this is related to json, but if you want to check if a variable exists before you use it, you can do:

if (isset($_REQUEST['car']['model']))
{
  $carModel = strip_tags($_REQUEST['car']['model']);
}

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.