Strange error: Passing integer but still it says argument is not an integer

Discussion in 'PHP' started by rohan_shenoy, Jul 30, 2008.

  1. #1
    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.
     
    rohan_shenoy, Jul 30, 2008 IP
  2. serialCoder

    serialCoder Guest

    Best Answers:
    0
    #2
    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.
     
    serialCoder, Jul 30, 2008 IP
  3. php-lover

    php-lover Active Member

    Messages:
    261
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    58
    #3
    $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
     
    php-lover, Jul 30, 2008 IP
  4. yleiko

    yleiko Peon

    Messages:
    74
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #4
    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.
     
    yleiko, Jul 30, 2008 IP
  5. rohan_shenoy

    rohan_shenoy Active Member

    Messages:
    441
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    60
    #5
    All these responses actually come as a shocker to me :D
    Anywayz, I will be careful in its mathematical ability(?).

    Also I am reporting this to PHP.net. What if its a bug? :)
     
    rohan_shenoy, Jul 30, 2008 IP
  6. wmtips

    wmtips Well-Known Member

    Messages:
    601
    Likes Received:
    70
    Best Answers:
    1
    Trophy Points:
    150
    #6
    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.
     
    wmtips, Jul 31, 2008 IP