Jump to content

benanamen

Members
  • Posts

    2,134
  • Joined

  • Last visited

  • Days Won

    42

Posts posted by benanamen

  1. 2 hours ago, Psycho said:

    The OP never stated "where" the value is coming from.

    Which is why you cant say "@Barand's test makes more sense." Based on OP's code, he expects there could be a negative int, otherwise he wouldn't be checking for it.

    If "int" is negative, @Barands solution will fail (false).

    var_dump(ctype_digit('-128')); // False

    Casting the string to an int wouldn't work either.

    $x =  (int) '5';

    Yes , it will make it an int and support negative values, but it will cast "anything" you put in there into an int such as the following and then you cannot check if it is a real int.

    $x =  (int) 'xx'; // int 0

     

    So what to do then? One option that also addresses the source as a string argument would be the following.

    function fact($x): int
    {
        return $x <= 1 ? 1 : $x * fact($x - 1);
    }
    
    $x = '-2147483648';// OK
    $x = -2147483648;// OK 
    $x = 5; // OK
    $x = '5';// OK
    $x = 'ABC'; // Fail: Please provide an integer                   
    echo (filter_var($x, FILTER_VALIDATE_INT) === 0 || filter_var($x, FILTER_VALIDATE_INT)) ? fact($x) : 'Please provide an integer';

     

     

  2. Hi @Barand,

    I am wondering why you would be introducing strings when this is specifically about int's. IMO is_int should be used instead of ctype_digit.

    OP, the else is redundant. You can remove it. Additionally, IMO the int check should not be in the function. The function/method should do one thing and do it well.

    As is example
     

    function fact($x)
    {
        if (!is_int($x)) {
            return 'Please provide an integer';
        }
        if ($x <= 1) {
            return 1;
        }
        return $x * fact($x - 1);
    }
    
    echo fact(5);

    Suggested Example

    function fact(int $x): int
    {
        return $x <= 1 ? 1 : $x * fact($x - 1);
    }
    
    $x = 5;
    echo is_int($x) ? fact($x) : 'Please provide an integer';

     

     

     

     

  3. 5 hours ago, SaranacLake said:

    !== FALSE)

    Anytime you see this type if thing, most usually in an if, it just points to a lack of understanding of basic functions. In your case, what your really saying is while this thing is true, do something. As with if, while is by default a truthy check so you simply just need to do while (expr).

  4. 27 minutes ago, platzDB said:

    It has been suggested

    What should be suggested is that you stop using dangerous obsolete mysql code that has been completely removed from PHP and was warned about for well over ten years and to stop running a php version that is hundreds of releases behind and reached end of life long ago. Toss that code in the trash. Every bit of it is bad.

    You need to use PDO with Prepared Statements. This tutorial will get you going.

    https://phpdelusions.net/pdo

  5. 19 minutes ago, ManieE said:

    Not sure what this means...

    It means you posted this in multiple forums and someone already took the time in another forum to answer you so we are not going to waste more experts time answering something that has already been answered.

    • Like 1
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.