PHP stream_context_set_params() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Stream

Definition

In PHP, stream_context_set_params() stores context parameters such as a notification callback on a stream or context resource.

Parameters are not the same as wrapper options. Options configure http or ssl behavior; parameters configure context-level behavior like progress notifications.

Syntax

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

PHP
stream_context_set_params($ctx, ["notification" => "stream_notification_callback"]);
CallWhat it does
stream_context_set_params($ctx, $params)Writes the parameter array onto the context.
stream_context_set_params($ctx, ["notification" => "my_notify"])Stores a callback name for stream notifications.
stream_context_get_params($ctx)Reads the parameters back for inspection.

Supported parameters include notification and options. notification is used by http:// and ftp:// wrappers. These examples only set and read params; they do not start a live transfer.

Library declarations
PHP
stream_context_set_params(resource $context, array $params): true

Parameters

ParameterDescription
contextRequired. The stream or context resource to update.
paramsRequired. Associative array of parameters, for example notification => callback name.

Return Value

Return ValueDescription
trueReturns true on success.

PHP Examples

Example 1 — Store a Notification Callback Name

Attach a notification parameter and print it with get_params().

PHP
<?php
$ctx = stream_context_create();
stream_context_set_params($ctx, [
    "notification" => "stream_notification_callback",
]);

$params = stream_context_get_params($ctx);
echo $params["notification"], PHP_EOL;

How It Works

  1. An empty context is created.
  2. set_params() stores the notification entry.
  3. get_params() returns the stored parameters.
  4. We print the callback name string.

Example 2 — Keep Options and Params Together

Create options first, then add a notification parameter, and show both through get_params().

PHP
<?php
$ctx = stream_context_create([
    "http" => ["method" => "GET"],
]);
stream_context_set_params($ctx, [
    "notification" => "stream_notification_callback",
]);

$params = stream_context_get_params($ctx);
echo $params["options"]["http"]["method"], PHP_EOL;
echo $params["notification"], PHP_EOL;

How It Works

  1. create() stores the HTTP method as an option.
  2. set_params() adds the notification parameter.
  3. get_params() exposes both areas in one array.
  4. The sample prints GET and the callback name.

Example 3 — Confirm the Boolean Success Result

Capture the return value of set_params() and list the resulting param keys.

PHP
<?php
$ctx = stream_context_create();
$ok = stream_context_set_params($ctx, [
    "notification" => "stream_notification_callback",
]);

$params = stream_context_get_params($ctx);
echo $ok ? "true" : "false", PHP_EOL;
echo implode(",", array_keys($params)), PHP_EOL;

How It Works

  1. set_params() returns true when it succeeds.
  2. get_params() then includes notification and options keys.
  3. implode() prints the key list in one line.
  4. This stays fully offline.

Frequently Asked Questions

It sets context parameters such as a notification callback on a stream or context resource.
Options configure wrappers (http, ssl, and so on). Params are context-level settings like notification.
It names a callback PHP can call during some transfers, mainly for http:// and ftp:// wrappers.
Yes, by including an options key, but beginners usually set options with set_option or set_options instead.
Call stream_context_get_params() on the same context or stream.

Next: stream_copy_to_stream()

stream_copy_to_stream() copies bytes from one stream to another and returns how many bytes.

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