Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Jake White
Jake White
41,730 Points

If statement help

Im trying to build a referral system using Stripe. Stripe does not allow customers to have multiple coupons, so Im trying to use if statements to change the $coupon. The problem with my code is that it loops through each if statement and takes you to the last one i.e. $coupon = "Referral" to $coupon = "Referral10". Basically it causes the user from going from 10% for one referral to 100% for one referral. Good for the customer, bad for the business. Heres my code. Its PHP. Can anyone help? Ive tried if/else, else if, and combinations of the two, and cant get it to work properly. I want it to stop after each if statement

$coupon = $referraldata["coupon"];
    if (empty($coupon)){
        $coupon = "Referral";
    } 

if ($coupon = "Referral"){
  $coupon = "Referral2";
} 

 if ($coupon = "Referral2"){
  $coupon = "Referral3";
} 

if ($coupon = "Referral3"){
  $coupon = "Referral4";
} 

if ($coupon = "Referral4"){
  $coupon = "Referral5";
} 

if ($coupon = "Referral5"){
  $coupon = "Referral6";
} 

if ($coupon = "Referral6"){
  $coupon = "Referral7";
} 

if ($coupon = "Referral7"){
  $coupon = "Referral8";
} 

if ($coupon = "Referral8"){
  $coupon = "Referral9";
} 

if ($coupon = "Referral9"){
  $coupon = "Referral10";
}

2 Answers

You can use a Switch. Check this link: http://www.php.net/manual/en/control-structures.switch.php

Jake White
Jake White
41,730 Points

Awesome. I thought that a Switch would work, but I wasn't sure since I haven't seen one done before. But it works perfectly. I now have a referral system using Stripe.

great!

Total guess but would it be something like adding an exit after each one and then the next one being an else?