1--TEST--
2Test fwrite() function : usage variation
3--CREDITS--
4Dave Kelsey <d_kelsey@uk.ibm.com>
5--FILE--
6<?php
7/* Prototype  : int fwrite(resource fp, string str [, int length])
8 * Description: Binary-safe file write
9 * Source code: ext/standard/file.c
10 * Alias to functions: bzwrite fputs gzwrite
11 */
12
13echo "*** Testing fwrite() : usage variation ***\n";
14
15// Define error handler
16function test_error_handler($err_no, $err_msg, $filename, $linenum, $vars) {
17	if (error_reporting() != 0) {
18		// report non-silenced errors
19		echo "Error: $err_no - $err_msg, $filename($linenum)\n";
20	}
21}
22set_error_handler('test_error_handler');
23
24// Initialise function arguments not being substituted (if any)
25
26$filename = __DIR__ . '/fwriteVar5.tmp';
27
28
29
30//get an unset variable
31$unset_var = 10;
32unset ($unset_var);
33
34// define some classes
35class classWithToString
36{
37	public function __toString() {
38		return "Class A object";
39	}
40}
41
42class classWithoutToString
43{
44}
45
46// heredoc string
47$heredoc = <<<EOT
48hello world
49EOT;
50
51// add arrays
52$index_array = array (1, 2, 3);
53$assoc_array = array ('one' => 1, 'two' => 2);
54
55//array of values to iterate over
56$inputs = array(
57
58      // int data
59      'int 0' => 0,
60      'int 1' => 1,
61      'int 12345' => 12345,
62      'int -12345' => -2345,
63
64      // float data
65      'float 10.5' => 10.5,
66      'float -10.5' => -10.5,
67      'float 12.3456789000e10' => 12.3456789000e10,
68      'float -12.3456789000e10' => -12.3456789000e10,
69      'float .5' => .5,
70
71      // array data
72      'empty array' => array(),
73      'int indexed array' => $index_array,
74      'associative array' => $assoc_array,
75      'nested arrays' => array('foo', $index_array, $assoc_array),
76
77      // null data
78      'uppercase NULL' => NULL,
79      'lowercase null' => null,
80
81      // boolean data
82      'lowercase true' => true,
83      'lowercase false' =>false,
84      'uppercase TRUE' =>TRUE,
85      'uppercase FALSE' =>FALSE,
86
87      // empty data
88      'empty string DQ' => "",
89      'empty string SQ' => '',
90
91      // object data
92      'instance of classWithToString' => new classWithToString(),
93      'instance of classWithoutToString' => new classWithoutToString(),
94
95      // undefined data
96      'undefined var' => @$undefined_var,
97
98      // unset data
99      'unset var' => @$unset_var,
100);
101
102// loop through each element of the array for str
103
104foreach($inputs as $key =>$value) {
105      echo "\n--$key--\n";
106      $fp = fopen($filename,'w');
107      fwrite($fp, $value);
108      fclose($fp);
109      readfile($filename);
110};
111unlink($filename);
112
113?>
114===DONE===
115--EXPECTF--
116*** Testing fwrite() : usage variation ***
117
118--int 0--
1190
120--int 1--
1211
122--int 12345--
12312345
124--int -12345--
125-2345
126--float 10.5--
12710.5
128--float -10.5--
129-10.5
130--float 12.3456789000e10--
131123456789000
132--float -12.3456789000e10--
133-123456789000
134--float .5--
1350.5
136--empty array--
137Error: 2 - fwrite() expects parameter 2 to be string, array given, %s(%d)
138
139--int indexed array--
140Error: 2 - fwrite() expects parameter 2 to be string, array given, %s(%d)
141
142--associative array--
143Error: 2 - fwrite() expects parameter 2 to be string, array given, %s(%d)
144
145--nested arrays--
146Error: 2 - fwrite() expects parameter 2 to be string, array given, %s(%d)
147
148--uppercase NULL--
149
150--lowercase null--
151
152--lowercase true--
1531
154--lowercase false--
155
156--uppercase TRUE--
1571
158--uppercase FALSE--
159
160--empty string DQ--
161
162--empty string SQ--
163
164--instance of classWithToString--
165Class A object
166--instance of classWithoutToString--
167Error: 2 - fwrite() expects parameter 2 to be string, object given, %s(%d)
168
169--undefined var--
170
171--unset var--
172===DONE===
173