Use gc_enabled() when you need to reports whether automatic cycle collection is enabled in a PHP application. The examples below show the actual operation and its limitations.
What Is gc_enabled() in PHP?
Reports whether automatic cycle collection is enabled. The exact signature supported by your installation depends on its PHP version and enabled extensions.
Its syntax is:
gc_enabled(): bool
Parameters and return value: The parameter types, optional arguments, references, and return type are shown in the signature above. Optional parameters are marked with an ellipsis in this reference signature.
Example 1: Basic Usage
<?php
var_dump(gc_enabled());
Result: Run the example in an environment that supports the function. Any values depending on operating-system state, installed extensions, process configuration, or network access must be checked on the target server.
Example 2: Different Input or Behavior
<?php
gc_disable(); var_dump(gc_enabled()); gc_enable();
Result: Run the example in an environment that supports the function. Any values depending on operating-system state, installed extensions, process configuration, or network access must be checked on the target server.
Practical Example: Applying the Function
<?php
$before = gc_enabled(); gc_disable(); $during = gc_enabled(); if ($before) gc_enable(); echo $during ? "Enabled" : "Disabled";
Result: Run the example in an environment that supports the function. Any values depending on operating-system state, installed extensions, process configuration, or network access must be checked on the target server.
Important Behavior and Edge Cases
The status does not say whether a collection has occurred or whether memory is exhausted.
Comparison With Related Functions
| Approach | Difference |
|---|---|
gc_enabled() | Reports whether automatic cycle collection is enabled. |
| Related operation | gc_status() reports collection statistics; gc_enabled() reports a boolean switch. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Ignoring the function-specific limitation | The status does not say whether a collection has occurred or whether memory is exhausted. |
| Confusing this function with a related operation | gc_status() reports collection statistics; gc_enabled() reports a boolean switch. |
Key Takeaways
- Reports whether automatic cycle collection is enabled.
- The status does not say whether a collection has occurred or whether memory is exhausted.
- gc_status() reports collection statistics; gc_enabled() reports a boolean switch.
Use gc_enabled() for the specific operation demonstrated above. Check its prerequisites and handle failure conditions before using it with production data.