Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Scrie o funcție în PHP 7+ care să verifice un număr natural.
<?php
/**
* Created by PhpStorm.
* User: cosmi
* Date: 09-Apr-18
* Time: 7:57 AM
*/
function CheckIfIsNumber($number = null){
if($number === (object)$number)
return false;
if ($number <= 0)
return false;
if($number >= 9876543210)
return false;
if ($number === (int)$number)
return true;
}
echo(assert(CheckIfIsNumber(42)));
echo(assert(CheckIfIsNumber("42")));
echo(assert(CheckIfIsNumber(0)));
echo(assert(CheckIfIsNumber(-1)));
echo(assert(CheckIfIsNumber(new stdClass())));
echo(assert(CheckIfIsNumber(111454013352)));
echo(assert(CheckIfIsNumber(123)));
echo(assert(CheckIfIsNumber(0111454013352)));
echo(assert(CheckIfIsNumber("42aaa")));
echo(assert(CheckIfIsNumber("4a2b")));
echo(assert(CheckIfIsNumber(1.2)));
echo(assert(CheckIfIsNumber(null)));
echo(CheckIfIsNumber((object)['caught' => true]));
echo(assert(CheckIfIsNumber((object)['caught' => true])));
echo(assert(CheckIfIsNumber('-2.0')));
echo(assert(CheckIfIsNumber(2.0)));
echo(assert(CheckIfIsNumber(array(42))));
echo(assert(CheckIfIsNumber(99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999)));
echo(assert(CheckIfIsNumber(0.9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999)));
echo(assert(CheckIfIsNumber(2)));
echo(assert(CheckIfIsNumber('b')));
echo(assert(CheckIfIsNumber("x100")));
echo(assert(CheckIfIsNumber((int)b'111')));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.