php-general Digest 3 Jan 2013 03:17:46 -0000 Issue 8084

2013-01-02 Thread php-general-digest-help

php-general Digest 3 Jan 2013 03:17:46 - Issue 8084

Topics (messages 319993 through 320002):

Boolean type forced on string assignment inside if statement
319993 by: Marc Guay
319994 by: Marc Guay
319995 by: Stephen
319996 by: Tim Streater
319997 by: Jim Lucas
319998 by: Marc Guay
31 by: Jim Lucas
32 by: Marc Guay
320001 by: Marc Guay
320002 by: tamouse mailing lists

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 ---
Hi folks,

if ($a = "foo" && $b = "bar"){
echo $a."".$b;
}

Returns:
1
bar

I expect:
foo
bar

Is this documented?

Marc
--- End Message ---
--- Begin Message ---
Interesting that if I wrap them up tighter in braces I get the desires result..

if (($a = "foo") && ($b = "bar")){
echo $a."".$b;
}

Returns
foo
bar
--- End Message ---
--- Begin Message ---

On 13-01-02 10:53 AM, Marc Guay wrote:

Hi folks,

if ($a = "foo" && $b = "bar"){
 echo $a."".$b;
}

Returns:
1
bar

I expect:
foo
bar

Is this documented?



&& takes precedence over =

http://php.net/manual/en/language.operators.precedence.php

You may want to use brackets

--
Stephen

--- End Message ---
--- Begin Message ---
On 02 Jan 2013 at 16:05, Stephen  wrote: 

> On 13-01-02 10:53 AM, Marc Guay wrote:
>> Hi folks,
>>
>> if ($a = "foo" && $b = "bar"){
>>  echo $a."".$b;
>> }
>>
>> Returns:
>> 1
>> bar
>>
>> I expect:
>> foo
>> bar
>>
>> Is this documented?
>>
>
> && takes precedence over =
>
> http://php.net/manual/en/language.operators.precedence.php
>
> You may want to use brackets

OP may want to avoid doing something unusual which may confuse a casual reader.

--
Cheers  --  Tim
--- End Message ---
--- Begin Message ---

On 01/02/2013 07:53 AM, Marc Guay wrote:

Hi folks,

if ($a = "foo"&&  $b = "bar"){
 echo $a."".$b;
}

Returns:
1
bar

I expect:
foo
bar

Is this documented?

Marc



Why would you do this.

I cannot envision a time or condition that would require such a "test".

Can you please explain why you would want to do this?

Won't this type of condition/test always return true?

--
Jim Lucas

http://www.cmsws.com/
http://www.cmsws.com/examples/
--- End Message ---
--- Begin Message ---
> Won't this type of condition/test always return true?

I simplified the example.  In my real life case the "foo" and "bar"
are variables which can be empty strings, and if they both are not, I
would like to display them.

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

On 01/02/2013 10:21 AM, Marc Guay wrote:

Won't this type of condition/test always return true?


I simplified the example.  In my real life case the "foo" and "bar"
are variables which can be empty strings, and if they both are not, I
would like to display them.

Marc



Then why not use empty()?

$a = "foo";
$b = "bar";
if ( !empty($a) && !empty($b) ){
echo $a."".$b;
}

The reason I ask is that what if one of your strings was a "0" (zero) I 
would expect your code would not work as you had intended.


php -r 'if ( $a="foo" && $b="0" ) { echo "\n\n{$a}\n{$b}\n\n"; }'

In my testing, it does not.  I would then have to ask, how often do you 
think a string will be "0"?


--
Jim Lucas

http://www.cmsws.com/
http://www.cmsws.com/examples/
--- End Message ---
--- Begin Message ---
Thanks for the head's up.  In this case the string will be an English
sentence when it's populated properly so that won't be a concern.

Marc
--- End Message ---
--- Begin Message ---
Something else that's happening with this, which makes it a Bad Idea
(tm) is that when the operator is "or", as it is in my real life
scenerio, the 2nd variable occasionally doesn't get populated if the
first one returns true.

if ($a = "foo" || $b = "bar"){
echo $a."".$b;
}

Returns
foo

And even worse, because I have this in a loop, what can happen is that
if $b gets populated on one loop, it doesn't get reset for the next
one so the data gets seriously bungled.

Moral of the story:  Don't be so fancy on your first day back after
vacation.  :)

Marc
--- End Message ---
--- Begin Message ---
On Wed, Jan 2, 2013 at 1:02 PM, Marc Guay  wrote:
> Something else that's happening with this, which makes it a Bad Idea
> (tm) is that when the operator is "or", as it is in my real life
> scenerio, the 2nd variable occasionally doesn't get populated if the
> first one returns true.
>
> if ($a = "foo" || $b = "bar"){
> echo $a."".$b;
> }
>
> Returns
> foo
>
> And even worse, because I have this in a loop, what can happen is that
> if $b gets populated on one loop, it doesn't get reset for the next
> one so the data gets seriously bungled.

Ah, yeah, seriously, don't mix assignments and tests up this way
unles

php-general Digest 2 Jan 2013 12:31:56 -0000 Issue 8083

2013-01-02 Thread php-general-digest-help

php-general Digest 2 Jan 2013 12:31:56 - Issue 8083

Topics (messages 319992 through 319992):

Is there any difference in assigning variable to itself after some actions on 
it or to another variable?
319992 by: Vyacheslav Kondratyuk

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 ---
Hi,

Not long ago I had a debates about assignment variables to itself after
some action on them.

Assign variable to itself [AI]:
echo memory_get_usage() . "\n"; // 645680
$repeatedString = str_repeat('Hello,', 1);
echo memory_get_usage() . "\n"; // 705944, AI_delta1 = 60264
$repeatedString = explode(',', $repeatedString);
echo memory_get_usage() . "\n"; // 3337888, AI_delta2 = 2631944
echo memory_get_peak_usage() . "\n"; // AI_peak = 3401024

Assign variable to another variable [AAV]:
echo memory_get_usage() . "\n"; // 645752
$repeatedString = str_repeat('Hello,', 1);
echo memory_get_usage() . "\n"; // 706024, AAV_delta1 = 60272
$explodedString = explode(',', $repeatedString);
echo memory_get_usage() . "\n"; // 3398256, AAV_delta2 = 2692232
echo memory_get_peak_usage() . "\n"; // AAV_peak = 3400984

I created tests of memory usage in both cases and subtract values:
AAV_delta1 - AI_delta = 8
AAV_delta2 - AI_delta2 = 60288
AAV_peak - AI_peak = -40

According to this results it doesn't matter which approach to use, memory
usage - same. It's only a question should I use variable $repeatedString at
my code below or not.

Am I right at my conclusions or my tests are not correct?
Why it happens this way?
Also a question: AAV_delta1 - AI_delta = 8, I expect it should be equal 0.
Why it's equals 8?

Note: Memory usage can vary on your system.
PHP Version: 5.3.5-1ubuntu7.11.
--- End Message ---