Kryptronic Software Support Forum

You are viewing this forum as a guest. Login to an existing account, or create a new account, to reply to topics and to create new topics.

#1 08-25-2016 08:01:18

tguswell
Member
From: Plymouth, Devon
Registered: 07-31-2010
Posts: 128
Website

PHP question

Hi

In my customised shipping method I have the following:

Code:

elseif ((substr($postcode, 0, 2) == "PL")||(substr($postcode, 0, 2) == "TR")||(substr($postcode, 0, 2) == "EX")||(substr($postcode, 0, 2) == "TQ")||(substr($postcode, 0, 2) == "TA"))
{ Do this }

where I'm trying to find if the customer lives in our nearby postal areas. My php coding is not the best, as you can see, and I use long-winded coding to get my result.

Is there a cleaner way of writing the above code to get the same result?

Many thanks

Terry

Offline

 

#2 08-25-2016 09:38:25

tguswell
Member
From: Plymouth, Devon
Registered: 07-31-2010
Posts: 128
Website

Re: PHP question

I think I might have answered my own question!

I have discovered in_array() which would mean the statement would become:

Code:

(in_array(substr($postcode, 0, 2), array("PL", "TR", "EX", "TQ", "TA")))

This obviously looks much neater, I'll just need to make sure it works!

Terry

Offline

 

#3 09-01-2016 08:21:46

webmaster
Administrator
From: York, PA
Registered: 04-20-2001
Posts: 19951
Website

Re: PHP question

That's more complex than I would have written it, but it does the job.  I would have done a simple preg_match:

Code:

if (preg_match('/^(PL|TR|EX|TQ|TX)/',$postcode)) {$do_something = 1;}

Nick Hendler

Offline

 

Board footer