Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

valore restituito al posto modificare il valore della variabile

13 views
Skip to first unread message

alex

unread,
Oct 16, 2020, 7:04:03 AM10/16/20
to
// DESIGN 1
echo "Non funziona, ma mi piace il 'DESIGN 1' ";
$arr = [1, 2];
print_r(
array_shift($arr)
);

// DESIGN 2
echo "Funziona, ma non mi piace il 'DESIGN 2': ";
$arr = [1, 2];
array_shift($arr);
print_r(
$arr
);

Output:

Non funziona, ma mi piace il 'DESIGN 1': 1

Funziona, ma non mi piace il 'DESIGN 2': Array
(
[0] => 2
)

Per trovare un compromesso mi son creato questa classe:

<?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

Ma come vedete qualcosa non funziona: il motivo lo lascio immaginare.

Qualche idea?

alex

unread,
Oct 16, 2020, 7:19:48 AM10/16/20
to
Il 16/10/20 13:03, alex ha scritto:
Precisazione:
Non funziona: cioè viene restituto 1 al posto dell'array.

massiws

unread,
Jul 20, 2023, 11:48:29 PM7/20/23
to
https://www.php.net/manual/en/function.print-r.php

"If you would like to capture the output of print_r(), use the return parameter. When this parameter is set to true, print_r() will return the information rather than print it."
0 new messages