Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #18386 > unrolled thread

value returned instead change the value of the variable

Started byalex <1j9448a02@lnx159sneakemail.com.invalid>
First post2020-10-16 13:23 +0200
Last post2020-10-17 10:16 +0200
Articles 6 — 3 participants

Back to article view | Back to comp.lang.php


Contents

  value returned instead change the value of the variable alex <1j9448a02@lnx159sneakemail.com.invalid> - 2020-10-16 13:23 +0200
    Re: value returned instead change the value of the variable Jerry Stuckle <jstucklex@attglobal.net> - 2020-10-16 11:11 -0400
      Re: value returned instead change the value of the variable alex <1j9448a02@lnx159sneakemail.com.invalid> - 2020-10-16 18:36 +0200
    Re: value returned instead change the value of the variable "J.O. Aho" <user@example.net> - 2020-10-16 20:51 +0200
      Re: value returned instead change the value of the variable alex <1j9448a02@lnx159sneakemail.com.invalid> - 2020-10-17 09:26 +0200
        Re: value returned instead change the value of the variable "J.O. Aho" <user@example.net> - 2020-10-17 10:16 +0200

#18386 — value returned instead change the value of the variable

Fromalex <1j9448a02@lnx159sneakemail.com.invalid>
Date2020-10-16 13:23 +0200
Subjectvalue returned instead change the value of the variable
Message-ID<rmbvqm$lnh$1@gioia.aioe.org>
// DESIGN 1
echo "It doesn't work (return 1 vs array), but I like 'DESIGN 1' ";
$arr = [1, 2];
print_r(
     array_shift($arr)
);

// DESIGN 2
echo "It works, but I don't like 'DESIGN 2': ";
$arr = [1, 2];
array_shift($arr);
print_r(
     $arr
);

Output:

It doesn't work (return 1 vs array), but I like 'DESIGN 1': 1

It works, but I don't like 'DESIGN 2': Array
(
     [0] => 2
)

To find a compromise I created this class:

<?php
class FirstProcessedArgumentReturn {
     static function __callStatic(string $name, array $arguments) {
         $name($arguments);

         return $arguments[0];
     }
}

FirstProcessedArgumentReturn::array_shift([1, 2]);
//TODO: Test the method FirstProcessedArgumentReturn::array_push(...);
//TODO: Test the method FirstProcessedArgumentReturn::array_pop(...);
//TODO: Test the method ...

PHP Notice:  Undefined offset: 0 in /tmp/test.php on line 6

But as you can see, something does not work: the reason I leave you to 
imagine.

Some idea?

[toc] | [next] | [standalone]


#18387

FromJerry Stuckle <jstucklex@attglobal.net>
Date2020-10-16 11:11 -0400
Message-ID<rmcd7a$qh7$1@jstuckle.eternal-september.org>
In reply to#18386
On 10/16/2020 7:23 AM, alex wrote:
> // DESIGN 1
> echo "It doesn't work (return 1 vs array), but I like 'DESIGN 1' ";
> $arr = [1, 2];
> print_r(
>      array_shift($arr)
> );
> 
> // DESIGN 2
> echo "It works, but I don't like 'DESIGN 2': ";
> $arr = [1, 2];
> array_shift($arr);
> print_r(
>      $arr
> );
> 
> Output:
> 
> It doesn't work (return 1 vs array), but I like 'DESIGN 1': 1
> 
> It works, but I don't like 'DESIGN 2': Array
> (
>      [0] => 2
> )
> 
> To find a compromise I created this class:
> 
> <?php
> class FirstProcessedArgumentReturn {
>      static function __callStatic(string $name, array $arguments) {
>          $name($arguments);
> 
>          return $arguments[0];
>      }
> }
> 
> FirstProcessedArgumentReturn::array_shift([1, 2]);
> //TODO: Test the method FirstProcessedArgumentReturn::array_push(...);
> //TODO: Test the method FirstProcessedArgumentReturn::array_pop(...);
> //TODO: Test the method ...
> 
> PHP Notice:  Undefined offset: 0 in /tmp/test.php on line 6
> 
> But as you can see, something does not work: the reason I leave you to 
> imagine.
> 
> Some idea?

It's working fine - but you're doing two different things. 
array_shift() shifts the array elements one position left - AND RETURNS 
THE SHIFTED VALUE.  So your first design is working properly.

In your second design you are discarding the returned value and printing 
the array.  So it, also, is working fine.

Your static function is failing because you're not passing an array. 
You are passing the returned value from array_shift().

-- 
==================
Remove the "x" from my email address
Jerry Stuckle
jstucklex@attglobal.net
==================

[toc] | [prev] | [next] | [standalone]


#18388

Fromalex <1j9448a02@lnx159sneakemail.com.invalid>
Date2020-10-16 18:36 +0200
Message-ID<rmci6b$1lco$1@gioia.aioe.org>
In reply to#18387
Il 16/10/20 17:11, Jerry Stuckle ha scritto:
> On 10/16/2020 7:23 AM, alex wrote:
>> // DESIGN 1
>> echo "It doesn't work (return 1 vs array), but I like 'DESIGN 1' ";
>> $arr = [1, 2];
>> print_r(
>>      array_shift($arr)
>> );
>>
>> // DESIGN 2
>> echo "It works, but I don't like 'DESIGN 2': ";
>> $arr = [1, 2];
>> array_shift($arr);
>> print_r(
>>      $arr
>> );
>>
>> Output:
>>
>> It doesn't work (return 1 vs array), but I like 'DESIGN 1': 1
>>
>> It works, but I don't like 'DESIGN 2': Array
>> (
>>      [0] => 2
>> )
>>
>> To find a compromise I created this class:
>>
>> <?php
>> class FirstProcessedArgumentReturn {
>>      static function __callStatic(string $name, array $arguments) {
>>          $name($arguments);
>>
>>          return $arguments[0];
>>      }
>> }
>>
>> FirstProcessedArgumentReturn::array_shift([1, 2]);
>> //TODO: Test the method FirstProcessedArgumentReturn::array_push(...);
>> //TODO: Test the method FirstProcessedArgumentReturn::array_pop(...);
>> //TODO: Test the method ...
>>
>> PHP Notice:  Undefined offset: 0 in /tmp/test.php on line 6
>>
>> But as you can see, something does not work: the reason I leave you to 
>> imagine.
>>
>> Some idea?
> 
> It's working fine - but you're doing two different things. array_shift() 
> shifts the array elements one position left - AND RETURNS THE SHIFTED 
> VALUE.  So your first design is working properly.
> 
> In your second design you are discarding the returned value and printing 
> the array.  So it, also, is working fine.
> 
> Your static function is failing because you're not passing an array. You 
> are passing the returned value from array_shift().
> 

final class FirstProcessedArgumentReturn {
     static function __callStatic(string $name, array $arguments) {
         $name($arguments[0]);

         return $arguments[0];
     }
}

// OK
print_r(
     FirstProcessedArgumentReturn::array_shift(
         [1, 2]
     )
);

// MISSING 3
print_r(
     FirstProcessedArgumentReturn::array_push(
         [1, 2],
         3
     )
);

Output:

Array
(
     [0] => 2
)
Array
(
     [0] => 1
     [1] => 2
)

[toc] | [prev] | [next] | [standalone]


#18389

From"J.O. Aho" <user@example.net>
Date2020-10-16 20:51 +0200
Message-ID<huu8d0Ft5ceU1@mid.individual.net>
In reply to#18386
On 16/10/2020 13.23, alex wrote:
> // DESIGN 1
> echo "It doesn't work (return 1 vs array), but I like 'DESIGN 1' ";
> $arr = [1, 2];
> print_r(
>      array_shift($arr)
> );
> 
 > Output:
 >
 > It doesn't work (return 1 vs array), but I like 'DESIGN 1': 1
 >

If you look at your original array, the cell 0 has an integer, so the 
return type will be int. See https://www.php.net/array_shift it say's:

	Return Values
		Returns the shifted value, or NULL if array is empty or
		is not an array.

If you prompt want an array, either you make it to one afterwards or 
store the values as single cell arrays in the main array

Alt 1:
	array(array_shit($arr));

Alt 2: $arr = [[1],[2]];



> // DESIGN 2
> echo "It works, but I don't like 'DESIGN 2': ";
> $arr = [1, 2];
> array_shift($arr);
> print_r(
>      $arr
> );
> 
> Output:
> 
> It works, but I don't like 'DESIGN 2': Array
> (
>      [0] => 2
> )

Sure we compare apples and oranges, the $arr will be an array even if 
you shift out the last value from it.

try this:

  $arr = [1, 2];
  array_shift($arr);
  print_r($arr);
  array_shift($arr);
  print_r($arr);
  echo "This will confuse you even more\n";
  print_r(array_shift($arr));


-- 
  //Aho


[toc] | [prev] | [next] | [standalone]


#18390

Fromalex <1j9448a02@lnx159sneakemail.com.invalid>
Date2020-10-17 09:26 +0200
Message-ID<rme6c1$g04$1@gioia.aioe.org>
In reply to#18389
Il 16/10/20 20:51, J.O. Aho ha scritto:
> If you prompt want an array, either you make it to one afterwards or 
> store the values as single cell arrays in the main array
> 
> Alt 1:
>      array(array_shit($arr));
> 
> Alt 2: $arr = [[1],[2]];

????
But what do I need?

>> // DESIGN 2
>> echo "It works, but I don't like 'DESIGN 2': ";
>> $arr = [1, 2];
>> array_shift($arr);
>> print_r(
>>      $arr
>> );
>>
>> Output:
>>
>> It works, but I don't like 'DESIGN 2': Array
>> (
>>      [0] => 2
>> )
> 
> Sure we compare apples and oranges, the $arr will be an array even if 
> you shift out the last value from it.
> 
> try this:
> 
>   $arr = [1, 2];
>   array_shift($arr);
>   print_r($arr);
>   array_shift($arr);
>   print_r($arr);
>   echo "This will confuse you even more\n";
>   print_r(array_shift($arr));
> 
> 

So what?

[toc] | [prev] | [next] | [standalone]


#18391

From"J.O. Aho" <user@example.net>
Date2020-10-17 10:16 +0200
Message-ID<huvnj7F7pb4U1@mid.individual.net>
In reply to#18390
On 17/10/2020 09.26, alex wrote:
> Il 16/10/20 20:51, J.O. Aho ha scritto:
>> If you prompt want an array, either you make it to one afterwards or 
>> store the values as single cell arrays in the main array
>>
>> Alt 1:
>>      array(array_shit($arr));
>>
>> Alt 2: $arr = [[1],[2]];
> 
> ????
> But what do I need?

I would say you don't need either, just fix your code to take care of 
the integer instead of thinking of having an array.

-- 

  //Aho

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.php


csiph-web