note 12037 deleted from language.types.string.php by jeroen
| From: | jeroen@php.net | Date: | Wed, 23 May 2001 22:27:55 +0000 |
| Subject: | note 12037 deleted from language.types.string.php by jeroen | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-14202@lists.php.net to get a copy of this message | ||
Summary: Variables in a String.
Following strings are valid under PHP4 (at least PHP4.0.4pl1)
Ambiguous variable reference can be resolved {}.
It is not intuitive, since {} is not described in operator section.
"()","{}"is not a operator in PHP (I'm not sure), but it affects operator
precedence or resolution of ambiguity.
<?php
//===== Variable Definitions ===
echo('<br>String Test<br>');
class foo {
var $var = 'Object foo';
}
class bar {
var $var = 'Object bar';
var $foo;
function bar() {
$this->foo = new foo;
}
}
$arr1 = array('first' => 'first element of array');
$arr2['one']['two']['three'] = 'Test Array - assoc array';
$arr3[10][10][10] = 'Test Array - numeric index';
$arr4 = &$arr3[10][10];
$arr5[1] = new foo;
$arr6[1] = new bar;
$obj1 = new foo;
$obj2 = new bar;
//====== All of them are valid in PHP4.0.4pl1/Linux/Apache DSO ======
// Need {} for Assoc arrays
$str = "This is $arr1[first] and it works";
print($str."<br>n");
$str = "This is {$arr1['first']} and it works";
print($str."<br>n");
// Need {} for multi dimensional arrays
// AND quotations for Assoc arrays (w/o quotations, PHP raises notice errors)
$str = "This is {$arr2['one']['two']['three']} and it
works";
print($str."<br>n");
// Need {} for multi dimensional arrays
$str = "This is {$arr3[10][10][10]} and it works";
print($str."<br>n");
// Reference works as expected
$str = "This is $arr4[10] and it works";
print($str."<br>n");
// Array and Object requries {}
$str = "This is {$arr5[1]->var} and it works";
print($str."<br>n");
// Array and Object requires {}
$str = "This is {$arr6[1]->foo->var} and it works";
print($str."<br>n");
// Object
$str = "This is $obj1->var and it works";
print($str."<br>n");
// Nested Object requries {}
$str = "This is {$obj2->foo->var} and it works";
print($str."<br>n");
?>