Strict Standards: Only variables should be passed by reference

Multi tool use
$el = array_shift($instance->find(..))
The above code somehow reports the strict standars warning,but this will not:
function get_arr(){
return array(1,2);
}
$el = array_shift(get_arr());
So when will it report the warning anyway?
php reference strict
add a comment |
$el = array_shift($instance->find(..))
The above code somehow reports the strict standars warning,but this will not:
function get_arr(){
return array(1,2);
}
$el = array_shift(get_arr());
So when will it report the warning anyway?
php reference strict
1
What does $instance->find(..) return?
– Silver Light
Mar 1 '10 at 8:44
2
Here is the solution: stackoverflow.com/questions/9848295/…
– ajaristi
Mar 7 '13 at 22:09
I think the examples (or logic) might be the wrong way round in the question, since the 2nd example (get_arr()
function) does produce the strict standards notice (tested PHP 5.2 and PHP 5.5).
– MrWhite
May 1 '14 at 0:03
add a comment |
$el = array_shift($instance->find(..))
The above code somehow reports the strict standars warning,but this will not:
function get_arr(){
return array(1,2);
}
$el = array_shift(get_arr());
So when will it report the warning anyway?
php reference strict
$el = array_shift($instance->find(..))
The above code somehow reports the strict standars warning,but this will not:
function get_arr(){
return array(1,2);
}
$el = array_shift(get_arr());
So when will it report the warning anyway?
php reference strict
php reference strict
edited Jun 26 '14 at 19:39
user2864740
43.7k670148
43.7k670148
asked Mar 1 '10 at 8:35
user198729user198729
22.6k91222322
22.6k91222322
1
What does $instance->find(..) return?
– Silver Light
Mar 1 '10 at 8:44
2
Here is the solution: stackoverflow.com/questions/9848295/…
– ajaristi
Mar 7 '13 at 22:09
I think the examples (or logic) might be the wrong way round in the question, since the 2nd example (get_arr()
function) does produce the strict standards notice (tested PHP 5.2 and PHP 5.5).
– MrWhite
May 1 '14 at 0:03
add a comment |
1
What does $instance->find(..) return?
– Silver Light
Mar 1 '10 at 8:44
2
Here is the solution: stackoverflow.com/questions/9848295/…
– ajaristi
Mar 7 '13 at 22:09
I think the examples (or logic) might be the wrong way round in the question, since the 2nd example (get_arr()
function) does produce the strict standards notice (tested PHP 5.2 and PHP 5.5).
– MrWhite
May 1 '14 at 0:03
1
1
What does $instance->find(..) return?
– Silver Light
Mar 1 '10 at 8:44
What does $instance->find(..) return?
– Silver Light
Mar 1 '10 at 8:44
2
2
Here is the solution: stackoverflow.com/questions/9848295/…
– ajaristi
Mar 7 '13 at 22:09
Here is the solution: stackoverflow.com/questions/9848295/…
– ajaristi
Mar 7 '13 at 22:09
I think the examples (or logic) might be the wrong way round in the question, since the 2nd example (
get_arr()
function) does produce the strict standards notice (tested PHP 5.2 and PHP 5.5).– MrWhite
May 1 '14 at 0:03
I think the examples (or logic) might be the wrong way round in the question, since the 2nd example (
get_arr()
function) does produce the strict standards notice (tested PHP 5.2 and PHP 5.5).– MrWhite
May 1 '14 at 0:03
add a comment |
6 Answers
6
active
oldest
votes
Consider the following code:
error_reporting(E_STRICT);
class test {
function test_arr(&$a) {
var_dump($a);
}
function get_arr() {
return array(1,2);
}
}
$t= new test;
$t->test_arr($t->get_arr());
This will generate the following output:
Strict Standards: Only variables should be passed by reference in `test.php` on line 14
array(2) {
[0]=>
int(1)
[1]=>
int(2)
}
The reason? The test::get_arr()
method is not a variable and under strict mode this will generate a warning. This behavior is extremely non-intuitive as the get_arr()
method returns an array value.
To get around this error in strict mode either change the signature of the method so it doesn't use a reference:
function test_arr($a) {
var_dump($a);
}
Since you can't change the signature of array_shift
you can also use an intermediate variable:
$inter= get_arr();
$el= array_shift($inter);
4
Seems there is no way to avoid using$inter
..
– user198729
Mar 1 '10 at 10:13
7
@user198729: I was looking for an explanation or fix too, and found you can use current() for the first item. Alas end() doesn't work for the last since it "advances the internal pointer to the last element". current(array_reverse(somefunction())) works (yes, it's silly)
– MSpreij
Oct 11 '11 at 9:27
2
It is very silly.
– lord_t
Jun 1 '12 at 8:24
1
Usingcurrent
makes the assumption that the array pointer is at the first element. It may be a valid assumption in most cases, but one to watch out for.
– cmbuckley
Aug 3 '13 at 12:58
1
@leepowers Of course, then there'd be the same issue asarray_shift()
in that it expects a reference to modify :-)
– cmbuckley
Aug 5 '13 at 8:45
|
show 4 more comments
$instance->find()
returns reference to variable.
You get the report when you are trying to use this reference as an argument to function,without storing it at variable first.
This helps preventing memory leaks, and will probably become error in next PHP versions.
Your 2nd code would throw error if it wrote like (note the & in function signature):
function &get_arr(){
return array(1,2);
}
$el = array_shift(get_arr());
So a quick (and not so nice) fix would be:
$el = array_shift($tmp = $instance->find(..));
Basically you do an assignment to temporary variable first, and send the variable as an argument.
1
I've tried it previously,not working
– user198729
Mar 1 '10 at 9:05
It should work now (checked it). In order to return reference you have to declare it at method signature, not return statement (my fault).
– Sagi
Mar 1 '10 at 9:10
No,I can't change the signature.@pygorex1's intermediate variable can solve this,but it looks redundant,doesn't it?
– user198729
Mar 1 '10 at 9:29
4
I tried your second snippet,not working.It only works in a separate line
– user198729
Mar 1 '10 at 11:27
2
Indeed. An assignment returns the assigned value.array_shift($tmp = $instance->find(..))
assigns the value of$instance->find(..)
to$tmp
and then passes the value of the assignment toarray_shift()
-- which is not the same thing as passing$tmp
itself, so is no better than the original situation without the assignment.
– phils
Feb 12 '14 at 1:28
|
show 1 more comment
The cause of the error is the use of the internal PHP Programming Data Structures function, array_shift() [ php.net/end ].
The function takes an array as a parameter. Although an ampersand is indicated in the prototype of array_shift()
in The Manual", there is no cautionary documentation following in the extended definition of that function, nor is there any apparent explanation that the parameter is in fact passed by reference.
Perhaps this is /understood/. I did not understand, however, so it was difficult for me to detect the cause of the error.
Reproduce code:
function get_arr()
{
return array(1,2);
}
$array = get_arr();
$el = array_shift($array);
add a comment |
The second snippet doesn't work either and that's why. array_shift
is a modifier function, that changes its argument, therefore it expects its parameter to be a reference, and you cannot reference something that is not a variable. See Rasmus' explanations here: Strict standards: Only variables should be passed by reference
add a comment |
This code:
$monthly_index = array_shift(unpack('H*', date('m/Y')));
Need to be changed into:
$date_time = date('m/Y');
$unpack = unpack('H*', $date_time);
array_shift($unpack);
add a comment |
Well, in obvious cases like that, you can always tell PHP to suppress messages by using "@" in front of the function.
$monthly_index = @array_shift(unpack('H*', date('m/Y')));
It may not be one of the best programming practices to suppress all errors this way, but in certain cases (like this one) it comes handy and is acceptable.
As result, I am sure your friend SysAdmin will be pleased with a less polluted "error.log". ;)
I don't know who downvoted this answer, but the presented solution DOES work and it IS a PHP standard technique. Really disappointing... Next time I might no answer a question anymore... :(
– Julio Marchi
Dec 17 '16 at 1:36
4
I would assume it was because suppressing the error message does not fix the problem with the code. What will you do when this type of error changes from E_STRICT to E_ERROR in a future PHP release and your code now does not run, and also does not produce any errors/output?
– Luke
Mar 6 '17 at 22:42
@TinoDidriksen, I understand and agree with the reasons to advise against some "bad habits", especially for the new generations. However, a resource exists to be used when (and if) it is safe to use and applicable to the proposed context. If the error suppressor "@" was to be abolished, it would have been removed from the language itself. Same as "eval" (it may be evil, but it has its purposes). What I am against is not about the usage of some resources but the generalization of an advice. In specific for the proposed case, it wouldn't be of any harm to use it, not even for debugging purposes.
– Julio Marchi
Aug 29 '17 at 3:55
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f2354609%2fstrict-standards-only-variables-should-be-passed-by-reference%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
6 Answers
6
active
oldest
votes
6 Answers
6
active
oldest
votes
active
oldest
votes
active
oldest
votes
Consider the following code:
error_reporting(E_STRICT);
class test {
function test_arr(&$a) {
var_dump($a);
}
function get_arr() {
return array(1,2);
}
}
$t= new test;
$t->test_arr($t->get_arr());
This will generate the following output:
Strict Standards: Only variables should be passed by reference in `test.php` on line 14
array(2) {
[0]=>
int(1)
[1]=>
int(2)
}
The reason? The test::get_arr()
method is not a variable and under strict mode this will generate a warning. This behavior is extremely non-intuitive as the get_arr()
method returns an array value.
To get around this error in strict mode either change the signature of the method so it doesn't use a reference:
function test_arr($a) {
var_dump($a);
}
Since you can't change the signature of array_shift
you can also use an intermediate variable:
$inter= get_arr();
$el= array_shift($inter);
4
Seems there is no way to avoid using$inter
..
– user198729
Mar 1 '10 at 10:13
7
@user198729: I was looking for an explanation or fix too, and found you can use current() for the first item. Alas end() doesn't work for the last since it "advances the internal pointer to the last element". current(array_reverse(somefunction())) works (yes, it's silly)
– MSpreij
Oct 11 '11 at 9:27
2
It is very silly.
– lord_t
Jun 1 '12 at 8:24
1
Usingcurrent
makes the assumption that the array pointer is at the first element. It may be a valid assumption in most cases, but one to watch out for.
– cmbuckley
Aug 3 '13 at 12:58
1
@leepowers Of course, then there'd be the same issue asarray_shift()
in that it expects a reference to modify :-)
– cmbuckley
Aug 5 '13 at 8:45
|
show 4 more comments
Consider the following code:
error_reporting(E_STRICT);
class test {
function test_arr(&$a) {
var_dump($a);
}
function get_arr() {
return array(1,2);
}
}
$t= new test;
$t->test_arr($t->get_arr());
This will generate the following output:
Strict Standards: Only variables should be passed by reference in `test.php` on line 14
array(2) {
[0]=>
int(1)
[1]=>
int(2)
}
The reason? The test::get_arr()
method is not a variable and under strict mode this will generate a warning. This behavior is extremely non-intuitive as the get_arr()
method returns an array value.
To get around this error in strict mode either change the signature of the method so it doesn't use a reference:
function test_arr($a) {
var_dump($a);
}
Since you can't change the signature of array_shift
you can also use an intermediate variable:
$inter= get_arr();
$el= array_shift($inter);
4
Seems there is no way to avoid using$inter
..
– user198729
Mar 1 '10 at 10:13
7
@user198729: I was looking for an explanation or fix too, and found you can use current() for the first item. Alas end() doesn't work for the last since it "advances the internal pointer to the last element". current(array_reverse(somefunction())) works (yes, it's silly)
– MSpreij
Oct 11 '11 at 9:27
2
It is very silly.
– lord_t
Jun 1 '12 at 8:24
1
Usingcurrent
makes the assumption that the array pointer is at the first element. It may be a valid assumption in most cases, but one to watch out for.
– cmbuckley
Aug 3 '13 at 12:58
1
@leepowers Of course, then there'd be the same issue asarray_shift()
in that it expects a reference to modify :-)
– cmbuckley
Aug 5 '13 at 8:45
|
show 4 more comments
Consider the following code:
error_reporting(E_STRICT);
class test {
function test_arr(&$a) {
var_dump($a);
}
function get_arr() {
return array(1,2);
}
}
$t= new test;
$t->test_arr($t->get_arr());
This will generate the following output:
Strict Standards: Only variables should be passed by reference in `test.php` on line 14
array(2) {
[0]=>
int(1)
[1]=>
int(2)
}
The reason? The test::get_arr()
method is not a variable and under strict mode this will generate a warning. This behavior is extremely non-intuitive as the get_arr()
method returns an array value.
To get around this error in strict mode either change the signature of the method so it doesn't use a reference:
function test_arr($a) {
var_dump($a);
}
Since you can't change the signature of array_shift
you can also use an intermediate variable:
$inter= get_arr();
$el= array_shift($inter);
Consider the following code:
error_reporting(E_STRICT);
class test {
function test_arr(&$a) {
var_dump($a);
}
function get_arr() {
return array(1,2);
}
}
$t= new test;
$t->test_arr($t->get_arr());
This will generate the following output:
Strict Standards: Only variables should be passed by reference in `test.php` on line 14
array(2) {
[0]=>
int(1)
[1]=>
int(2)
}
The reason? The test::get_arr()
method is not a variable and under strict mode this will generate a warning. This behavior is extremely non-intuitive as the get_arr()
method returns an array value.
To get around this error in strict mode either change the signature of the method so it doesn't use a reference:
function test_arr($a) {
var_dump($a);
}
Since you can't change the signature of array_shift
you can also use an intermediate variable:
$inter= get_arr();
$el= array_shift($inter);
edited Dec 11 '18 at 9:57


Alive to Die
55.7k82869
55.7k82869
answered Mar 1 '10 at 9:14


leepowersleepowers
26.3k1985121
26.3k1985121
4
Seems there is no way to avoid using$inter
..
– user198729
Mar 1 '10 at 10:13
7
@user198729: I was looking for an explanation or fix too, and found you can use current() for the first item. Alas end() doesn't work for the last since it "advances the internal pointer to the last element". current(array_reverse(somefunction())) works (yes, it's silly)
– MSpreij
Oct 11 '11 at 9:27
2
It is very silly.
– lord_t
Jun 1 '12 at 8:24
1
Usingcurrent
makes the assumption that the array pointer is at the first element. It may be a valid assumption in most cases, but one to watch out for.
– cmbuckley
Aug 3 '13 at 12:58
1
@leepowers Of course, then there'd be the same issue asarray_shift()
in that it expects a reference to modify :-)
– cmbuckley
Aug 5 '13 at 8:45
|
show 4 more comments
4
Seems there is no way to avoid using$inter
..
– user198729
Mar 1 '10 at 10:13
7
@user198729: I was looking for an explanation or fix too, and found you can use current() for the first item. Alas end() doesn't work for the last since it "advances the internal pointer to the last element". current(array_reverse(somefunction())) works (yes, it's silly)
– MSpreij
Oct 11 '11 at 9:27
2
It is very silly.
– lord_t
Jun 1 '12 at 8:24
1
Usingcurrent
makes the assumption that the array pointer is at the first element. It may be a valid assumption in most cases, but one to watch out for.
– cmbuckley
Aug 3 '13 at 12:58
1
@leepowers Of course, then there'd be the same issue asarray_shift()
in that it expects a reference to modify :-)
– cmbuckley
Aug 5 '13 at 8:45
4
4
Seems there is no way to avoid using
$inter
..– user198729
Mar 1 '10 at 10:13
Seems there is no way to avoid using
$inter
..– user198729
Mar 1 '10 at 10:13
7
7
@user198729: I was looking for an explanation or fix too, and found you can use current() for the first item. Alas end() doesn't work for the last since it "advances the internal pointer to the last element". current(array_reverse(somefunction())) works (yes, it's silly)
– MSpreij
Oct 11 '11 at 9:27
@user198729: I was looking for an explanation or fix too, and found you can use current() for the first item. Alas end() doesn't work for the last since it "advances the internal pointer to the last element". current(array_reverse(somefunction())) works (yes, it's silly)
– MSpreij
Oct 11 '11 at 9:27
2
2
It is very silly.
– lord_t
Jun 1 '12 at 8:24
It is very silly.
– lord_t
Jun 1 '12 at 8:24
1
1
Using
current
makes the assumption that the array pointer is at the first element. It may be a valid assumption in most cases, but one to watch out for.– cmbuckley
Aug 3 '13 at 12:58
Using
current
makes the assumption that the array pointer is at the first element. It may be a valid assumption in most cases, but one to watch out for.– cmbuckley
Aug 3 '13 at 12:58
1
1
@leepowers Of course, then there'd be the same issue as
array_shift()
in that it expects a reference to modify :-)– cmbuckley
Aug 5 '13 at 8:45
@leepowers Of course, then there'd be the same issue as
array_shift()
in that it expects a reference to modify :-)– cmbuckley
Aug 5 '13 at 8:45
|
show 4 more comments
$instance->find()
returns reference to variable.
You get the report when you are trying to use this reference as an argument to function,without storing it at variable first.
This helps preventing memory leaks, and will probably become error in next PHP versions.
Your 2nd code would throw error if it wrote like (note the & in function signature):
function &get_arr(){
return array(1,2);
}
$el = array_shift(get_arr());
So a quick (and not so nice) fix would be:
$el = array_shift($tmp = $instance->find(..));
Basically you do an assignment to temporary variable first, and send the variable as an argument.
1
I've tried it previously,not working
– user198729
Mar 1 '10 at 9:05
It should work now (checked it). In order to return reference you have to declare it at method signature, not return statement (my fault).
– Sagi
Mar 1 '10 at 9:10
No,I can't change the signature.@pygorex1's intermediate variable can solve this,but it looks redundant,doesn't it?
– user198729
Mar 1 '10 at 9:29
4
I tried your second snippet,not working.It only works in a separate line
– user198729
Mar 1 '10 at 11:27
2
Indeed. An assignment returns the assigned value.array_shift($tmp = $instance->find(..))
assigns the value of$instance->find(..)
to$tmp
and then passes the value of the assignment toarray_shift()
-- which is not the same thing as passing$tmp
itself, so is no better than the original situation without the assignment.
– phils
Feb 12 '14 at 1:28
|
show 1 more comment
$instance->find()
returns reference to variable.
You get the report when you are trying to use this reference as an argument to function,without storing it at variable first.
This helps preventing memory leaks, and will probably become error in next PHP versions.
Your 2nd code would throw error if it wrote like (note the & in function signature):
function &get_arr(){
return array(1,2);
}
$el = array_shift(get_arr());
So a quick (and not so nice) fix would be:
$el = array_shift($tmp = $instance->find(..));
Basically you do an assignment to temporary variable first, and send the variable as an argument.
1
I've tried it previously,not working
– user198729
Mar 1 '10 at 9:05
It should work now (checked it). In order to return reference you have to declare it at method signature, not return statement (my fault).
– Sagi
Mar 1 '10 at 9:10
No,I can't change the signature.@pygorex1's intermediate variable can solve this,but it looks redundant,doesn't it?
– user198729
Mar 1 '10 at 9:29
4
I tried your second snippet,not working.It only works in a separate line
– user198729
Mar 1 '10 at 11:27
2
Indeed. An assignment returns the assigned value.array_shift($tmp = $instance->find(..))
assigns the value of$instance->find(..)
to$tmp
and then passes the value of the assignment toarray_shift()
-- which is not the same thing as passing$tmp
itself, so is no better than the original situation without the assignment.
– phils
Feb 12 '14 at 1:28
|
show 1 more comment
$instance->find()
returns reference to variable.
You get the report when you are trying to use this reference as an argument to function,without storing it at variable first.
This helps preventing memory leaks, and will probably become error in next PHP versions.
Your 2nd code would throw error if it wrote like (note the & in function signature):
function &get_arr(){
return array(1,2);
}
$el = array_shift(get_arr());
So a quick (and not so nice) fix would be:
$el = array_shift($tmp = $instance->find(..));
Basically you do an assignment to temporary variable first, and send the variable as an argument.
$instance->find()
returns reference to variable.
You get the report when you are trying to use this reference as an argument to function,without storing it at variable first.
This helps preventing memory leaks, and will probably become error in next PHP versions.
Your 2nd code would throw error if it wrote like (note the & in function signature):
function &get_arr(){
return array(1,2);
}
$el = array_shift(get_arr());
So a quick (and not so nice) fix would be:
$el = array_shift($tmp = $instance->find(..));
Basically you do an assignment to temporary variable first, and send the variable as an argument.
edited Dec 11 '18 at 9:57


Alive to Die
55.7k82869
55.7k82869
answered Mar 1 '10 at 9:00
SagiSagi
7,51832125
7,51832125
1
I've tried it previously,not working
– user198729
Mar 1 '10 at 9:05
It should work now (checked it). In order to return reference you have to declare it at method signature, not return statement (my fault).
– Sagi
Mar 1 '10 at 9:10
No,I can't change the signature.@pygorex1's intermediate variable can solve this,but it looks redundant,doesn't it?
– user198729
Mar 1 '10 at 9:29
4
I tried your second snippet,not working.It only works in a separate line
– user198729
Mar 1 '10 at 11:27
2
Indeed. An assignment returns the assigned value.array_shift($tmp = $instance->find(..))
assigns the value of$instance->find(..)
to$tmp
and then passes the value of the assignment toarray_shift()
-- which is not the same thing as passing$tmp
itself, so is no better than the original situation without the assignment.
– phils
Feb 12 '14 at 1:28
|
show 1 more comment
1
I've tried it previously,not working
– user198729
Mar 1 '10 at 9:05
It should work now (checked it). In order to return reference you have to declare it at method signature, not return statement (my fault).
– Sagi
Mar 1 '10 at 9:10
No,I can't change the signature.@pygorex1's intermediate variable can solve this,but it looks redundant,doesn't it?
– user198729
Mar 1 '10 at 9:29
4
I tried your second snippet,not working.It only works in a separate line
– user198729
Mar 1 '10 at 11:27
2
Indeed. An assignment returns the assigned value.array_shift($tmp = $instance->find(..))
assigns the value of$instance->find(..)
to$tmp
and then passes the value of the assignment toarray_shift()
-- which is not the same thing as passing$tmp
itself, so is no better than the original situation without the assignment.
– phils
Feb 12 '14 at 1:28
1
1
I've tried it previously,not working
– user198729
Mar 1 '10 at 9:05
I've tried it previously,not working
– user198729
Mar 1 '10 at 9:05
It should work now (checked it). In order to return reference you have to declare it at method signature, not return statement (my fault).
– Sagi
Mar 1 '10 at 9:10
It should work now (checked it). In order to return reference you have to declare it at method signature, not return statement (my fault).
– Sagi
Mar 1 '10 at 9:10
No,I can't change the signature.@pygorex1's intermediate variable can solve this,but it looks redundant,doesn't it?
– user198729
Mar 1 '10 at 9:29
No,I can't change the signature.@pygorex1's intermediate variable can solve this,but it looks redundant,doesn't it?
– user198729
Mar 1 '10 at 9:29
4
4
I tried your second snippet,not working.It only works in a separate line
– user198729
Mar 1 '10 at 11:27
I tried your second snippet,not working.It only works in a separate line
– user198729
Mar 1 '10 at 11:27
2
2
Indeed. An assignment returns the assigned value.
array_shift($tmp = $instance->find(..))
assigns the value of $instance->find(..)
to $tmp
and then passes the value of the assignment to array_shift()
-- which is not the same thing as passing $tmp
itself, so is no better than the original situation without the assignment.– phils
Feb 12 '14 at 1:28
Indeed. An assignment returns the assigned value.
array_shift($tmp = $instance->find(..))
assigns the value of $instance->find(..)
to $tmp
and then passes the value of the assignment to array_shift()
-- which is not the same thing as passing $tmp
itself, so is no better than the original situation without the assignment.– phils
Feb 12 '14 at 1:28
|
show 1 more comment
The cause of the error is the use of the internal PHP Programming Data Structures function, array_shift() [ php.net/end ].
The function takes an array as a parameter. Although an ampersand is indicated in the prototype of array_shift()
in The Manual", there is no cautionary documentation following in the extended definition of that function, nor is there any apparent explanation that the parameter is in fact passed by reference.
Perhaps this is /understood/. I did not understand, however, so it was difficult for me to detect the cause of the error.
Reproduce code:
function get_arr()
{
return array(1,2);
}
$array = get_arr();
$el = array_shift($array);
add a comment |
The cause of the error is the use of the internal PHP Programming Data Structures function, array_shift() [ php.net/end ].
The function takes an array as a parameter. Although an ampersand is indicated in the prototype of array_shift()
in The Manual", there is no cautionary documentation following in the extended definition of that function, nor is there any apparent explanation that the parameter is in fact passed by reference.
Perhaps this is /understood/. I did not understand, however, so it was difficult for me to detect the cause of the error.
Reproduce code:
function get_arr()
{
return array(1,2);
}
$array = get_arr();
$el = array_shift($array);
add a comment |
The cause of the error is the use of the internal PHP Programming Data Structures function, array_shift() [ php.net/end ].
The function takes an array as a parameter. Although an ampersand is indicated in the prototype of array_shift()
in The Manual", there is no cautionary documentation following in the extended definition of that function, nor is there any apparent explanation that the parameter is in fact passed by reference.
Perhaps this is /understood/. I did not understand, however, so it was difficult for me to detect the cause of the error.
Reproduce code:
function get_arr()
{
return array(1,2);
}
$array = get_arr();
$el = array_shift($array);
The cause of the error is the use of the internal PHP Programming Data Structures function, array_shift() [ php.net/end ].
The function takes an array as a parameter. Although an ampersand is indicated in the prototype of array_shift()
in The Manual", there is no cautionary documentation following in the extended definition of that function, nor is there any apparent explanation that the parameter is in fact passed by reference.
Perhaps this is /understood/. I did not understand, however, so it was difficult for me to detect the cause of the error.
Reproduce code:
function get_arr()
{
return array(1,2);
}
$array = get_arr();
$el = array_shift($array);
edited Dec 11 '18 at 9:56


Alive to Die
55.7k82869
55.7k82869
answered Nov 25 '14 at 12:16


Biju B AdoorBiju B Adoor
41446
41446
add a comment |
add a comment |
The second snippet doesn't work either and that's why. array_shift
is a modifier function, that changes its argument, therefore it expects its parameter to be a reference, and you cannot reference something that is not a variable. See Rasmus' explanations here: Strict standards: Only variables should be passed by reference
add a comment |
The second snippet doesn't work either and that's why. array_shift
is a modifier function, that changes its argument, therefore it expects its parameter to be a reference, and you cannot reference something that is not a variable. See Rasmus' explanations here: Strict standards: Only variables should be passed by reference
add a comment |
The second snippet doesn't work either and that's why. array_shift
is a modifier function, that changes its argument, therefore it expects its parameter to be a reference, and you cannot reference something that is not a variable. See Rasmus' explanations here: Strict standards: Only variables should be passed by reference
The second snippet doesn't work either and that's why. array_shift
is a modifier function, that changes its argument, therefore it expects its parameter to be a reference, and you cannot reference something that is not a variable. See Rasmus' explanations here: Strict standards: Only variables should be passed by reference
edited Dec 11 '18 at 9:55


Alive to Die
55.7k82869
55.7k82869
answered Mar 1 '10 at 9:24
user187291user187291
45.3k1678126
45.3k1678126
add a comment |
add a comment |
This code:
$monthly_index = array_shift(unpack('H*', date('m/Y')));
Need to be changed into:
$date_time = date('m/Y');
$unpack = unpack('H*', $date_time);
array_shift($unpack);
add a comment |
This code:
$monthly_index = array_shift(unpack('H*', date('m/Y')));
Need to be changed into:
$date_time = date('m/Y');
$unpack = unpack('H*', $date_time);
array_shift($unpack);
add a comment |
This code:
$monthly_index = array_shift(unpack('H*', date('m/Y')));
Need to be changed into:
$date_time = date('m/Y');
$unpack = unpack('H*', $date_time);
array_shift($unpack);
This code:
$monthly_index = array_shift(unpack('H*', date('m/Y')));
Need to be changed into:
$date_time = date('m/Y');
$unpack = unpack('H*', $date_time);
array_shift($unpack);
edited Dec 11 '18 at 9:55


Alive to Die
55.7k82869
55.7k82869
answered Mar 7 '16 at 21:36
user6031348user6031348
311
311
add a comment |
add a comment |
Well, in obvious cases like that, you can always tell PHP to suppress messages by using "@" in front of the function.
$monthly_index = @array_shift(unpack('H*', date('m/Y')));
It may not be one of the best programming practices to suppress all errors this way, but in certain cases (like this one) it comes handy and is acceptable.
As result, I am sure your friend SysAdmin will be pleased with a less polluted "error.log". ;)
I don't know who downvoted this answer, but the presented solution DOES work and it IS a PHP standard technique. Really disappointing... Next time I might no answer a question anymore... :(
– Julio Marchi
Dec 17 '16 at 1:36
4
I would assume it was because suppressing the error message does not fix the problem with the code. What will you do when this type of error changes from E_STRICT to E_ERROR in a future PHP release and your code now does not run, and also does not produce any errors/output?
– Luke
Mar 6 '17 at 22:42
@TinoDidriksen, I understand and agree with the reasons to advise against some "bad habits", especially for the new generations. However, a resource exists to be used when (and if) it is safe to use and applicable to the proposed context. If the error suppressor "@" was to be abolished, it would have been removed from the language itself. Same as "eval" (it may be evil, but it has its purposes). What I am against is not about the usage of some resources but the generalization of an advice. In specific for the proposed case, it wouldn't be of any harm to use it, not even for debugging purposes.
– Julio Marchi
Aug 29 '17 at 3:55
add a comment |
Well, in obvious cases like that, you can always tell PHP to suppress messages by using "@" in front of the function.
$monthly_index = @array_shift(unpack('H*', date('m/Y')));
It may not be one of the best programming practices to suppress all errors this way, but in certain cases (like this one) it comes handy and is acceptable.
As result, I am sure your friend SysAdmin will be pleased with a less polluted "error.log". ;)
I don't know who downvoted this answer, but the presented solution DOES work and it IS a PHP standard technique. Really disappointing... Next time I might no answer a question anymore... :(
– Julio Marchi
Dec 17 '16 at 1:36
4
I would assume it was because suppressing the error message does not fix the problem with the code. What will you do when this type of error changes from E_STRICT to E_ERROR in a future PHP release and your code now does not run, and also does not produce any errors/output?
– Luke
Mar 6 '17 at 22:42
@TinoDidriksen, I understand and agree with the reasons to advise against some "bad habits", especially for the new generations. However, a resource exists to be used when (and if) it is safe to use and applicable to the proposed context. If the error suppressor "@" was to be abolished, it would have been removed from the language itself. Same as "eval" (it may be evil, but it has its purposes). What I am against is not about the usage of some resources but the generalization of an advice. In specific for the proposed case, it wouldn't be of any harm to use it, not even for debugging purposes.
– Julio Marchi
Aug 29 '17 at 3:55
add a comment |
Well, in obvious cases like that, you can always tell PHP to suppress messages by using "@" in front of the function.
$monthly_index = @array_shift(unpack('H*', date('m/Y')));
It may not be one of the best programming practices to suppress all errors this way, but in certain cases (like this one) it comes handy and is acceptable.
As result, I am sure your friend SysAdmin will be pleased with a less polluted "error.log". ;)
Well, in obvious cases like that, you can always tell PHP to suppress messages by using "@" in front of the function.
$monthly_index = @array_shift(unpack('H*', date('m/Y')));
It may not be one of the best programming practices to suppress all errors this way, but in certain cases (like this one) it comes handy and is acceptable.
As result, I am sure your friend SysAdmin will be pleased with a less polluted "error.log". ;)
edited Dec 29 '18 at 15:54
answered Dec 8 '16 at 22:09


Julio MarchiJulio Marchi
16310
16310
I don't know who downvoted this answer, but the presented solution DOES work and it IS a PHP standard technique. Really disappointing... Next time I might no answer a question anymore... :(
– Julio Marchi
Dec 17 '16 at 1:36
4
I would assume it was because suppressing the error message does not fix the problem with the code. What will you do when this type of error changes from E_STRICT to E_ERROR in a future PHP release and your code now does not run, and also does not produce any errors/output?
– Luke
Mar 6 '17 at 22:42
@TinoDidriksen, I understand and agree with the reasons to advise against some "bad habits", especially for the new generations. However, a resource exists to be used when (and if) it is safe to use and applicable to the proposed context. If the error suppressor "@" was to be abolished, it would have been removed from the language itself. Same as "eval" (it may be evil, but it has its purposes). What I am against is not about the usage of some resources but the generalization of an advice. In specific for the proposed case, it wouldn't be of any harm to use it, not even for debugging purposes.
– Julio Marchi
Aug 29 '17 at 3:55
add a comment |
I don't know who downvoted this answer, but the presented solution DOES work and it IS a PHP standard technique. Really disappointing... Next time I might no answer a question anymore... :(
– Julio Marchi
Dec 17 '16 at 1:36
4
I would assume it was because suppressing the error message does not fix the problem with the code. What will you do when this type of error changes from E_STRICT to E_ERROR in a future PHP release and your code now does not run, and also does not produce any errors/output?
– Luke
Mar 6 '17 at 22:42
@TinoDidriksen, I understand and agree with the reasons to advise against some "bad habits", especially for the new generations. However, a resource exists to be used when (and if) it is safe to use and applicable to the proposed context. If the error suppressor "@" was to be abolished, it would have been removed from the language itself. Same as "eval" (it may be evil, but it has its purposes). What I am against is not about the usage of some resources but the generalization of an advice. In specific for the proposed case, it wouldn't be of any harm to use it, not even for debugging purposes.
– Julio Marchi
Aug 29 '17 at 3:55
I don't know who downvoted this answer, but the presented solution DOES work and it IS a PHP standard technique. Really disappointing... Next time I might no answer a question anymore... :(
– Julio Marchi
Dec 17 '16 at 1:36
I don't know who downvoted this answer, but the presented solution DOES work and it IS a PHP standard technique. Really disappointing... Next time I might no answer a question anymore... :(
– Julio Marchi
Dec 17 '16 at 1:36
4
4
I would assume it was because suppressing the error message does not fix the problem with the code. What will you do when this type of error changes from E_STRICT to E_ERROR in a future PHP release and your code now does not run, and also does not produce any errors/output?
– Luke
Mar 6 '17 at 22:42
I would assume it was because suppressing the error message does not fix the problem with the code. What will you do when this type of error changes from E_STRICT to E_ERROR in a future PHP release and your code now does not run, and also does not produce any errors/output?
– Luke
Mar 6 '17 at 22:42
@TinoDidriksen, I understand and agree with the reasons to advise against some "bad habits", especially for the new generations. However, a resource exists to be used when (and if) it is safe to use and applicable to the proposed context. If the error suppressor "@" was to be abolished, it would have been removed from the language itself. Same as "eval" (it may be evil, but it has its purposes). What I am against is not about the usage of some resources but the generalization of an advice. In specific for the proposed case, it wouldn't be of any harm to use it, not even for debugging purposes.
– Julio Marchi
Aug 29 '17 at 3:55
@TinoDidriksen, I understand and agree with the reasons to advise against some "bad habits", especially for the new generations. However, a resource exists to be used when (and if) it is safe to use and applicable to the proposed context. If the error suppressor "@" was to be abolished, it would have been removed from the language itself. Same as "eval" (it may be evil, but it has its purposes). What I am against is not about the usage of some resources but the generalization of an advice. In specific for the proposed case, it wouldn't be of any harm to use it, not even for debugging purposes.
– Julio Marchi
Aug 29 '17 at 3:55
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f2354609%2fstrict-standards-only-variables-should-be-passed-by-reference%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Nps6,0lz2aJ nGXscvs9zqf1jd8i,NCi,QFt,bTQB3qA dy,34 P825WGfqHLX VvN0zmxxeRZRDaHnMlBBsRsXN1R uL
1
What does $instance->find(..) return?
– Silver Light
Mar 1 '10 at 8:44
2
Here is the solution: stackoverflow.com/questions/9848295/…
– ajaristi
Mar 7 '13 at 22:09
I think the examples (or logic) might be the wrong way round in the question, since the 2nd example (
get_arr()
function) does produce the strict standards notice (tested PHP 5.2 and PHP 5.5).– MrWhite
May 1 '14 at 0:03