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.
Accessing scalar variables
The following example show you how to access scalar variables with fopen() , fread() , frwite() and fclose() .
Example 39-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 39-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.
Who's Online
Guest Users: 8
Deliver First Class Web Sites: 101 Essential Checklists Want to learn how to make your web sites usable and accessible? Want to ensure that your sites meet current best practice, without spending hours trawling through incomprehensible specifications and recommendations from dozens of different books, research papers, and web sites? Want to make sure that the sites you build are "right the first time," requiring no costly redevelopments?
More Sample Chapters