-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path0804.php
More file actions
52 lines (42 loc) · 1.62 KB
/
0804.php
File metadata and controls
52 lines (42 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<?php
declare (strict_types = 1);
// strict types only affect function argument and return type checking
echo 'April 8th';
$value = 10;
$value ??= 8;
echo $value;
var_dump($value);
//This is called type casting. means we force php to convert the data from one type to another.
$value = (string) 13;
var_dump($value);
echo '<br>';
// here string is auto converted to int for below expression.
// This is called type juggling
$result = $value + 10;
var_dump($result);
/**
*
* Ah, with declare(strict_types=1);, PHP will enforce strict typing.
In your case, the type cast (string) 13 will not raise any errors, but when you try to add the string "13" (which is still a string) to the integer 10, PHP will attempt to convert the string to an integer automatically, because + expects numbers.
However, strict types only affect function argument and return type checking, not automatic type conversions during operations like addition. So, in this case, the addition still works as PHP will convert the string to an integer and the result will still be 23.
To summarize:
Strict typing affects function argument/return types.
Type juggling still occurs during operations like +.
*
*
*/
$hname = 'Dilip';
// function myName(string $name) use ($hname)
// {
// return "My Name is $name, $hname";
// }
// use keyword is only for anonymous functions
$myName = function (string $name) use ($hname) {
return "My name is $name $hname Parmar";
};
echo $myName('Janak');
echo '<br>';
// 2 ways to define constants. we are not using $ sign when we define constants.
const ME = 100;
define("SUR", 'Parmar');
echo "I am " . SUR . " and my age is " . ME;