PEAR Manual
Prev |  | Next

# Example

Example -- Example for the usage of Stream_Var

## Registering the wrapper

The following example shows you how to register Stream_Var as a wrapper for the stream functions.

Example 54-1. Registering Stream_Var
    require_once "Stream/Var.php";
    stream_wrapper_register( "var", "Stream_Var" );

## Accessing scalar variables

The following example show you how to access scalar variables with fopen(), fread(), frwite() and fclose().

Example 54-2. Accessing scalar variables
    require_once "Stream/Var.php";
    stream_wrapper_register( "var", "Stream_Var" );
    $foo = "I really like tomatoes.";

    echo "Content of foo: $foo<br />";

    $fp = fopen('var://GLOBALS/foo','r+');

    $data = fread($fp, 9);
    echo "Read from stream: $data<br />";

    fwrite($fp,"hate");

    fclose($fp);

    echo "Content of foo: $foo<br />";

## Accessing an array

The following example shows how to use opendir() to access an array.

Example 54-3. Accessing an array
    require_once "Stream/Var.php";
    stream_wrapper_register( "var", "Stream_Var" );
    $dirname = 'var://_SERVER';
    $dir = opendir($dirname);
    echo    "<strong>opening directory '$dirname'</strong><br><br>";

    while ($entry = readdir($dir)) {
        echo "opening file $dirname/$entry<br />";
        if (!$fp = @fopen($dirname."/".$entry,"r")) {
            echo "seems to be a directory<br /><br />";
            continue;
        }

        echo "reading from $entry<br />";
        while (!feof($fp)) {
            echo fread($fp, 16);
        }
        fclose($fp);
        echo    "<br /><br />";
    }
    closedir($dir);

## More examples

If you want to take a look at some more examples, just install the package and they will be installed in the docs directory.

Prev | Home | Next
Introduction | Up | Structures