Welcome! Log In Create A New Profile

Advanced

[PHP] else if vs switch

Posted by April Mains 
April Mains
[PHP] else if vs switch
June 16, 2012 12:30AM
I have 25 cities and am currently using the following to set the $toaddress for submitting student pre-registration forms based on the city selected:

if ($city == "Calgary") {
$toaddress = "abc@emailaddress";
} elseif ($city == "Brooks") {
$toaddress = "def@emailaddress";

and so on.


Would using switch statements like the following make any appreciable difference?

switch ($city) {
case"Calgary":
$toaddress = "abc@emailaddress";
break;
case"Brooks":
$toaddress = "def@emailaddress";
break;

and so on.

Is there a more elegant solution?

Thank you for your help,

April
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Joshua Kehn
Re: [PHP] else if vs switch
June 16, 2012 12:40AM
Way easier to just use a map.

$mapping = array(
'Calgary' => "abc@emailaddress",
'Brooks' => "def@emailaddress",
// etc
);
$toaddress = $mapping[$city];

Regards,

–Josh
____________________________________
Joshua Kehn | @joshkehn
http://joshuakehn.com

On Jun 15, 2012, at 6:20 PM, April Mains wrote:

> I have 25 cities and am currently using the following to set the $toaddress for submitting student pre-registration forms based on the city selected:
>
> if ($city == "Calgary") {
> $toaddress = "abc@emailaddress";
> } elseif ($city == "Brooks") {
> $toaddress = "def@emailaddress";
>
> and so on.
>
>
> Would using switch statements like the following make any appreciable difference?
>
> switch ($city) {
> case"Calgary":
> $toaddress = "abc@emailaddress";
> break;
> case"Brooks":
> $toaddress = "def@emailaddress";
> break;
>
> and so on.
>
> Is there a more elegant solution?
>
> Thank you for your help,
>
> April
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
April Mains
Re: [PHP] else if vs switch
June 16, 2012 12:50AM
Ah yes that's it.

Thank you for your help. Have a good weekend.

April

On 2012-06-15, at 4:29 PM, Joshua Kehn wrote:

> Way easier to just use a map.
>
> $mapping = array(
> 'Calgary' => "abc@emailaddress",
> 'Brooks' => "def@emailaddress",
> // etc
> );
> $toaddress = $mapping[$city];
>
> Regards,
>
> –Josh
> ____________________________________
> Joshua Kehn | @joshkehn
> http://joshuakehn.com
>
> On Jun 15, 2012, at 6:20 PM, April Mains wrote:
>
>> I have 25 cities and am currently using the following to set the $toaddress for submitting student pre-registration forms based on the city selected:
>>
>> if ($city == "Calgary") {
>> $toaddress = "abc@emailaddress";
>> } elseif ($city == "Brooks") {
>> $toaddress = "def@emailaddress";
>>
>> and so on.
>>
>>
>> Would using switch statements like the following make any appreciable difference?
>>
>> switch ($city) {
>> case"Calgary":
>> $toaddress = "abc@emailaddress";
>> break;
>> case"Brooks":
>> $toaddress = "def@emailaddress";
>> break;
>>
>> and so on.
>>
>> Is there a more elegant solution?
>>
>> Thank you for your help,
>>
>> April
>> --
>> PHP General Mailing List (http://www.php.net/)
>> To unsubscribe, visit: http://www.php.net/unsub.php
>>
>
James Yerge
Re: [PHP] else if vs switch
June 16, 2012 01:00AM
On 06/15/2012 06:44 PM, April Mains wrote:
> Ah yes that's it.
>
> Thank you for your help. Have a good weekend.
>
> April
>
> On 2012-06-15, at 4:29 PM, Joshua Kehn wrote:
>
>> Way easier to just use a map.
>>
>> $mapping = array(
>> 'Calgary' => "abc@emailaddress",
>> 'Brooks' => "def@emailaddress",
>> // etc
>> );
>> $toaddress = $mapping[$city];
>>
>> Regards,
>>
>> –Josh
>> ____________________________________
>> Joshua Kehn | @joshkehn
>> http://joshuakehn.com
>>
>> On Jun 15, 2012, at 6:20 PM, April Mains wrote:
>>
>>> I have 25 cities and am currently using the following to set the $toaddress for submitting student pre-registration forms based on the city selected:
>>>
>>> if ($city == "Calgary") {
>>> $toaddress = "abc@emailaddress";
>>> } elseif ($city == "Brooks") {
>>> $toaddress = "def@emailaddress";
>>>
>>> and so on.
>>>
>>>
>>> Would using switch statements like the following make any appreciable difference?
>>>
>>> switch ($city) {
>>> case"Calgary":
>>> $toaddress = "abc@emailaddress";
>>> break;
>>> case"Brooks":
>>> $toaddress = "def@emailaddress";
>>> break;
>>>
>>> and so on.
>>>
>>> Is there a more elegant solution?
>>>
>>> Thank you for your help,
>>>
>>> April
>>> --
>>> PHP General Mailing List (http://www.php.net/)
>>> To unsubscribe, visit: http://www.php.net/unsub.php
>>>
>

If a database solution isn't desired, check out the parse_ini_file()
function. You can create a simple INI file containing the necessary
mappings and, instead of having to touch the code every time you need to
update a record, you simply modify the INI file.

Another solution would be to use Joshua's suggestion but instead of
managing the array in the PHP file using that array, throw the array in
a separate PHP file and simply require_once() it.

Of course, these solutions assume that the PHP script reading the INI
file or including the PHP that contains the array has the appropriate
file permissions to do so.

Just my two cents :)

- James

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Jim Lucas
Re: [PHP] else if vs switch
June 18, 2012 02:20AM
On 6/15/2012 3:29 PM, Joshua Kehn wrote:
> Way easier to just use a map.
>
> $mapping = array(
> 'Calgary' => "abc@emailaddress",
> 'Brooks' => "def@emailaddress",
> // etc
> );
> $toaddress = $mapping[$city];

I would use this, but add a check to it.

$mapping = array(
'default' => '[email protected]',
....
);

....

if ( isset($mapping[$city]) ) {
$toaddress = $mapping[$city];
} else {
$toaddress = $mapping['default'];
}

Jim

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
James
Re: [PHP] else if vs switch
June 18, 2012 03:50AM
Same logical check with my personal preference ;)

$toaddress = $mapping['default'];

if ( isset($city) && isset($mapping[$city]) ) { ... }

--
Sent from my Android phone with K-9 Mail. Please excuse my brevity.

Jim Lucas <[email protected]> wrote:

On 6/15/2012 3:29 PM, Joshua Kehn wrote:
> Way easier to just use a map.
>
> $mapping = array(
> 'Calgary' => "abc@emailaddress",
> 'Brooks' => "def@emailaddress",
> // etc
> );
> $toaddress = $mapping[$city];

I would use this, but add a check to it.

$mapping = array(
'default' => '[email protected]',
....
);

....

if ( isset($mapping[$city]) ) {
$toaddress = $mapping[$city];
} else {
$toaddress = $mapping['default'];
}

Jim

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
April Mains
Re: [PHP] else if vs switch
June 18, 2012 03:50PM
This is what I had been using as the check based on the code that had been
there previously and along with an email validator that sets $email to ""
if the address isn't valid. The purpose of the form is lead generation. The
last bit is to prevent spammers from entering urls in the class text box.

iif (($name == "") || ($email == "") || ($phone =="") || ($city=="Select
your city") || ($class=="") ||
preg_match("/[^A-Za-z0-9-\\s\\(\\)\\?\\:\\;@\\.&trade;\\,\\&ndash;\\&'\\t]/uis",
$class))
{...}

Does this do the same thing as isset? Would isset be better?

April

On Sun, Jun 17, 2012 at 7:41 PM, James <[email protected]> wrote:

> Same logical check with my personal preference ;)
>
> $toaddress = $mapping['default'];
>
> if ( isset($city) && isset($mapping[$city]) ) { ... }
>
> --
> Sent from my Android phone with K-9 Mail. Please excuse my brevity.
>
> Jim Lucas <[email protected]> wrote:
>>
>> On 6/15/2012 3:29 PM, Joshua Kehn wrote:
>> > Way easier to just use a map.
>> >
>> > $mapping = array(
>>
>> > 'Calgary' => "abc@emailaddress",
>> > 'Brooks' => "def@emailaddress",
>> > // etc
>> > );
>> > $toaddress = $mapping[$city];
>>
>> I would use this, but add a check to it.
>>
>> $mapping = array(
>> 'default' => '[email protected]',
>> ...
>> );
>>
>> ...
>>
>> if ( isset($mapping[$city]) ) {
>> $toaddress = $mapping[$city];
>>
>> } else {
>> $toaddress = $mapping['default'];
>>
>> }
>>
>> Jim
>>
>> --
>> PHP General Mailing List (http://www.php.net/)
>> To unsubscribe, visit: http://www.php.net/unsub.php
>>
>>
>>
James
Re: Re: [PHP] else if vs switch
June 18, 2012 04:00PM
>---- Original Message ----
>From: April Mains <[email protected]>
>To:
>Cc: "PHP-General list" <[email protected]>
>Sent: Mon, Jun 18, 2012, 9:41 AM
>Subject: Re: [PHP] else if vs switch
>
>This is what I had been using as the check based on the code that had been
>there previously and along with an email validator that sets $email to ""
>if the address isn't valid. The purpose of the form is lead generation. The
>last bit is to prevent spammers from entering urls in the class text box.
>
>iif (($name == "") || ($email == "") || ($phone =="") || ($city=="Select
>your city") || ($class=="") ||
>preg_match("/[^A-Za-z0-9-\\s\\(\\)\\?\\:\\;@\\.&trade;\\,\\&ndash;\\&'\\t]/uis",
>$class))
>{...}
>
>Does this do the same thing as isset? Would isset be better?
>
>April
>
>On Sun, Jun 17, 2012 at 7:41 PM, James <[email protected]> wrote:
>
>> Same logical check with my personal preference ;)
>>
>> $toaddress = $mapping['default'];
>>
>> if ( isset($city) && isset($mapping[$city]) ) { ... }
>>
>> --
>> Sent from my Android phone with K-9 Mail. Please excuse my brevity.
>>
>> Jim Lucas <[email protected]> wrote:
>>>
>>> On 6/15/2012 3:29 PM, Joshua Kehn wrote:
>>> > Way easier to just use a map.
>>> >
>>> > $mapping = array(
>>>
>>> > 'Calgary' => "abc@emailaddress",
>>> > 'Brooks' => "def@emailaddress",
>>> > // etc
>>> > );
>>> > $toaddress = $mapping[$city];
>>>
>>> I would use this, but add a check to it.
>>>
>>> $mapping = array(
>>> 'default' => '[email protected]',
>>> ...
>>> );
>>>
>>> ...
>>>
>>> if ( isset($mapping[$city]) ) {
>>> $toaddress = $mapping[$city];
>>>
>>> } else {
>>> $toaddress = $mapping['default'];
>>>
>>> }
>>>
>>> Jim
>>>
>>> --
>>> PHP General Mailing List (http://www.php.net/)
>>> To unsubscribe, visit: http://www.php.net/unsub.php
>>>
>>>
>>>


Technically, no, that's not the same as using isset(). The isset() function determines if the variable is set and is not NULL. Your checks, for example, will throw a PHP Notice if any one of those variables were never initialized in the scope of your script. If you're not going to initialize your variables before performing the check, then using isset() would be ideal. However, for the sake of correctness, I'd recommend you either initialize your variables or use isset(). Take a look at the empty() function in PHP, it may suit your needs. http://us3.php.net/manual/en/function.empty.php.

PHP Notice message example:
[18-Jun-2012 13:43:20 UTC] PHP Notice: Undefined variable: name in /root/test.php on line 2

Example of variable initialization

$name = "";
$email = "";
$phone = "";
$city = "Select your city";
$class = "";

James


--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
April Mains
Re: [PHP] else if vs switch
June 18, 2012 05:10PM
On 2012-06-18, at 7:59 AM, James wrote:

>> ---- Original Message ----
>> From: April Mains <[email protected]>
>> To:
>> Cc: "PHP-General list" <[email protected]>
>> Sent: Mon, Jun 18, 2012, 9:41 AM
>> Subject: Re: [PHP] else if vs switch
>>
>> This is what I had been using as the check based on the code that had been
>> there previously and along with an email validator that sets $email to ""
>> if the address isn't valid. The purpose of the form is lead generation. The
>> last bit is to prevent spammers from entering urls in the class text box.
>>
>> iif (($name == "") || ($email == "") || ($phone =="") || ($city=="Select
>> your city") || ($class=="") ||
>> preg_match("/[^A-Za-z0-9-\\s\\(\\)\\?\\:\\;@\\.&trade;\\,\\&ndash;\\&'\\t]/uis",
>> $class))
>> {...}
>>
>> Does this do the same thing as isset? Would isset be better?
>>
>> April
>>
>> On Sun, Jun 17, 2012 at 7:41 PM, James <[email protected]> wrote:
>>
>>> Same logical check with my personal preference ;)
>>>
>>> $toaddress = $mapping['default'];
>>>
>>> if ( isset($city) && isset($mapping[$city]) ) { ... }
>>>
>>> --
>>> Sent from my Android phone with K-9 Mail. Please excuse my brevity.
>>>
>>> Jim Lucas <[email protected]> wrote:
>>>>
>>>> On 6/15/2012 3:29 PM, Joshua Kehn wrote:
>>>>> Way easier to just use a map.
>>>>>
>>>>> $mapping = array(
>>>>
>>>>> 'Calgary' => "abc@emailaddress",
>>>>> 'Brooks' => "def@emailaddress",
>>>>> // etc
>>>>> );
>>>>> $toaddress = $mapping[$city];
>>>>
>>>> I would use this, but add a check to it.
>>>>
>>>> $mapping = array(
>>>> 'default' => '[email protected]',
>>>> ...
>>>> );
>>>>
>>>> ...
>>>>
>>>> if ( isset($mapping[$city]) ) {
>>>> $toaddress = $mapping[$city];
>>>>
>>>> } else {
>>>> $toaddress = $mapping['default'];
>>>>
>>>> }
>>>>
>>>> Jim
>>>>
>>>> --
>>>> PHP General Mailing List (http://www.php.net/)
>>>> To unsubscribe, visit: http://www.php.net/unsub.php
>>>>
>>>>
>>>>
>
>
> Technically, no, that's not the same as using isset(). The isset() function determines if the variable is set and is not NULL. Your checks, for example, will throw a PHP Notice if any one of those variables were never initialized in the scope of your script. If you're not going to initialize your variables before performing the check, then using isset() would be ideal. However, for the sake of correctness, I'd recommend you either initialize your variables or use isset(). Take a look at the empty() function in PHP, it may suit your needs. http://us3.php.net/manual/en/function.empty.php.
>
> PHP Notice message example:
> [18-Jun-2012 13:43:20 UTC] PHP Notice: Undefined variable: name in /root/test.php on line 2
>
> Example of variable initialization
>
> $name = "";
> $email = "";
> $phone = "";
> $city = "Select your city";
> $class = "";
>
> James
>


Sorry I didn't include the whole script. The variables as they come in from a form as follows:

//initialize variables
$name=$_POST['name'];
$email=$_POST['email'];
$phone=$_POST['phone'];
$city=$_POST['city'];
$class=$_POST['class'];

Thus its not throwing an errors.

Thank you for all your help with this. Its given me lots to think about.

April






--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Ashley Sheridan
Re: [PHP] else if vs switch
June 18, 2012 07:00PM
April Mains <[email protected]> wrote:

>
>On 2012-06-18, at 7:59 AM, James wrote:
>
>>> ---- Original Message ----
>>> From: April Mains <[email protected]>
>>> To:
>>> Cc: "PHP-General list" <[email protected]>
>>> Sent: Mon, Jun 18, 2012, 9:41 AM
>>> Subject: Re: [PHP] else if vs switch
>>>
>>> This is what I had been using as the check based on the code that
>had been
>>> there previously and along with an email validator that sets $email
>to ""
>>> if the address isn't valid. The purpose of the form is lead
>generation. The
>>> last bit is to prevent spammers from entering urls in the class text
>box.
>>>
>>> iif (($name == "") || ($email == "") || ($phone =="") ||
>($city=="Select
>>> your city") || ($class=="") ||
>>>
>preg_match("/[^A-Za-z0-9-\\s\\(\\)\\?\\:\\;@\\.&trade;\\,\\&ndash;\\&'\\t]/uis",
>>> $class))
>>> {...}
>>>
>>> Does this do the same thing as isset? Would isset be better?
>>>
>>> April
>>>
>>> On Sun, Jun 17, 2012 at 7:41 PM, James <[email protected]>
>wrote:
>>>
>>>> Same logical check with my personal preference ;)
>>>>
>>>> $toaddress = $mapping['default'];
>>>>
>>>> if ( isset($city) && isset($mapping[$city]) ) { ... }
>>>>
>>>> --
>>>> Sent from my Android phone with K-9 Mail.. Please excuse my brevity.
>>>>
>>>> Jim Lucas <[email protected]> wrote:
>>>>>
>>>>> On 6/15/2012 3:29 PM, Joshua Kehn wrote:
>>>>>> Way easier to just use a map.
>>>>>>
>>>>>> $mapping = array(
>>>>>
>>>>>> 'Calgary' => "abc@emailaddress",
>>>>>> 'Brooks' => "def@emailaddress",
>>>>>> // etc
>>>>>> );
>>>>>> $toaddress = $mapping[$city];
>>>>>
>>>>> I would use this, but add a check to it.
>>>>>
>>>>> $mapping = array(
>>>>> 'default' => '[email protected]',
>>>>> ...
>>>>> );
>>>>>
>>>>> ...
>>>>>
>>>>> if ( isset($mapping[$city]) ) {
>>>>> $toaddress = $mapping[$city];
>>>>>
>>>>> } else {
>>>>> $toaddress = $mapping['default'];
>>>>>
>>>>> }
>>>>>
>>>>> Jim
>>>>>
>>>>> --
>>>>> PHP General Mailing List (http://www.php.net/)
>>>>> To unsubscribe, visit: http://www.php.net/unsub.php
>>>>>
>>>>>
>>>>>
>>
>>
>> Technically, no, that's not the same as using isset(). The isset()
>function determines if the variable is set and is not NULL. Your
>checks, for example, will throw a PHP Notice if any one of those
>variables were never initialized in the scope of your script. If you're
>not going to initialize your variables before performing the check,
>then using isset() would be ideal. However, for the sake of
>correctness, I'd recommend you either initialize your variables or use
>isset().. Take a look at the empty() function in PHP, it may suit your
>needs. http://us3.php.net/manual/en/function.empty.php.
>>
>> PHP Notice message example:
>> [18-Jun-2012 13:43:20 UTC] PHP Notice: Undefined variable: name in
>/root/test.php on line 2
>>
>> Example of variable initialization
>>
>> $name = "";
>> $email = "";
>> $phone = "";
>> $city = "Select your city";
>> $class = "";
>>
>> James
>>
>
>
>Sorry I didn't include the whole script. The variables as they come in
>from a form as follows:
>
>//initialize variables
>$name=$_POST['name'];
>$email=$_POST['email'];
>$phone=$_POST['phone'];
>$city=$_POST['city'];
>$class=$_POST['class'];
>
>Thus its not throwing an errors.
>
>Thank you for all your help with this. Its given me lots to think
>about.
>
>April
>
>
>
>
>
>
>--
>PHP General Mailing List (http://www.php.net/)
>To unsubscribe, visit: http://www.php.net/unsub.php

If they're coming from the $_POST array you should be doing the asset() there:

$name = (isset($_POST['name']))?$_POST['name']:'';

This is because the you can never rely on user data, even when you think its coming from a form you built. It may cone from another source or something may get changed which breaks the receiving code.

Thanks,
Ash
http://ashleysheridan.co.uk

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Sorry, only registered users may post in this forum.

Click here to login