Fueling Your Coding Mojo

Buckle up, fellow PHP enthusiast! We're loading up the rocket fuel for your coding adventures...

Popular Searches:
22
Q:

Php strict standards: Only variables should be passed by reference

Hello everyone,

I hope you're all doing well. I have recently come across an issue while working with PHP and I was hoping someone could shed some light on it. I keep encountering the error message "PHP strict standards: Only variables should be passed by reference". As I'm relatively new to PHP, I am not exactly sure what this error means and how to fix it.

Allow me to provide a bit of context. I am working on a project where I need to modify an existing PHP code. Within the code, there is a function call that seems to be causing this error. From what I understand, passing variables by reference can be useful in certain situations, but it seems like this is not allowed in strict standards.

I would really appreciate it if someone could explain to me in simple terms what this error message means. Additionally, I would like to know how I can modify the code to fix this issue and meet the strict standards of PHP.

Thank you in advance for your help and expertise!

All Replies

nasir10

Hey there!

I've encountered this error before, and it can be a bit tricky to understand at first. The "PHP strict standards: Only variables should be passed by reference" message usually indicates that you're trying to pass a non-variable as a reference to a function.

One common scenario where this error occurs is when you try to pass a literal value or a constant as a reference to a function that expects a variable. For example, if you have code like this:


function myFunction(&$variable) {
// do something with $variable
}

myFunction('some value');


In this case, the error will be triggered because you're passing the literal value 'some value' instead of an actual variable. To fix this, you need to assign the value to a variable before passing it as a reference, like so:


$myValue = 'some value';
myFunction($myValue);


By doing this, you're now passing a variable as a reference, which satisfies the strict standards of PHP.

I hope this explanation helps you understand the error better. Let me know if you have any further questions or need more examples!

jpacocha

Hey everyone,

I've also encountered this error message during my PHP coding journey, and it can definitely puzzle you if you're not familiar with it. So when you see "PHP strict standards: Only variables should be passed by reference," it means that you're trying to pass something other than a variable by reference, which is not allowed according to PHP's strict standards.

One situation where this error often crops up is when you try to pass the result of a function or an expression as a reference. For example:


function modifyValue(&$value) {
// Modify the value somehow
}

modifyValue(getSomeValue());


In the code snippet above, if the function `getSomeValue()` returns a value directly without being stored in a variable, PHP throws the "only variables should be passed by reference" error. To resolve this, you'll need to first assign the returned value to a variable and then pass that variable as a reference, like this:


$result = getSomeValue();
modifyValue($result);


By saving the result in a variable first, you're now passing a variable (in this case, `$result`) as a reference, which adheres to PHP's strict standards.

I hope this clarification helps you understand the issue better. Feel free to reach out if you have more questions or need further examples. Happy coding!

rogahn.jessyca

Hey there,

I faced the same issue not too long ago and thought I'd share my experience. When you encounter the error "PHP strict standards: Only variables should be passed by reference," it's indicating that you're passing something as a reference that isn't a variable, which goes against PHP's strict standards.

One scenario where this error commonly arises is when you try to pass the return value of a function directly as a reference. Here's an example:


function processData(&$data) {
// Process the data here
}

processData(getData());


If the `getData()` function returns a value directly without being stored in a variable, PHP throws the "only variables should be passed by reference" error. To resolve this, you need to assign the return value to a variable first and then pass that variable as a reference, like so:


$result = getData();
processData($result);


By assigning the returned value to `$result` before passing it as a reference, you adhere to PHP's strict standards.

I hope this insight helps you grasp the error better. Let me know if you require further clarification or additional examples. Happy coding!

New to LearnPHP.org Community?

Join the community