From 432aeb74d5a49e6f0efd1063113cef099c93aef6 Mon Sep 17 00:00:00 2001 From: murphy Date: Mon, 28 Dec 2009 07:27:12 +0000 Subject: Copying changes and fixes for 0.9.0rc3 from terminal-encoder branch over to trunk. --- test/scanners/php/strings.in.php | 360 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 360 insertions(+) create mode 100644 test/scanners/php/strings.in.php (limited to 'test/scanners/php/strings.in.php') diff --git a/test/scanners/php/strings.in.php b/test/scanners/php/strings.in.php new file mode 100644 index 0000000..dd6ffc7 --- /dev/null +++ b/test/scanners/php/strings.in.php @@ -0,0 +1,360 @@ +Single quoted + + +Double quoted + + + +Heredoc + +Example #1 Invalid example + + +Example #2 Heredoc string quoting example + +foo = 'Foo'; + $this->bar = array('Bar1', 'Bar2', 'Bar3'); + } +} + +$foo = new foo(); +$name = 'MyName'; + +echo <<foo. +Now, I am printing some {$foo->bar[1]}. +This should print a capital 'A': \x41 +EOT; +?> + +The above example will output: + +My name is "MyName". I am printing some Foo. +Now, I am printing some Bar2. +This should print a capital 'A': A + +Example #3 Heredoc in arguments example + + + +Example #4 Using Heredoc to initialize static values + + + +Example #5 Using double quotes in Heredoc + + + +Nowdoc + +Example #6 Nowdoc string quoting example + +foo = 'Foo'; + $this->bar = array('Bar1', 'Bar2', 'Bar3'); + } +} + +$foo = new foo(); +$name = 'MyName'; + +echo <<<'EOT' +My name is "$name". I am printing some $foo->foo. +Now, I am printing some {$foo->bar[1]}. +This should not print a capital 'A': \x41 +EOT; +?> +The above example will output: + +My name is "$name". I am printing some $foo->foo. +Now, I am printing some {$foo->bar[1]}. +This should not print a capital 'A': \x41 + +Example #7 Static data example + + + +Variable parsing + +When a string is specified in double quotes or with heredoc, variables are parsed within it. + +There are two types of syntax: a simple one and a complex one. The simple syntax is the most common and convenient. It provides a way to embed a variable, an array value, or an object property in a string with a minimum of effort. + +The complex syntax was introduced in PHP 4, and can be recognised by the curly braces surrounding the expression. + +Simple syntax + +If a dollar sign ($) is encountered, the parser will greedily take as many tokens as possible to form a valid variable name. Enclose the variable name in curly braces to explicitly specify the end of the name. + + +Similarly, an array index or an object property can be parsed. With array indices, the closing square bracket (]) marks the end of the index. The same rules apply to object properties as to simple variables. + + 'red', 'banana' => 'yellow'); + +// Works, but note that this works differently outside a string +echo "A banana is $fruits[banana]."; + +// Works +echo "A banana is {$fruits['banana']}."; + +// Works, but PHP looks for a constant named banana first, as described below. +echo "A banana is {$fruits[banana]}."; + +// Won't work, use braces. This results in a parse error. +echo "A banana is $fruits['banana']."; + +// Works +echo "A banana is " . $fruits['banana'] . "."; + +// Works +echo "This square is $square->width meters broad."; + +// Won't work. For a solution, see the complex syntax. +echo "This square is $square->width00 centimeters broad."; +?> +For anything more complex, you should use the complex syntax. + +Complex (curly) syntax + +This isn't called complex because the syntax is complex, but because it allows for the use of complex expressions. + +In fact, any value in the namespace can be included in a string with this syntax. Simply write the expression the same way as it would appear outside the string, and then wrap it in { and }. Since { can not be escaped, this syntax will only be recognised when the $ immediately follows the {. Use {\$ to get a literal {$. Some examples to make it clear: + +width}00 centimeters broad."; + +// Works +echo "This works: {$arr[4][3]}"; + +// This is wrong for the same reason as $foo[bar] is wrong outside a string. +// In other words, it will still work, but only because PHP first looks for a +// constant named foo; an error of level E_NOTICE (undefined constant) will be +// thrown. +echo "This is wrong: {$arr[foo][3]}"; + +// Works. When using multi-dimensional arrays, always use braces around arrays +// when inside of strings +echo "This works: {$arr['foo'][3]}"; + +// Works. +echo "This works: " . $arr['foo'][3]; + +echo "This works too: {$obj->values[3]->name}"; + +echo "This is the value of the var named $name: {${$name}}"; + +echo "This is the value of the var named by the return value of getName(): {${getName()}}"; + +echo "This is the value of the var named by the return value of \$object->getName(): {${$object->getName()}}"; +?> +It is also possible to access class properties using variables within strings using this syntax. + +$bar}\n"; +echo "{$foo->$baz[1]}\n"; +?> +The above example will output: +I am bar. +I am bar. + +Note: Functions, method calls, static class variables, and class constants inside {$} work since PHP 5. However, the value accessed will be interpreted as the name of a variable in the scope in which the string is defined. Using single curly braces ({}) will not work for accessing the return values of functions or methods or the values of class constants or static class variables. + +String access and modification by character + +Characters within strings may be accessed and modified by specifying the zero-based offset of the desired character after the string using square array brackets, as in $str[42]. Think of a string as an array of characters for this purpose. + +Note: Strings may also be accessed using braces, as in $str{42}, for the same purpose. However, this syntax is deprecated as of PHP 5.3.0. Use square brackets instead, such as $str[42]. + +Warning +Writing to an out of range offset pads the string with spaces. Non-integer types are converted to integer. Illegal offset type emits E_NOTICE. Negative offset emits E_NOTICE in write but reads empty string. Only the first character of an assigned string is used. Assigning empty string assigns NUL byte. + +Example #8 Some string examples + + +String conversion to numbers + + + +\n"; +?> + +If you want a parsed variable surrounded by curly braces, just double the curly braces: + + + +Although current documentation says 'A string literal can be specified in four different ways: ...', actually there is a fifth way to specify a (binary) string: + + -- cgit v1.2.1