php-general Digest 22 Feb 2013 00:05:38 -0000 Issue 8132

Topics (messages 320275 through 320277):

Re: parsing select multiple="multiple"
        320275 by: tamouse mailing lists
        320276 by: Jim Giner

PHP 5.4.12 and PHP 5.3.22 released!
        320277 by: Johannes Schlüter

Administrivia:

To subscribe to the digest, e-mail:
        php-general-digest-subscr...@lists.php.net

To unsubscribe from the digest, e-mail:
        php-general-digest-unsubscr...@lists.php.net

To post to the list, e-mail:
        php-gene...@lists.php.net


----------------------------------------------------------------------
--- Begin Message ---
On Tue, Feb 19, 2013 at 1:02 PM, John Taylor-Johnston
<john.taylor-johns...@cegepsherbrooke.qc.ca> wrote:
>
> tamouse mailing lists wrote:
>>>
>>> >I hate arrays. :D
>>
>> Here's a small snippet showing how it works, I hope:
>>
>> foreach ($DPRpriority as $item => $value) {
>>    echo "<li> ".$item.": ".$value['name']." selected:
>> ".$value['selected']." </li>\n";
>> }
>>
> Question 1: when did we have to add [] to a <input> name to turn it into an
> array?
>
> <input type="checkbox" name="DPRlocationdetails[]" value="Unknown">
>
> According to phpinfo() it still comes out as $_POST['DPRlocationdetails']
> without [].
>
> Are the [] necessary?

[] are necessary when you want to return multiple values for a form
field, or collection of form fields such as checkbox. AFAIK, this has
always been the case with PHP. See
https://gist.github.com/tamouse/5002728

> ----------------------------------------------
> Question 2:
> I was looking at some code in the Manual, where someone used isset and
> is_array.
>
> How necessary is if(isset($_POST['DPRlocationdetails']))
>
> and then to use: if(is_array($_POST['DPRlocationdetails']))
>
> That seems like over kill?

It's more defensive, in the case where someone may be by-passing your
form to send things in.

> ----------------------------------------------
> Question 3:
>
> My code works, perfectly.

Then there must be no questions. :)

> In this case, I decided to attack some check-boxes
> first. The resulting function will work for <select multiple> too..
>
> Does anyone see me doing something wrong in my code below?
>
> My questions are:
>
> Is this the only way to pass "Unknown", "Family Home" or "Apartment" into
> the function?
>
> Is this correct?
>
> ---- if ($_POST['DPRlocationdetails'] == "Unknown")
>
> Somebody once told me I had to do it this way?
>
> ---- if ("Unknown" == $_POST['DPRlocationdetails'])

In this scenario, these are equivalent. There is no preference of one
over the other.

I *have* heard claims that something like this is preferrable, though:

if (FALSE === $variable)

But I think that may have been due to some misunderstanding of
precedences in the following sort of scenario:

if (FALSE === ($result = some_function())

where if done this way:

if ($result = some_function() === FALSE)

was giving them bad results.

> John
>
> --------------------snip---------------------------
>
> <form action="foo.php" id="DPRform" method="post"><input value="Update"
> type="submit">
> <input type="checkbox" name="DPRlocationdetails[]" value="Unknown" <?php
> filter_value($_POST['DPRlocationdetails'],"Unknown"); ?>> Unknown
> <input type="checkbox" name="DPRlocationdetails[]" value="Family Home" <?php
> filter_value($_POST['DPRlocationdetails'],"Family Home"); ?>> Family Home
> <input type="checkbox" name="DPRlocationdetails[]" value="Apartment" <?php
> filter_value($_POST['DPRlocationdetails'],"Apartment"); ?>> Apartment
> </form>
>
> <?php
> function filter_value($tofilter,$tofind) {
> foreach($tofilter as $value){
>     if ($value == $tofind) echo "checked";
>     }
> }
> ?>
>
>

--- End Message ---
--- Begin Message ---


I *have* heard claims that something like this is preferrable, though:

if (FALSE === $variable)

I believe I read a comment somewhere once that writing your IF statements that way helped to trigger an error message when the coder forgot to use the double equal sign (==) in the statement. I haven't adopted it yet, but I think that's the sole reason for it. Basically you can't make an assignment (=) to a constant or some non-variable target.
--- End Message ---
--- Begin Message ---
The PHP development team announces the immediate availability of PHP 
5.4.12 and PHP 5.3.22. These releases fix about 10 bugs. All users of 
PHP are encouraged to upgrade to PHP 5.4. PHP 5.3.22 is recommended 
for those wishing to remain on the 5.3 series.

The full list of changes are recorded in the ChangeLog on 
http://www.php.net/ChangeLog-5.php

For source downloads of PHP 5.4.12 and PHP 5.3.22 please visit our 
downloads page at http://www.php.net/downloads.php

Windows binaries can be found on http://windows.php.net/download/

Stanislav Malyshev         Johannes Schlüter
PHP 5.4 Release Master     PHP 5.3 Release Master



--- End Message ---

Reply via email to