PHP stream_context_set_default() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Stream

Definition

In PHP, stream_context_set_default() replaces the options on the default stream context used by file functions that do not receive their own context.

The options array uses the same nested wrapper shape as stream_context_create(). The function returns the default context resource after applying those options.

Syntax

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

PHP
$default = stream_context_set_default(["http" => ["user_agent" => "App/1"]]);
CallWhat it does
stream_context_set_default($options)Writes $options into the shared default context and returns it.
stream_context_get_options(stream_context_set_default($options))Sets the default and immediately inspects the stored options.
stream_context_set_default(["http" => ["method" => "GET"]])Stores a sample HTTP method on the default context.

Passing an empty array does not clear existing default options. To experiment safely in tutorials, set explicit keys and inspect them with get_options() instead of relying on a reset.

Library declarations
PHP
stream_context_set_default(array $options): resource

Parameters

ParameterDescription
optionsRequired. Nested associative array of wrapper options in $arr['wrapper']['option'] form.

Return Value

Return ValueDescription
resourceThe default stream context resource.

PHP Examples

Example 1 — Set a Default User Agent

Store a sample HTTP user agent on the default context and read it back. No network I/O happens.

PHP
<?php
$default = stream_context_set_default([
    "http" => [
        "user_agent" => "TutorialAgent/1",
        "method" => "GET",
    ],
]);

$opts = stream_context_get_options($default);
echo $opts["http"]["user_agent"], PHP_EOL;
echo $opts["http"]["method"], PHP_EOL;

How It Works

  1. We build a nested options array for the http wrapper.
  2. stream_context_set_default() applies it to the shared default context.
  3. get_options() reads the values back.
  4. The printed lines confirm user_agent and method.

Example 2 — Default Survives Alongside Private Contexts

After setting the default, create a private context with different values and show both side by side.

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

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

echo stream_context_get_options(stream_context_get_default())["http"]["user_agent"], PHP_EOL;
echo stream_context_get_options($private)["http"]["user_agent"], PHP_EOL;

How It Works

  1. set_default() stores Default/1 on the shared context.
  2. create() builds a separate context with Private/1.
  3. Each get_options() call reads a different resource.
  4. Both values remain available at the same time.

Example 3 — Set Multiple Wrapper Keys

Store sample http and ssl options together on the default context, then list the wrapper keys.

PHP
<?php
stream_context_set_default([
    "http" => [
        "method" => "GET",
    ],
    "ssl" => [
        "verify_peer" => true,
    ],
]);

$opts = stream_context_get_options(stream_context_get_default());
echo implode(",", array_keys($opts)), PHP_EOL;
echo $opts["http"]["method"], PHP_EOL;
echo $opts["ssl"]["verify_peer"] ? "verify=1" : "verify=0", PHP_EOL;

How It Works

  1. One options array can hold several wrapper sections.
  2. set_default() stores both http and ssl keys.
  3. array_keys() lists the top-level wrapper names.
  4. We print the method and a verify flag for confirmation.

Frequently Asked Questions

It sets the options on the default stream context that file functions use when no context argument is given.
It returns the default context resource after applying the options.
No. Passing [] does not reset existing default options. Set the keys you need explicitly.
Avoid it in shared libraries or anywhere other code might rely on untouched defaults. Pass a local context instead.
Call stream_context_get_options(stream_context_get_default()) or use the resource returned by set_default().

Next: stream_context_set_option()

stream_context_set_option() sets one wrapper option on a stream or context, such as http/u.

stream_context_set_option() 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