Using a _GET gives me a debug error (over my head)

Just looking for some advice on something that is bugging me. Please bear in mind I am a PHP noob 🙂

I am passing a variable in the url to my WP index page like so:

<a href="https://wordpress.stackexchange.com/questions/23315/<?php bloginfo("url');?>/?do=thing">Thing</a>

I am the catching that variable, and using it to show content like so:

<?php $do_that = $_GET["do"]; if($do_that == 'thing') : ?> etc

Naturally I am getting debug errors when the variable is not passed 🙂

"Undefined index: do"

My question is what am I missing wrong? Obvious I spose?

Also is this bad practice?

1 Answer
1

The error is occurring as the $_GET array doesn’t have the item $_GET[‘do’] in it. Therefore it throws this notice. To check if something exisits in the array try:

if( isset( $_GET['do'] ) )
   $do_that = $_GET['do'];
else
   $do_that="";

or a cleaner method would be to use short hand notation

$do_that = ( isset( $_GET['do'] ) ) ? $_GET['do'] : '';

Leave a Comment