Re: else if vs switch
| From: | April Mains | Date: | Mon, 18 Jun 2012 15:03:21 +0000 |
| Subject: | Re: else if vs switch | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-318277@lists.php.net to get a copy of this message | ||
On 2012-06-18, at 7:59 AM, James wrote:
>> ---- Original Message ----
>> From: April Mains <aprilmains@gmail.com>
>> To:
>> Cc: "PHP-General list" <php-general@lists.php.net>
>> 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\\(\\)\\?\\:\\;@\\.™\\,\\–\\&'\\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 <james@nixsecurity.org> 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 <lists@cmsws.com> 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' => 'default@domain.tld',
>>>> ...
>>>> );
>>>>
>>>> ...
>>>>
>>>> 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