doubleplusgood
doubleplusgood

Reputation: 2556

array_key_exists

I'm implementing some affiliate tracking to my site. The affiliate network has asked that I hide the telephone number on the site.

When an affiliate clicks on a link to my site, any of the site URLs will be appended wth something like /?source=affiliate&siteid=XXXX for example; mydomain.com/?source=affiliate&siteid=XXXX

I've been trying to do this to hide the telephone number;

<?php
 if (!array_key_exists('affiliate', $_GET)){
  //Show telephone number
  echo "<li>+44 (0)1234 567891</li>";
 }
?>

However, this doesn't seem to be working. Ideally I need to show the number by default, but if the URL contains the affilaite part of the url, then the telephone number should be hidden.

Upvotes: 1

Views: 1562

Answers (3)

iroel
iroel

Reputation: 1798

The conclusion:

if (array_key_exists('source', $_GET) && $_GET['source'] == 'affiliate') {
    //Show telephone number
    echo "<li>+44 (0)1234 567891</li>";
}

Upvotes: 0

Michael Madsen
Michael Madsen

Reputation: 55009

It sounds like you've mixed up keys and values. In a URL, the key is the part to the left of the =, while the value is on the right side.

The condition you want should be if (!array_key_exists('source', $_GET) || $_GET['source'] != 'affiliate'). This checks that the source key is defined, and that it has the value affiliate.

Upvotes: 4

sleepynate
sleepynate

Reputation: 8036

in your $_GET array, source is the key, affiliate is the value. you want:

<?php
 if (!in_array('affiliate', $_GET)){
  //Show telephone number
  echo "<li>+44 (0)1234 567891</li>";
 }
?>

or

<?php
 if (!array_key_exists('source', $_GET)){
  //Show telephone number
  echo "<li>+44 (0)1234 567891</li>";
 }
?>

Upvotes: 4

Related Questions