Advertisement
Guest User

Untitled

a guest
Jul 30th, 2016
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.99 KB | None | 0 0
  1. <?php
  2. // Show all errors
  3. error_reporting(E_ALL);
  4.  
  5. $great = 'fantastic';
  6.  
  7. // Won't work, outputs: This is { fantastic}
  8. echo "This is { $great}";
  9.  
  10. // Works, outputs: This is fantastic
  11. echo "This is {$great}";
  12. echo "This is ${great}";
  13.  
  14. // Works
  15. echo "This square is {$square->width}00 centimeters broad.";
  16.  
  17.  
  18. // Works, quoted keys only work using the curly brace syntax
  19. echo "This works: {$arr['key']}";
  20.  
  21.  
  22. // Works
  23. echo "This works: {$arr[4][3]}";
  24.  
  25. // This is wrong for the same reason as $foo[bar] is wrong outside a string.
  26. // In other words, it will still work, but only because PHP first looks for a
  27. // constant named foo; an error of level E_NOTICE (undefined constant) will be
  28. // thrown.
  29. echo "This is wrong: {$arr[foo][3]}";
  30.  
  31. // Works. When using multi-dimensional arrays, always use braces around arrays
  32. // when inside of strings
  33. echo "This works: {$arr['foo'][3]}";
  34.  
  35. // Works.
  36. echo "This works: " . $arr['foo'][3];
  37.  
  38. echo "This works too: {$obj->values[3]->name}";
  39.  
  40. echo "This is the value of the var named $name: {${$name}}";
  41.  
  42. echo "This is the value of the var named by the return value of getName(): {${getName()}}";
  43.  
  44. echo "This is the value of the var named by the return value of $object->getName(): {${$object->getName()}}";
  45.  
  46. // Won't work, outputs: This is the return value of getName(): {getName()}
  47. echo "This is the return value of getName(): {getName()}";
  48. ?>
  49.  
  50. $a = 'abcd';
  51. $out = "$a $a"; // "abcd abcd";
  52.  
  53. $out = "{$a} {$a}"; // same
  54.  
  55. $out = "$aefgh";
  56.  
  57. $out = "${a}efgh"; // or
  58. $out = "{$a}efgh";
  59.  
  60. <?php
  61.  
  62. $a = '12345';
  63.  
  64. // This works:
  65. echo "qwe{$a}rty"; // qwe12345rty, using braces
  66. echo "qwe" . $a . "rty"; // qwe12345rty, concatenation used
  67.  
  68. // Does not work:
  69. echo 'qwe{$a}rty'; // qwe{$a}rty, single quotes are not parsed
  70. echo "qwe$arty"; // qwe, because $a became $arty, which is undefined
  71.  
  72. ?>
  73.  
  74. $number = 4;
  75. print "You have the {$number}th edition book";
  76. //output: "You have the 4th edition book";
  77.  
  78. $data = $wpdb->get_results("select * from {$wpdb->prefix}download_monitor_files");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement