Fueling Your Coding Mojo

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

Popular Searches:
72
Q:

Strict Standards: Only variables should be assigned by reference PHP 5.4

Hi everyone,

I'm currently facing an issue with my PHP 5.4 code and I was hoping someone could help me out. I'm receiving the error message "Strict Standards: Only variables should be assigned by reference" and I'm not sure how to fix it.

To provide some context, I'm working on a web application that allows users to create and manage their own profiles. Within these profiles, users can upload images. However, when a user tries to upload an image, I'm seeing this strict standards error.

I've done some research on this issue, and from what I understand, this error occurs when a reference is being attempted to assign a value rather than a variable. I've checked my code, but I'm unable to identify where this might be happening.

Here's a snippet of the code that is triggering the error:

```php
$image = &$_FILES['image']['tmp_name'];
```

I understand that using references in this context is no longer recommended and causes this error in PHP 5.4. However, I'm not sure how to modify the code to resolve the issue. Could someone provide guidance on the correct way to assign the temporary name of the image without triggering this strict standards error?

I appreciate any help or insights you can provide. Thank you in advance.

Best regards,
[Your Name]

All Replies

froberts

User 2:
Hey there,

I ran into a similar strict standards error while working on a PHP 5.4 project a few months ago. It can indeed be frustrating, but I managed to find a solution that worked for me.

In my case, the error was triggered when I was attempting to pass a constant value as a reference. To resolve this, I made use of an intermediary variable to hold the constant value and then assigned that variable to the reference. Here's an example of the code adjustment I made:

php
const MY_CONSTANT = 'some value';
$temp = MY_CONSTANT;
$myVariable = &$temp;


By assigning the constant value to the `$temp` variable first and then assigning `$temp` to `$myVariable` using the reference assignment operator (`&`), I was able to bypass the strict standards error.

Give this approach a try and see if it helps resolve your issue. If you have any further questions or need additional assistance, feel free to let me know.

Best regards,
User 2

bruen.paolo

User 1:
Hey [Your Name],

I faced a similar issue a while back when working with PHP 5.4. In my case, the error was occurring because I was trying to assign a value to a reference directly. After some trial and error, I managed to resolve it by making a slight adjustment to my code.

Instead of assigning the value directly to the reference, I assigned it to a variable first and then assigned that variable to the reference. Here's an example of what I did:

php
$tempName = $_FILES['image']['tmp_name'];
$image = &$tempName;


By first assigning the value to the `$tempName` variable, which is just a regular variable, I was able to avoid the strict standards error. Then, I assigned `$tempName` to `$image` using the reference assignment operator (`&`).

Give this approach a try in your code and see if it helps resolve the issue. Let me know if you have any questions or if you need further assistance.

Best regards,
User 1

New to LearnPHP.org Community?

Join the community