Hi guys, I just tried to experiment with user function by making them accept only particular datatypes. I am stuck here. I pass a integer to the function but the function says something wierd: 'Argument 1 must be integer, but integer given in ... on line...' Any idea what I doing wrong? <?php $a=21; echo gettype($a);//outpts integer function asl(integer $age, string $sex, string $location) { return "$age,$sex,$location"; } asl($a,'M','Thane'); /* outputs error : Catchable fatal error: Argument 1 passed to asl() must be an instance of integer, integer given, called in C:\wamp\www\phpxp\function.php on line 8 and defined in C:\wamp\www\phpxp\function.php on line 4 */ ?> PHP: Thanks -Rohan.
i'm not really sure if typecasting is allowed in the function parameter part (i rarely typecast my vars in php actually unless its crucial) try removing those and see what happens.
$a=21; //echo gettype($a);//outpts integer function asl($age, $sex, $location) { return "$age,$sex,$location"; } asl($a,'M','Thane'); PHP: It's look like you trying to programming in other language..lol
The problem with php (and some other similar languages), they are not developed for mathematical or scientific purposes, they sometimes can make mistakes even for the simplest mathematics. For example $m = 30; $n = 5; if($m - $n == 5) { } this statement should be right, but sometimes, 30-5 doesn't result 5, and it results 5,000000000001 or sometihng like that. When you echo it, or check whether it is a integer or not, php cannot tell the difference. php is not that sophisticated. This is some error (or bug), i encountered a few times during my php developer career. (int)((int)$m - (int)$n) == 5 could be more precise. What i mean is, Just be careful while doing mathematical operations. Thats why mathematicians and other scientists never use any language other than fortran.
All these responses actually come as a shocker to me Anywayz, I will be careful in its mathematical ability(?). Also I am reporting this to PHP.net. What if its a bug?
PHP > 5 has a Type Hinting, but it's applicable only for object and array function parameters. Other type declarations in functions parameters isn't supported.