php is variable an array or object

php is variable an array or object

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

Stefan Bogdanescu

Founder & Senior Architect

Is PHP a Variable, Array, or Object? Unpacking Type Checking

Translating type-checking logic from JavaScript to PHP often triggers confusion because the two languages handle primitives and containers differently under the hood. In your JavaScript example, you are checking if a variable is an Array or a generic Object. While that pattern works in JS, it relies on specific duck-typing behaviors where almost everything inherits from Object.prototype, making broad checks somewhat leaky.

In PHP, attempting to replicate this exact logic directly can lead to unexpected behavior due to how the language treats arrays and objects as distinct types. You cannot simply cast a string or integer to an array in the same way, nor do you inherit from a base "Object" class that includes arrays in its hierarchy for type checking purposes.

To determine if a value is an array or an object in PHP, you must use specific built-in functions that inspect the internal data structure. The most direct equivalent to your JavaScript logic involves combining is_array() and is_object(). However, there is a critical nuance regarding strings and numbers that requires careful handling.

Handling Arrays and Objects

If your goal is to branch execution based on whether the input is a structured collection (array or object) versus a primitive value, you need two distinct checks. Unlike JavaScript's loose typing where typeof might return "object" for arrays, PHP distinguishes these as separate types: "array" and "object".

You can implement this logic cleanly by chaining conditions or using logical operators:

if (is_array($str) || is_object($str)) {
    // The value is either an array or an object
    // You can safely iterate or access properties here
} else {
    // The value is likely a string, int, float, bool, or null
    // Handle primitive logic here
}

This approach mirrors your JavaScript intent but uses PHP's explicit type functions. It ensures that if $str is passed as "hello" (a string) or 123 (an integer), the code enters the else block. This prevents runtime errors when trying to iterate over a primitive value or access properties like $str->name.

The Case of Strings and Integers

In your JavaScript snippet, you explicitly mentioned checking if the parameter was a string or int in the else block. PHP is dynamically typed, so variables hold their type at runtime. If you pass a string to the function above, is_array() returns false and is_object() returns false, directing the flow to the primitive handler.

However, be wary of loose typing if you are reading data from external sources like JSON or API responses. If a JSON string is decoded without strict mode, it becomes a PHP array or object. Conversely, if you receive raw input, it remains a string. Always verify the source of your data to ensure is_array() and is_object() behave as expected.

Why Not gettype()?

You might wonder why we don't rely solely on gettype(). While gettype() is useful for debugging, it returns a string representing the type (e.g., "array", "object", "string"). Using gettype($str) === 'array' || gettype($str) === 'object' works but is less readable and harder to maintain than the dedicated boolean functions is_array() and is_object(). The latter are optimized for performance and clarity in conditional logic.

A Note on Laravel Ecosystem

If you are working within the Laravel framework, type checking remains a fundamental responsibility of the developer, even with strong typing tools like PHPStan or Laravel's built-in validation facades. While frameworks provide robust validators to ensure inputs match expected schemas (e.g., ensuring an array is passed where required), raw type checking with is_array() and is_object() is still essential for handling flexible, polymorphic data structures that might shift between states during execution. For instance, a utility function might accept an array of users or a UserCollection object interchangeably; distinguishing these at runtime ensures your logic adapts correctly without crashing.

Final Considerations on Type Safety

The main pitfall in PHP when transitioning from JavaScript is assuming that "everything is an object." In PHP, arrays are not objects by default (though they can be cast as such). Therefore, checking for both types separately is mandatory. If you need to treat them uniformly later in the code, consider using array_values() or converting objects to arrays via (array) $value, but only after confirming the initial type check has passed. This separation of concerns keeps your logic robust and prevents subtle bugs where primitive values are accidentally treated as collections.

Our network