# php如何判断一个字符串是不是纯数字，is\_int不好使。

**URL:** <https://www.shiqidu.com/t/topic/226>\
**Category:** 问题求助\
**Tags:** 问与答\
**Created:** [October 1, 2017, 4:13am UTC](https://www.shiqidu.com/t/topic/226 "2017-10-01T04:13:07Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![boyfix](https://www.shiqidu.com/user_avatar/www.shiqidu.com/boyfix/32/16_2.png) [@boyfix](https://www.shiqidu.com/u/boyfix)\
**Post date:** [October 1, 2017, 4:13am UTC](https://www.shiqidu.com/t/topic/226/1 "2017-10-01T04:13:07Z")

</div>

我通过$\_REQUEST接受一个用户输入的值，我要判断这个值是不是纯数字(id)或者不是纯数字(名称)，来组装不同的sql。

```php
$project = isset($_REQUEST['project']) ? trim($_REQUEST['project']) : 0;  

```

我想实现的是这样的逻辑

```php
$where = '';  
if (is_int($project)){  
    $where .= &quot; and p.id = {$project}&quot;  
} else{  
    $where .= &quot; and p.name like '%{$project}%'&quot;;  
}  

```

但是如果我输入一个23会变成“23”，导致is\_int不好用。  
php有没有什么函数能够判断一个字符串是不是纯数字的？

---

<div class="post-metadata">

**Author:** ![Leon](https://www.shiqidu.com/user_avatar/www.shiqidu.com/leon/32/5_2.png) [@Leon](https://www.shiqidu.com/u/Leon)\
**Post date:** [October 1, 2017, 4:14am UTC](https://www.shiqidu.com/t/topic/226/2 "2017-10-01T04:14:44Z")

</div>

```php
function isInteger($input) {  
   return (is_numeric(strval($input)));  
}  

```

---

<div class="post-metadata">

**Author:** ![boyfix](https://www.shiqidu.com/user_avatar/www.shiqidu.com/boyfix/32/16_2.png) [@boyfix](https://www.shiqidu.com/u/boyfix)\
**Post date:** [October 1, 2017, 4:23am UTC](https://www.shiqidu.com/t/topic/226/3 "2017-10-01T04:23:31Z")

</div>

@saonian: 好用。多谢
