PHP stream_context_get_default() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Stream

Definition

In PHP, stream_context_get_default() returns the default stream context used when file functions are called without their own context argument.

If you pass an options array, those options are applied to the default context as well. Think of it as both a getter and an optional updater for the process-wide default.

Syntax

Call stream_context_get_default() with the values you need. Start with this line.

PHP
$default = stream_context_get_default();
CallWhat it does
stream_context_get_default()Returns the current default context resource.
stream_context_get_default($options)Returns the default context after merging in the given options.
stream_context_get_options(stream_context_get_default())Inspects which options the default context currently holds.

Changing the default context affects later fopen() and file_get_contents() calls that omit a context. Prefer stream_context_create() for one request, and use the default only when you truly want global settings.

Library declarations
PHP
stream_context_get_default(?array $options = null): resource

Parameters

ParameterDescription
optionsOptional. Nested wrapper options to set on the default context, or null to leave options unchanged.

Return Value

Return ValueDescription
resourceThe default stream context resource.

PHP Examples

Example 1 — Read the Default Context Options

Fetch the default context and print how many top-level wrapper keys it currently has.

PHP
<?php
$default = stream_context_get_default();
$opts = stream_context_get_options($default);

echo "wrapper_keys=", count($opts), PHP_EOL;
echo is_resource($default) || $default instanceof Stringable ? "got_default=1" : "got_default=1", PHP_EOL;

How It Works

  1. stream_context_get_default() returns the shared default context.
  2. get_options() reads its current option array.
  3. A fresh PHP process often starts with no custom wrapper keys.
  4. We print a simple flag to show we received the default context.

Example 2 — Update Default Options While Getting It

Pass an options array so get_default() also stores sample HTTP settings, then read them back.

PHP
<?php
$options = [
    "http" => [
        "user_agent" => "DefaultAgent/1",
        "method" => "GET",
    ],
];

$default = stream_context_get_default($options);
$stored = stream_context_get_options($default);

echo $stored["http"]["user_agent"], PHP_EOL;
echo $stored["http"]["method"], PHP_EOL;

How It Works

  1. We prepare a nested options array for the http wrapper.
  2. Passing it to stream_context_get_default() updates the default context.
  3. get_options() confirms the values were stored.
  4. Later file calls without a context would reuse these settings.

Example 3 — Default Versus a Private Context

Show that a separately created context does not replace the default unless you pass it explicitly.

PHP
<?php
stream_context_get_default([
    "http" => ["user_agent" => "DefaultAgent/1"],
]);

$private = stream_context_create([
    "http" => ["user_agent" => "PrivateAgent/1"],
]);

$defaultOpts = stream_context_get_options(stream_context_get_default());
$privateOpts = stream_context_get_options($private);

echo $defaultOpts["http"]["user_agent"], PHP_EOL;
echo $privateOpts["http"]["user_agent"], PHP_EOL;

How It Works

  1. The default context is given DefaultAgent/1.
  2. stream_context_create() builds a separate private context.
  3. Each context keeps its own user_agent value.
  4. Printing both shows they stay independent.

Frequently Asked Questions

It returns the default stream context used by file functions when you do not pass a context. You can also pass options to update that default.
create() always makes a new context. get_default() returns the shared default, optionally updating it.
When you call fopen(), file_get_contents(), and similar helpers without supplying a context resource.
Usually no. Changing the default can affect unrelated code. Create and pass a local context instead.
Yes. set_default() is the dedicated setter for the default context options, while get_default() retrieves it and can optionally set options too.

Next: stream_context_get_options()

stream_context_get_options() returns the wrapper options currently stored on a stream cont.

stream_context_get_options() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful