Re: else if vs switch

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 




April Mains <aprilmains@xxxxxxxxx> wrote:

>
>On 2012-06-18, at 7:59 AM, James wrote:
>
>>> ---- Original Message ----
>>> From: April Mains <aprilmains@xxxxxxxxx>
>>> To:
>>> Cc: "PHP-General list" <php-general@xxxxxxxxxxxxx>
>>> Sent: Mon, Jun 18, 2012, 9:41 AM
>>> Subject: Re:  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 <james@xxxxxxxxxxxxxxx>
>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@xxxxxxxxx> 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@xxxxxxxxxx',
>>>>> ...
>>>>> );
>>>>> 
>>>>> ...
>>>>> 
>>>>> 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




[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux