1--TEST--
2Test strtr() function : usage variations - unexpected inputs for 'str' argument
3--FILE--
4<?php
5/* Prototype  : string strtr(string $str, string $from[, string $to]);
6                string strtr(string $str, array $replace_pairs);
7 * Description: Translates characters in str using given translation tables
8 * Source code: ext/standard/string.c
9*/
10
11/* Test strtr() function: with unexpected inputs for 'str'
12 *  and expected type for 'from' & 'to' arguments
13*/
14
15echo "*** Testing strtr() function: with unexpected inputs for 'str' ***\n";
16
17//get an unset variable
18$unset_var = 'string_val';
19unset($unset_var);
20
21//defining a class
22class sample  {
23  public function __toString() {
24    return "sample object";
25  }
26}
27
28//getting the resource
29$file_handle = fopen(__FILE__, "r");
30
31// array with different values
32$strings =  array (
33
34		  // integer values
35/*1*/	  0,
36		  1,
37		  -2,
38
39		  // float values
40/*4*/	  10.5,
41		  -20.5,
42		  10.1234567e10,
43
44		  // array values
45/*7*/	  array(),
46		  array(0),
47		  array(1, 2),
48
49		  // boolean values
50/*10*/	  true,
51		  false,
52		  TRUE,
53		  FALSE,
54
55		  // null vlaues
56/*14*/	  NULL,
57		  null,
58
59		  // objects
60/*16*/	  new sample(),
61
62		  // resource
63/*17*/	  $file_handle,
64
65		  // undefined variable
66/*18*/	  @$undefined_var,
67
68		  // unset variable
69/*19*/	  @$unset_var
70);
71
72//defining 'from' argument
73$from = "012atm";
74
75//defining 'to' argument
76$to = "atm012";
77
78// loop through with each element of the $strings array to test strtr() function
79$count = 1;
80for($index = 0; $index < count($strings); $index++) {
81  echo "-- Iteration $count --\n";
82  $str = $strings[$index];
83  var_dump( strtr($str, $from, $to) );
84  $count ++;
85}
86
87fclose($file_handle);  //closing the file handle
88
89?>
90===DONE===
91--EXPECTF--
92*** Testing strtr() function: with unexpected inputs for 'str' ***
93-- Iteration 1 --
94string(1) "a"
95-- Iteration 2 --
96string(1) "t"
97-- Iteration 3 --
98string(2) "-m"
99-- Iteration 4 --
100string(4) "ta.5"
101-- Iteration 5 --
102string(5) "-ma.5"
103-- Iteration 6 --
104string(12) "tatm34567aaa"
105-- Iteration 7 --
106
107Warning: strtr() expects parameter 1 to be string, array given in %s on line %d
108NULL
109-- Iteration 8 --
110
111Warning: strtr() expects parameter 1 to be string, array given in %s on line %d
112NULL
113-- Iteration 9 --
114
115Warning: strtr() expects parameter 1 to be string, array given in %s on line %d
116NULL
117-- Iteration 10 --
118string(1) "t"
119-- Iteration 11 --
120string(0) ""
121-- Iteration 12 --
122string(1) "t"
123-- Iteration 13 --
124string(0) ""
125-- Iteration 14 --
126string(0) ""
127-- Iteration 15 --
128string(0) ""
129-- Iteration 16 --
130string(13) "s02ple objec1"
131-- Iteration 17 --
132
133Warning: strtr() expects parameter 1 to be string, resource given in %s on line %d
134NULL
135-- Iteration 18 --
136string(0) ""
137-- Iteration 19 --
138string(0) ""
139===DONE===
140