PHP Warning: Attempt to read property '...' on string/array [Solved]

How to fix PHP Warning attempt to read property of array or string

This article explores the reasons behind and solutions for the PHP Warning message, specifically the "PHP Warning: Attempt to read property '...' on a string or array". If you're currently facing this error during your PHP project development, this article will shed light on why it occurs and how to prevent it.

In PHP, various types of exceptions may arise during the project development phase. These exceptions include PHP Fatal Errors and PHP Warnings. Fatal Errors have the potential to immediately stop the script execution or crash the application, while PHP Warnings do not lead to script termination.

In this article, we focus on errors related to PHP Warnings. Apart from the error message mentioned earlier, PHP Warnings may also occur like the following:

Click on the error messages above to access articles discussing each error in more detail.

What does "PHP Warning: Attempt to read property '...' on string or array" mean?

The "PHP Warning: Attempt to read property '...' on string" or "PHP Warning: Attempt to read property '...' on array" is a PHP warning or error that occurs when accessing a variable property or object. This error is similar to the "PHP Notice: Trying to get property of non-object" error in older versions of PHP.

Essentially, this error occurs when you attempt to access a property or method of a variable that is not an object. For example, consider a simple JSON file called data.json with the following contents:

  1. {
  2.     "data":[{
  3.         "id":1,
  4.         "name": "Mark Cooper",
  5.         "amount" : 14062023.00
  6.     },
  7.     {
  8.         "id":2,
  9.         "name": "Samantha Lou",
  10.         "amount" : 1415710.00
  11.     },
  12.     {
  13.         "id":1,
  14.         "name": "John Smith",
  15.         "amount" : 1997157.00
  16.     }]
  17. }

Next, we will attempt to display the list of data items listed in the file by retrieving the file's content using the file_get_contents() function in our PHP script.

  1. <?php
  2. // Get JSON Data
  3. $dataString = file_get_contents('./data.json');
  4. // Output data from JSON data
  5. print_r($dataString);
  6. ?>

In the provided HTML snippet, you may encounter a warning message like "PHP Warning: Trying to access property '...' on a string.". This warning is triggered because the file_get_contents() function returns the file contents as a string, not as an object.

How to fix PHP Warning attempt to read property of array or string

To prevent this error, especially when dynamically defining variables, you can use the is_object() function to check if a variable is of type object. Here's an example of how to use it:

  1. <?php
  2. // Get JSON Data
  3. $dataString = file_get_contents('./data.json');
  4. // Output data from JSON data
  5. if(is_object($dataString)){
  6.     print_r($dataString->data);
  7. }else{
  8.     print("The given variable is not an object!");
  9. }
  10. ?>

How to fix PHP Warning attempt to read property of array or string

Here's another example of a PHP script that can trigger this error. In this case, the defined variable is of type array, which results in a PHP warning like "PHP Warning: Trying to access property '...' on an array.".

  1. <?php
  2. // Get JSON Data
  3. $dataString = file_get_contents('./data.json');
  4. $dataJSON =(array) json_decode($dataString);
  5. // Output data from JSON data
  6. print_r($dataJSON->data);
  7. ?>

How to fix PHP Warning attempt to read property of array or string

To address this issue, you can follow the same approach as in the previous example, which involves incorporating a conditional parameter with the is_object() function.

  1. <?php
  2. // Get JSON Data
  3. $dataString = file_get_contents('./data.json');
  4. $dataJSON =(array) json_decode($dataString);
  5. // Output data from JSON data
  6. if(is_object($dataJSON)){
  7.     print_r($dataJSON->data);
  8. }else{
  9.     print("The given variable is not an object");
  10. }
  11. ?>

How to fix PHP Warning attempt to read property of array or string

Here is an example that effectively retrieves and outputs the property return value:

  1. <?php
  2. // Get JSON Data
  3. $dataString = file_get_contents('./data.json');
  4. $dataJSON = json_decode($dataString);
  5. // Output data from JSON data
  6. if(is_object($dataJSON)){
  7.     print_r($dataJSON->data);
  8. }else{
  9.     print("The given variable is not an object");
  10. }
  11. ?>

How to fix PHP Warning attempt to read property of array or string

Conclusion

To put it simple, the "PHP Warning: Attempt to access property '...' on a string or array" occurs when we try to access a property or method from a non-object variable. To prevent this warning, it's crucial to include a conditional parameter in our script to verify whether the given variable is of type object. Additionally, utilizing PHP's is_object() built-in function is essential for checking the defined variable's type.

There you have it! I hope this article has equipped you with the knowledge to resolve this error and prevent it from recurring. Feel free to explore more on this website for more Free Source Codes, Tutorials, and articles covering various programming languages, errors, and updates.

Happy Coding =)

Add new comment