Advertisement
Guest User

Untitled

a guest
Sep 2nd, 2015
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.96 KB | None | 0 0
  1. $var = "10";
  2. $value = 10 + $var;
  3. var_dump($value); // int(20)
  4.  
  5. $var = "10";
  6. $value = 10 + $var;
  7. $value = (string)$value;
  8. var_dump($value); // string(2) "20"
  9.  
  10. $var = "10";
  11. $value = (int)$var;
  12. $value = $value . ' TaDa!';
  13. var_dump($value); // string(8) "10 TaDa!"
  14.  
  15. // example 1
  16. $foo = 0;
  17. $foo = (string)$foo;
  18. $foo = '# of Reasons for the programmer to type cast $foo as a string: ' . $foo;
  19.  
  20. // example 2
  21. $foo = 0;
  22. $foo = (int)$foo;
  23. $foo = '# of Reasons for the programmer to type cast $foo as a string: ' . $foo;
  24.  
  25. Menu Item 1 .............. $ 4
  26. Menu Item 2 .............. $ 7.5
  27. Menu Item 3 .............. $ 3
  28.  
  29. $price = '7.50'; // a string from the database layer.
  30. echo 'Menu Item 2 .............. $ ' . (float)$price;
  31.  
  32. var a = 5;
  33. var b = "10"
  34. var incorrect = a + b; // "510"
  35. var correct = a + Number(b); // 15
  36.  
  37. console.log("5" > "10" ? "true" : "false"); // true
  38.  
  39. echo "5" > "10" ? "true" : "false"; // false!
  40.  
  41. function testprint(string $a) {
  42. echo $a;
  43. }
  44.  
  45. $test = 5;
  46. testprint((string)5); // "Catchable fatal error: Argument 1 passed to testprint()
  47. // must be an instance of string, string given" WTF?
  48.  
  49. console.log("0" ? "true" : "false"); // True, as expected. Non-empty string.
  50.  
  51. echo "0" ? "true" : "false"; // False! This one probably causes a lot of bugs.
  52.  
  53. $val = "test";
  54. $val2 = "10";
  55. $intval = (int)$val; // 0
  56. $intval2 = (int)$val2; // 10
  57. $boolval = (bool)$intval // false
  58. $boolval2 = (bool)$intval2 // true
  59. $props = (array)$myobject // associative array of $myobject's properties
  60.  
  61. function get_by_id ($id) {
  62. $id = (int)$id;
  63. $q = "SELECT * FROM table WHERE id=$id LIMIT 1";
  64. ........
  65. }
  66.  
  67. $amount = (float) $_POST['amount'];
  68.  
  69. if( $amount > 0 ){
  70. // Do some maths
  71. }
  72.  
  73. $tags = _GET['tags'];
  74. foreach ($tags as $tag) {
  75. echo 'tag: ', $tag;
  76. }
  77.  
  78. $tags = (array) _GET['tags'];
  79. foreach ($tags as $tag) {
  80. echo 'tag: ', $tag;
  81. }
  82.  
  83. { "user" : { "id" : "1", "name" : "Bob" } }
  84.  
  85. { "user" : { "id" : 1, "name" : "Bob" } }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement