Notice: Array to string conversion in C:\xampp\htdocs\example\echo.php on line 8 Array

Notice: Array to string conversion in C:\xampp\htdocs\example\echo.php on line 8 Array

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Understanding "Array to String Conversion" Errors in PHP

When you run a PHP script and encounter a Notice: Array to string conversion error, it usually points to a specific type of logic mistake rather than a broken system component. The error occurs when the language engine attempts to treat an array object as if it were a single string value. In your specific case, the issue lies in how you are attempting to output the $products variable versus how you attempt to loop through it later.

The root cause of the first error is hidden inside the echo $products; statement on line 8 of your script. The echo keyword in PHP expects exactly one argument (or a series of arguments separated by spaces) that can be cast into a string. When you pass an array directly to echo, PHP does not automatically iterate through the elements and join them with commas or newlines. Instead, it tries to convert the entire array structure into a single string representation. By default, PHP converts arrays to strings using their internal key-value mapping in a format like Array (or sometimes Array ( ... ) depending on configuration), which triggers the notice you are seeing.

To fix this immediately, you must explicitly iterate over the array before echoing. Your second attempt using foreach is on the right track conceptually, but there is a subtle syntax error in that loop: echo $product;. When you execute echo $product inside the loop, you are again telling PHP to print the entire associative array for that specific product as a single string. This will result in the same "Array" notice repeating for every item in your list.

To correctly output each element, you need to access the keys within the $product array. For example:

foreach ($products as $product) {
    echo $product['name'] . ' - ' . $product['price'] . '<br>';
}

By accessing specific keys like name or price, you are providing a scalar (string or integer) value to echo. PHP can easily convert these scalars into strings without triggering any notices. This approach also allows you to format the output however you see fit, perhaps adding newlines or building HTML elements dynamically.

If your dataset grows larger, containing multiple entries like $products[101], $products[102], and so on up to $products[n], the foreach loop remains the most robust solution. This construct iterates over every key-value pair in the array regardless of whether the keys are integers or strings. Whether you have five products or five thousand, the loop handles the iteration logic internally, keeping your main script clean and readable.

However, be mindful of how you handle missing keys. If you try to access $product['img'] inside the loop for a product that somehow lacks an image property, PHP will throw a warning about undefined index. A safer pattern involves using the null coalescing operator or checking existence first:

foreach ($products as $product) {
    echo htmlspecialchars($product['name'] ?? 'No Name') . '<br>';
}

This practice prevents errors when rendering data from external sources or user input where data integrity cannot be guaranteed at every step. When building dynamic interfaces, always assume that while the structure exists, the content might be empty.

For developers working with larger applications, frameworks like Laravel handle these iterations automatically through collection methods such as ->each() or ->map(), abstracting away the raw array syntax. If you are starting a new project, leveraging a framework can reduce the likelihood of making manual iteration mistakes early on. For more details on handling arrays and collections in modern PHP environments, resources like https://laravelcompany.com provide excellent guides on best practices for data manipulation.

Ultimately, the "Array to string conversion" notice is a helpful hint that your code is trying to treat a collection as a single item. By explicitly iterating and accessing individual keys, you ensure that only valid strings are sent to the output buffer, resulting in clean, error-free execution.

Our network