Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $var = "10";
- $value = 10 + $var;
- var_dump($value); // int(20)
- $var = "10";
- $value = 10 + $var;
- $value = (string)$value;
- var_dump($value); // string(2) "20"
- $var = "10";
- $value = (int)$var;
- $value = $value . ' TaDa!';
- var_dump($value); // string(8) "10 TaDa!"
- // example 1
- $foo = 0;
- $foo = (string)$foo;
- $foo = '# of Reasons for the programmer to type cast $foo as a string: ' . $foo;
- // example 2
- $foo = 0;
- $foo = (int)$foo;
- $foo = '# of Reasons for the programmer to type cast $foo as a string: ' . $foo;
- Menu Item 1 .............. $ 4
- Menu Item 2 .............. $ 7.5
- Menu Item 3 .............. $ 3
- $price = '7.50'; // a string from the database layer.
- echo 'Menu Item 2 .............. $ ' . (float)$price;
- var a = 5;
- var b = "10"
- var incorrect = a + b; // "510"
- var correct = a + Number(b); // 15
- console.log("5" > "10" ? "true" : "false"); // true
- echo "5" > "10" ? "true" : "false"; // false!
- function testprint(string $a) {
- echo $a;
- }
- $test = 5;
- testprint((string)5); // "Catchable fatal error: Argument 1 passed to testprint()
- // must be an instance of string, string given" WTF?
- console.log("0" ? "true" : "false"); // True, as expected. Non-empty string.
- echo "0" ? "true" : "false"; // False! This one probably causes a lot of bugs.
- $val = "test";
- $val2 = "10";
- $intval = (int)$val; // 0
- $intval2 = (int)$val2; // 10
- $boolval = (bool)$intval // false
- $boolval2 = (bool)$intval2 // true
- $props = (array)$myobject // associative array of $myobject's properties
- function get_by_id ($id) {
- $id = (int)$id;
- $q = "SELECT * FROM table WHERE id=$id LIMIT 1";
- ........
- }
- $amount = (float) $_POST['amount'];
- if( $amount > 0 ){
- // Do some maths
- }
- $tags = _GET['tags'];
- foreach ($tags as $tag) {
- echo 'tag: ', $tag;
- }
- $tags = (array) _GET['tags'];
- foreach ($tags as $tag) {
- echo 'tag: ', $tag;
- }
- { "user" : { "id" : "1", "name" : "Bob" } }
- { "user" : { "id" : 1, "name" : "Bob" } }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement