Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // Show all errors
- error_reporting(E_ALL);
- $great = 'fantastic';
- // Won't work, outputs: This is { fantastic}
- echo "This is { $great}";
- // Works, outputs: This is fantastic
- echo "This is {$great}";
- echo "This is ${great}";
- // Works
- echo "This square is {$square->width}00 centimeters broad.";
- // Works, quoted keys only work using the curly brace syntax
- echo "This works: {$arr['key']}";
- // 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()}}";
- // Won't work, outputs: This is the return value of getName(): {getName()}
- echo "This is the return value of getName(): {getName()}";
- ?>
- $a = 'abcd';
- $out = "$a $a"; // "abcd abcd";
- $out = "{$a} {$a}"; // same
- $out = "$aefgh";
- $out = "${a}efgh"; // or
- $out = "{$a}efgh";
- <?php
- $a = '12345';
- // This works:
- echo "qwe{$a}rty"; // qwe12345rty, using braces
- echo "qwe" . $a . "rty"; // qwe12345rty, concatenation used
- // Does not work:
- echo 'qwe{$a}rty'; // qwe{$a}rty, single quotes are not parsed
- echo "qwe$arty"; // qwe, because $a became $arty, which is undefined
- ?>
- $number = 4;
- print "You have the {$number}th edition book";
- //output: "You have the 4th edition book";
- $data = $wpdb->get_results("select * from {$wpdb->prefix}download_monitor_files");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement