php-general Digest 21 Nov 2008 14:29:43 -0000 Issue 5802

Topics (messages 283624 through 283631):

Re: in_array breaks down for 0 as value
        283624 by: Lars Torben Wilson
        283628 by: Stut

reading XML
        283625 by: Angelo Zanetti
        283626 by: Jim Lucas
        283627 by: Jim Lucas
        283629 by: Nathan Rixham

Re: user access/roles/privs functionality
        283630 by: Martijn Korse

Anyway to simulate pcntl_fork() on Windows?
        283631 by: Brian Dunning

Administrivia:

To subscribe to the digest, e-mail:
        [EMAIL PROTECTED]

To unsubscribe from the digest, e-mail:
        [EMAIL PROTECTED]

To post to the list, e-mail:
        [EMAIL PROTECTED]


----------------------------------------------------------------------
--- Begin Message ---
2008/11/20 Stut <[EMAIL PROTECTED]>:
> On 20 Nov 2008, at 06:55, Yashesh Bhatia wrote:
>>
>>  I wanted to use in_array to verify the results of a form submission
>> for a checkbox and found an interesting
>> behaviour.
>>
>> $ php -v
>> PHP 5.2.5 (cli) (built: Jan 12 2008 14:54:37)
>> $
>>
>> $ cat in_array2.php
>> <?php
>> $node_review_types = array(
>>                          'page'       => 'page',
>>                          'story'      => 'story',
>>                          'nodereview' => 'abc',
>>                          );
>>
>> if (in_array('page', $node_review_types)) {
>>  print "page found in node_review_types\n";
>> }
>> if (in_array('nodereview', $node_review_types)) {
>>  print "nodereview found in node_review_types\n";
>> }
>>
>> ?>
>> $ php in_array2.php
>> page found in node_review_types
>> $
>>
>> This  works fine. but if i change the value of the key 'nodereview' to
>> 0 it breaks down.
>>
>> $ diff in_array2.php in_array3.php
>> 6c6
>> <                            'nodereview' => 'abc',
>> ---
>>>
>>>                          'nodereview' => 0,
>>
>> $
>>
>> $ php in_array3.php
>> page found in node_review_types
>> nodereview found in node_review_types
>> $
>>
>> Any reason why in_array is returning TRUE when one has a 0 value on the
>> array ?
>
> That's weird, 5.2.6 does the same thing. There's actually a comment about
> this on the in_array manual page from james dot ellis at gmail dot com...
>
> <quote>
>
> Be aware of oddities when dealing with 0 (zero) values in an array...
>
> This script:
> <?php
> $array = array('testing',0,'name');
> var_dump($array);
> //this will return true
> var_dump(in_array('foo', $array));
> //this will return false
> var_dump(in_array('foo', $array, TRUE));
> ?>
>
> It seems in non strict mode, the 0 value in the array is evaluating to
> boolean FALSE and in_array returns TRUE. Use strict mode to work around this
> peculiarity.
> This only seems to occur when there is an integer 0 in the array. A string
> '0' will return FALSE for the first test above (at least in 5.2.6).
>
> </quote>
>
> So use strict mode and this problem will go away. Oh, and please read the
> manual before asking a question in future.
>
> -Stut

I wouldn't consider it weird; it's just how PHP handles loose type
comparisons. I would certainly agree that it's not terribly obvious
why it happens, though. :) That said, it's consistent with PHP
behaviour.

James Ellis almost got it right in his note. As I already noted, it's
not because of a conversion to boolean FALSE, but a conversion to
integer 0. You can test this by substituting FALSE for the 0 in the
array in the example and trying it.


Torben

--- End Message ---
--- Begin Message ---
On 20 Nov 2008, at 23:09, Ashley Sheridan wrote:
On Thu, 2008-11-20 at 09:25 +0000, Stut wrote:
On 20 Nov 2008, at 06:55, Yashesh Bhatia wrote:
I wanted to use in_array to verify the results of a form submission
for a checkbox and found an interesting
behaviour.

$ php -v
PHP 5.2.5 (cli) (built: Jan 12 2008 14:54:37)
$

$ cat in_array2.php
<?php
$node_review_types = array(
                         'page'       => 'page',
                         'story'      => 'story',
                         'nodereview' => 'abc',
                         );

if (in_array('page', $node_review_types)) {
print "page found in node_review_types\n";
}
if (in_array('nodereview', $node_review_types)) {
print "nodereview found in node_review_types\n";
}

?>
$ php in_array2.php
page found in node_review_types
$

This works fine. but if i change the value of the key 'nodereview' to
0 it breaks down.

$ diff in_array2.php in_array3.php
6c6
<                            'nodereview' => 'abc',
---
                         'nodereview' => 0,
$

$ php in_array3.php
page found in node_review_types
nodereview found in node_review_types
$

Any reason why in_array is returning TRUE when one has a 0 value on
the array ?

That's weird, 5.2.6 does the same thing. There's actually a comment
about this on the in_array manual page from james dot ellis at gmail
dot com...

<quote>

Be aware of oddities when dealing with 0 (zero) values in an array...

This script:
<?php
$array = array('testing',0,'name');
var_dump($array);
//this will return true
var_dump(in_array('foo', $array));
//this will return false
var_dump(in_array('foo', $array, TRUE));
?>

It seems in non strict mode, the 0 value in the array is evaluating to
boolean FALSE and in_array returns TRUE. Use strict mode to work
around this peculiarity.
This only seems to occur when there is an integer 0 in the array. A
string '0' will return FALSE for the first test above (at least in
5.2.6).

</quote>

So use strict mode and this problem will go away. Oh, and please read
the manual before asking a question in future.

-Stut

--
http://stut.net/

What about using the === and !== comparisons to compare and make sure
that 0 is not giving a false false.

That's effectively what using strict mode does. RTFM please.

-Stut

--
http://stut.net/

--- End Message ---
--- Begin Message ---
Hi all, 

 

I have a script that I call with CURL and get an XML response with a few
parameters.

 

I need to be able to read that XML but not sure how to.

 

I know of simpleXML but the server is php4.

 

Would xml_parse be the thing to look at?

 

What is the simplest way to accomplish this small challenge?

 

Thanks in advance.

 

 


--- End Message ---
--- Begin Message ---
Angelo Zanetti wrote:
Hi all,
I have a script that I call with CURL and get an XML response with a few
parameters.

I need to be able to read that XML but not sure how to.

I know of simpleXML but the server is php4.

Would xml_parse be the thing to look at?

What is the simplest way to accomplish this small challenge?

Thanks in advance.



Maybe look into this example. Just a quick search and found this. Not sure if it will even work in your situation, but thought I would pass it along.

http://us2.php.net/manual/en/function.xml-parser-create.php#38739

Jim Lucas

--- End Message ---
--- Begin Message ---
Angelo Zanetti wrote:
Hi all,
I have a script that I call with CURL and get an XML response with a few
parameters.

I need to be able to read that XML but not sure how to.

I know of simpleXML but the server is php4.

Would xml_parse be the thing to look at?

What is the simplest way to accomplish this small challenge?

Thanks in advance.



Or this one even

http://us2.php.net/manual/en/function.xml-parse.php#83416

Jim Lucas

--- End Message ---
--- Begin Message ---
Angelo Zanetti wrote:

What is the simplest way to accomplish this small challenge?


you could give this a go if you like (one i made earlier): http://programphp.com/xmlparser.phps - class at the top, usage at the bottom.

it simply turns xml into an associated array, using regex; just tested it again as it's a couple of years old and it's working on php 4.4.7 and php 5.2.5
--- End Message ---
--- Begin Message ---

bruce-60 wrote:
> 
> Hi list...
> 
> I need a way of managing users/teams/etc.. implementing roles/access
> rights/privs,etc...

Have a look at Zend_Acl:

http://framework.zend.com/manual/en/zend.acl.html

-----
http://devshed.excudo.net http://devshed.excudo.net 
-- 
View this message in context: 
http://www.nabble.com/user-access-roles-privs-functionality-tp20508624p20620446.html
Sent from the PHP - General mailing list archive at Nabble.com.


--- End Message ---
--- Begin Message --- It's the function I need most and it's not available on Windows, and unfortunately that's what the client uses. Any suggestions for a workaround?
--- End Message ---

Reply via email to