php-general Digest 24 Oct 2010 01:48:55 -0000 Issue 7003

Topics (messages 309036 through 309043):

Re: Possible foreach bug; seeking advice to isolate the problem
        309036 by: Peter Lind
        309040 by: Jonathan Sachs
        309041 by: Peter Lind
        309043 by: Jonathan Sachs

Re: Independent Contractor Suggestions
        309037 by: Per Jessen

Re: Entity 'reg' not defined
        309038 by: Richard Quadling

Re: a loop constructing the URLs and make PHP to fetch up to 10 thousand pages
        309039 by: Richard Quadling

Re: "My truth comes out" [1]
        309042 by: Nathan Rixham

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 23 October 2010 07:50, Jonathan Sachs <081...@jhsachs.com> wrote:
> Gary wrote:
>
>>Better. I can tell you how to solve it:
>>    $a = array('a', 'b','c');
>>    foreach($a as &$row){
>>        //you don't have to do anything here
>>    }
>>    unset($row); // <----<<< THIS IS KEY!
>>    print_r($a);
>>    foreach($a as $row){
>>        echo "<br />".$row;
>>    }
>>    print_r($a);
>
> I see what you're doing now: unsetting the variable, not the array
> element referenced by the variable. You're right, it does work.
>
> It's not exactly better than knowing what's wrong, though... it's
> different. I already had a workaround, although it was not quite as
> elegant. My desire now is to find out whether the behavior I described
> is due to something I don't understand about PHP, or to a bug. If it's
> something I don't understand, I want to understand it. If it's a bug,
> I want to report it.

It is not a bug - somewhere before the foreach loop you've got, a
variable is being referenced, and that's throwing things off.
Otherwise, unsetting the variable would have no effect on the problem
you're seeing.

Regards
Peter

-- 
<hype>
WWW: plphp.dk / plind.dk
LinkedIn: plind
BeWelcome/Couchsurfing: Fake51
Twitter: kafe15
</hype>

--- End Message ---
--- Begin Message ---
On Sat, 23 Oct 2010 08:34:27 +0200, peter.e.l...@gmail.com (Peter
Lind) wrote:

>It is not a bug - somewhere before the foreach loop you've got, a
>variable is being referenced, and that's throwing things off.
>Otherwise, unsetting the variable would have no effect on the problem
>you're seeing.

Could you please be more specific? "Somewhere... a variable is being
referenced" doesn't give me any insight into what is happening.

It's absolutely clear to me WHERE the problem is. The first foreach
loop is causing the problem; I can prove this by removing it. But WHAT
the problem is, is absolutely unclear. I've studied this code every
which way, and I don't see how the first foreach loop can possibly
make the second one change the array -- unless it's a bug.

I've boiled the script down to a couple of dozen lines that
demonstrate the problem without references to a database or to other
scripts. Having this to play with may help others give me some
insight.


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head> <title>foreach problem</title> </head>
<body>

<?php
   $qs = array();
   for ($i=0; $i<3; ++$i) {
      $qs[] = array($i);
   } 

   foreach ($qs as &$q) {
   }

   echo $qs[0][0] . $qs[1][0] . $qs[2][0] . "<br>";

   foreach ( $qs as $q ) {
      echo $qs[0][0] . $qs[1][0] . $qs[2][0] . "<br>";
   }
   
   echo $qs[0][0] . $qs[1][0] . $qs[2][0] . "<br>";
?>

</body>
</html>

--- End Message ---
--- Begin Message ---
On 23 October 2010 16:48, Jonathan Sachs <081...@jhsachs.com> wrote:
> On Sat, 23 Oct 2010 08:34:27 +0200, peter.e.l...@gmail.com (Peter
> Lind) wrote:
>
>>It is not a bug - somewhere before the foreach loop you've got, a
>>variable is being referenced, and that's throwing things off.
>>Otherwise, unsetting the variable would have no effect on the problem
>>you're seeing.
>
> Could you please be more specific? "Somewhere... a variable is being
> referenced" doesn't give me any insight into what is happening.
>
> It's absolutely clear to me WHERE the problem is. The first foreach
> loop is causing the problem; I can prove this by removing it. But WHAT
> the problem is, is absolutely unclear. I've studied this code every
> which way, and I don't see how the first foreach loop can possibly
> make the second one change the array -- unless it's a bug.
>
> I've boiled the script down to a couple of dozen lines that
> demonstrate the problem without references to a database or to other
> scripts. Having this to play with may help others give me some
> insight.
>
>
> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
> <html>
> <head> <title>foreach problem</title> </head>
> <body>
>
> <?php
>   $qs = array();
>   for ($i=0; $i<3; ++$i) {
>      $qs[] = array($i);
>   }
>
>   foreach ($qs as &$q) {
>   }
>
>   echo $qs[0][0] . $qs[1][0] . $qs[2][0] . "<br>";
>
>   foreach ( $qs as $q ) {
>      echo $qs[0][0] . $qs[1][0] . $qs[2][0] . "<br>";
>   }
>
>   echo $qs[0][0] . $qs[1][0] . $qs[2][0] . "<br>";
> ?>
>
> </body>
> </html>
>

You should read
http://dk2.php.net/manual/en/control-structures.foreach.php - it
points out the problem you're facing.

More information (and a complete breakdown of your problem) is
available here:
http://schlueters.de/blog/archives/141-References-and-foreach.html

It's not a bug - it's the effect you get when using references.

Regards
Peter

-- 
<hype>
WWW: plphp.dk / plind.dk
LinkedIn: plind
BeWelcome/Couchsurfing: Fake51
Twitter: kafe15
</hype>

--- End Message ---
--- Begin Message ---
On Sat, 23 Oct 2010 16:56:52 +0200, peter.e.l...@gmail.com (Peter
Lind) wrote:

>More information (and a complete breakdown of your problem) is
>available here:
>http://schlueters.de/blog/archives/141-References-and-foreach.html

Thank you! The situation is obscure and confusing, but that
explanation is quite clear.

Now that I understand it, I can see the same thing would happen if I
wrote the equivalent code in C, and probably in Java.

--- End Message ---
--- Begin Message ---
Paul M Foster wrote:

> On Wed, Oct 20, 2010 at 05:47:12PM -0700, Kris Craig wrote:
> 
>> I did a lot better after I started charging $100/hr for my work.  A
>> *lot* better!  This was after my research showed that PHP development
>> firms generally charge a minimum of $80/hr for PHP work, and can go
>> as
>> high as $200/hr.  So if you're going at $20/hr, the companies with
>> deep pockets probably won't take you seriously, and the clients you
>> do get will be the ones who want a ton of work done but don't have
>> the
>> budget available to make it worth your while.  Those are the clients
>> who will take advantage of you if you're not careful.
> 
> +1
> 
> I can't explain this phenomenon, but I've seen it before, and it's
> exactly as Kris has described.

A lot of people believe that if you don't pay for it, it's not worth
anything.  We all know that doesn't apply to everything (e.g. not open
source software), but when you're paying an individual for a job,
well ....



-- 
Per Jessen, Zürich (8.8°C)


--- End Message ---
--- Begin Message ---
On 22 October 2010 19:32, Bastien Koert <phps...@gmail.com> wrote:
> On Fri, Oct 22, 2010 at 2:28 PM, Richard Quadling <rquadl...@gmail.com> wrote:
>> On 22 October 2010 19:01, TR Shaw <ts...@oitc.com> wrote:
>>>
>>> On Oct 22, 2010, at 1:56 PM, Ashley Sheridan wrote:
>>>
>>>> On Fri, 2010-10-22 at 12:03 -0400, Adam Richardson wrote:
>>>>
>>>>> On Fri, Oct 22, 2010 at 11:47 AM, TR Shaw <ts...@oitc.com> wrote:
>>>>>
>>>>>> Anyone have an idea how to work around this? I tried:
>>>>>>
>>>>>> define ('reg', '®');
>>>>>> define ('&reg;', '®');
>>>>>>
>>>>>> can't figure how to override the entity table.  Errors follw:
>>>>>>
>>>>>> Warning: simplexml_load_string():
>>>>>> o.cc/46/e53d68e007fd45c2fccb502f2e7ccad5.php?user_id=47&amp;sub_id=61862469&reg;
>>>>>> in checkifup.php on line 5119
>>>>>>
>>>>>> Warning: simplexml_load_string():
>>>>>>                                     ^ in checkifup.php on line 5119
>>>>>>
>>>>>> Warning: simplexml_load_string(): Entity: line 220: parser error : Entity
>>>>>> 'reg' not defined in checkifup.php on line 5119
>>>>>>
>>>>>> Warning: simplexml_load_string():
>>>>>> /office/e53d68e007fd45c2fccb502f2e7ccad5.php?user_id=47&amp;sub_id=89877485&reg;
>>>>>> in checkifup.php on line 5119
>>>>>>
>>>>>>
>>>>>> --
>>>>>> PHP General Mailing List (http://www.php.net/)
>>>>>> To unsubscribe, visit: http://www.php.net/unsub.php
>>>>>>
>>>>>>
>>>>> Does doing a str_replace and changing it to the corresponding entity 
>>>>> number
>>>>> (&#174;) before parsing with simple_xml work?
>>>>>
>>>>> Here's a more robust function:
>>>>> http://www.sourcerally.net/Scripts/39-Convert-HTML-Entities-to-XML-Entities
>>>>>
>>>>> Adam
>>>>>
>>>>
>>>>
>>>>
>>>> This isn't a PHP error, it's an error with your XML. The regular HTML
>>>> entities which you're used to such as &reg; and &copy; are not
>>>> recognised in XML without first being declared as entities. The entities
>>>> exist in HTML because just outputting those characters won't always work
>>>> in a web browser (*Internet Explorer* *cough* *cough*) whereas XML was
>>>> never meant to be displayed in a browser, but transformed and then
>>>> output to a browser (among many other things) through XSLT. If you use
>>>> the characters directly in your XML you should be fine with the parser,
>>>> although you may have to make sure your document is saved in utf-8, as
>>>> most of the entity characters are above the ascii range.
>>>>
>>>
>>> Ash
>>>
>>> Its not my XML (&reg; isn't a www standard anyway) but its the xml provided 
>>> by the source I have to deal with and I can't change them so I guess I'll 
>>> just read the xml and then run defensing string replacements.
>>>
>>> Thanks for everyone's help.
>>>
>>> Tom
>>>
>>>
>>>
>>> --
>>> PHP General Mailing List (http://www.php.net/)
>>> To unsubscribe, visit: http://www.php.net/unsub.php
>>>
>>>
>>
>> My xml file is ...
>>
>> <?xml version="1.0"?>
>> <tag>&reg;</tag>
>>
>> Using different tools the error is always along the lines of ...
>>
>> "This page contains the following errors:
>>
>> error on line 1 at column 11: Entity 'reg' not defined
>> Below is a rendering of the page up to the first error."
>>
>>
>> <?xml version="1.0"?>
>> <tag>&#ae;</tag>
>>
>> "This page contains the following errors:
>>
>> error on line 2 at column 8: CharRef: invalid decimal value
>> Below is a rendering of the page up to the first error."
>>
>> The only thing I could do was ...
>>
>> <?xml version="1.0"?>
>> <tag>&amp;reg;</tag>
>>
>>
>> --
>> Richard Quadling
>> Twitter : EE : Zend
>> @RQuadling : e-e.com/M_248814.html : bit.ly/9O8vFY
>>
>> --
>> PHP General Mailing List (http://www.php.net/)
>> To unsubscribe, visit: http://www.php.net/unsub.php
>>
>>
>
> Couldn't you cdata wrap that value?

I'm guessing not as the XML is being received is invalid.

Whatever way you look at this, you have to fix the XML first.

Either server side (the best option) or client side by doing straight
string manipulation.


-- 
Richard Quadling
Twitter : EE : Zend
@RQuadling : e-e.com/M_248814.html : bit.ly/9O8vFY

--- End Message ---
--- Begin Message ---
On 23 October 2010 06:10, "jobst müller" <floo...@web.de> wrote:
> hello dear list - good morning!
>
>
> I am trying to figure out a method how to retrieve on the same URLs [see  
> below] with different query arguments, and i am wondering if this is doable 
> with PHP!?
>
> on a sidenote:  Well - i guess that we can do this with  LWP::UserAgent -  
> guess that this provides a way for us to loop through the query arguments: I 
> am not sure that LWP::UserAgent has a method for us to do that. I tried to 
> figure it out. And i digged deeper in the Manpages and Howtos. we can have a 
> loop constructing the URLs and use
> LWP::UserAgent repeatedly:
>
>
> see the Code:
>
> for my $id (0 .. 100000)
>
> {
>
>  $ua->get($url."?id=21&extern_eid=".(0-$id))
>
>  //rest of the code
>
> }
>
>
>
> Well, alternatively we can add a request_prepare handler that  computes and 
> add the query arguments before we send out the request. Do you think that 
> this fits the needs?
>
> But wait - i want to do this with PHP!
>
> What is aimed: Here on this following site we find a list of many  schools: 
> [see the page with the subsequent results - approx more than 1000 sites]
>
> see this site:  http://www-db.sn.schule.de/index.php?id=25
>
> i want to fetch the sites that are listet on this page - and therefore i  
> want to use PHP for this [job] - and subesquently
>  i want to parse them.
>
> the sites can be reached directly - by constructing in other words the 
> subsites of the overview can be reached via direct
>  links... see the following.
>
>
> http://www-db.sn.schule.de/index.php?]id=21&extern_eid=1543
>
> http://www-db.sn.schule.de/index.php?]id=21&extern_eid=709
>
> http://www-db.sn.schule.de/index.php?]id=21&extern_eid=789
>
> http://www-db.sn.schule.de/index.php?]id=21&extern_eid=1297
>
>
>
> Well - i want to fetch all those.... And i try to do it with PHP and a
> mentioned loop. Does this work!?
> ___________________________________________________________
> GRATIS! Movie-FLAT mit über 300 Videos.
> Jetzt freischalten unter http://movieflat.web.de
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>

You can go down that route. If the site has an API allowing you to
gather the data another way (i.e. in 1 large file), then use that
instead.

I'd also make sure you are allowed to gather all that data. You may
find some issues with that - just check to be sure.

Also, you had a typo in the URLs ... an extra ] for no reason.



-- 
Richard Quadling
Twitter : EE : Zend
@RQuadling : e-e.com/M_248814.html : bit.ly/9O8vFY

--- End Message ---
--- Begin Message ---
Richard Quadling wrote:
On 21 October 2010 12:42, Richard Quadling <rquadl...@gmail.com> wrote:
On 21 October 2010 10:39, Gary <php-gene...@garydjones.name> wrote:
Is there any nice way to convert a string containing either "TRUE" or
"FALSE" to a bool with the appropriate value? I know I can just "if
(strcmp..." but, as the song goes on to say "...ugly, so ugly, it's ugly"[1]
What is the response when the string is NOT "TRUE" or "FALSE"?

<?php
$s_OriginalValue = 'FALSE';
$b_Test = in_array($s_Test = strtolower($s_OriginalValue),
array('true', 'false')) ? !!strcmp($s_Test, 'false') : null;
var_dump($b_Test);

$s_OriginalValue = 'TRUE';
$b_Test = in_array($s_Test = strtolower($s_OriginalValue),
array('true', 'false')) ? !!strcmp($s_Test, 'false') : null;
var_dump($b_Test);


$s_OriginalValue = 'GARBAGE';
$b_Test = in_array($s_Test = strtolower($s_OriginalValue),
array('true', 'false')) ? !!strcmp($s_Test, 'false') : null;
var_dump($b_Test);
?>

outputs ...

bool(false)
bool(true)
NULL

This is one of those threads where you can go mad with proposals, thus:

$value = constant($value);

just so happens that constant() is case insensitive and there are constants "true" and "false"

<?php
var_dump( constant("false") );
var_dump( constant("FALSE") );
var_dump( constant("true") );
var_dump( constant("TRUE") );

//output
bool(false)
bool(false)
bool(true)
bool(true)


--- End Message ---

Reply via email to