0

In multiple place on a script I have ...

if (isset($_GET['event-id']) { ... } 

Would it slow things down to call the key twice? Should I just define a new var like ...

$event_id = $_GET['event-id']; if ($event_id) { ... } 

4 Answers 4

1

You should always go with second approach

$event_id = $_GET['event-id']; if ($event_id) { ... } 

It will not slow things but suppose in the future 'event-id' changes then you will have to change it every where which will be a heavy task to do.

Sign up to request clarification or add additional context in comments.

Comments

1

This also raise an E_NOTICE if $_GET['event-id'] is not set:

$event_id = $_GET['event-id']; 

I prefer

$event_id = isset($_GET['event-id']) ? $_GET['event-id'] : false; 

especially when I am going to use $event_id multiple times. Then, I can write:

if($event_id){ //do your code } 

or in cases that an empty $event_id is possible:

if($event_id !== false){ //do your code } 

Comments

0

The best would be to assign this onto a var and validate it before all further processing, to get sure this var contains no prohibited content.

I don't think it would slow down as it's only a Array call, but it is a security gap.

Edit:

Also if it isn't set you could define a default value.

Comments

0

If there is really a need to check $_GET['event-id'] at many places in your code, you can set it once using Ternary Operator

$event_id = isset($_GET['event-id']) ? $_GET['event-id'] : '' ; 

Then you can use it like if ($event_id) or if(empty($event_id)) or if(!empty($event_id)) depending upon your need of condition.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.