检测数据类型php函数集

 更新时间:2016年11月25日 15:09  点击:2319

检测数据类型php教程函数集

检测数据类型即对数据类型进行检测,判断所检测类型是否属于检测类型,符合则返回真,否则返回假。检测数据类型定义如下:

is_bool

是否为布尔类型,例,is_bool(srue)  is_bool(false)


is_string

是否为字符串型,例,is_string(‘string’)  is_string(1234)


is_float/double

是否为浮点型,例,is_float(3.1415)  is_float(‘3.1415’)


is_integer/int

是否为整型,例,is_integer(34)  is_integer(‘34’)


is_null

是否为空值,例,is_null(null)


is_array

是否为数组,例,is_array($arr)


is_object

是否为一个对象,例,is_object($obj)


is_numeric

是否为数字或由数字组成的字符串,例,is_numeric(‘5’)  is_numeric(‘bcc110’)

 

示例

<?php

$boo="1234567890";

if(is_numeric($boo))

echo "变量boo属由数字组成的字符串类型:".$boo;

else

echo"无法判断";

?>

 

php教程 trim() 表单验证不为空实例,应该算是入门级的实例了,告诉你如何利用trim函数来删除空格然后判断用户提交的数据是否为空。

<html>
<body>
<form method="post" action="formerrorcheck.php">
<h1>contact information</h1>
<table>

<tr>
  <td><b>nickname:</b></td>
  <td><input type="text" name="nickname"></td>
</tr>

<tr>
  <td>title:</td>
  <td><input type="text" name="title"></td>
</tr>

<tr>
  <td><b>first name:</b></td>
  <td><input type="text" name="firstname"></td>
</tr>

<tr>
  <td>middle name:</td>
  <td><input type="text" name="middlename"></td>
</tr>

<tr>
  <td><b>last name:</b></td>
  <td><input type="text" name="lastname"></td>
</tr>

<tr>
  <td><b>primary email:</b></td>
  <td><input type="text" name="email"></td>
  <td width="20">&nbsp;</td>
  <td>secondary email:</td>
  <td><input type="text" name="secondaryemail"></td>
</tr>

<tr>
  <td>company name:</td>
  <td><input type="text" name="companyname"></td>
</tr>

<tr>
  <td>office address:</td>
  <td><input type="text" name="officeaddres1"></td>
  <td width="20">&nbsp;</td>
  <td>home address:</td>
  <td><input type="text" name="homeaddress"></td>
</tr>

<tr>
  <td></td>
  <td><input type="text" name="officeaddress2"></td>
</tr>

<tr>
  <td>city:</td>
  <td><input type="text" name="officecity"></td>
  <td width="20">&nbsp;</td>
  <td>&nbsp;</td>
  <td><input type="text" name="homecity"></td>
</tr>

<tr>
  <td>state:</td>
  <td><input type="text" name="officestate"></td>
  <td width="20">&nbsp;</td>
  <td>&nbsp;</td>
  <td><input type="text" name="homestate"></td>
</tr>

<tr>
  <td>zip:</td>
  <td><input type="text" name="officezip"></td>
  <td width="20">&nbsp;</td>
  <td>&nbsp;</td>
  <td><input type="text" name="homezip"></td>
</tr>

<tr>
  <td>phone:</td>
  <td><input type="text" name="officephone"></td>
  <td width="20">&nbsp;</td>
  <td>&nbsp;</td>
  <td><input type="text" name="homephone"></td>
</tr>

<tr>
  <td>birthday:</td>
  <td><input type="text" name="birthday"></td>
</tr>

<tr>
  <td>spouse name:</td>
  <td><input type="text" name="spousename"></td>
  <td width="20">&nbsp;</td>
  <td>childrens' names:</td>
  <td><input type="text" name="children"></td>
</tr>

<tr>
  <td>anniversary:</td>
  <td><input type="text" name="anniversary"></td>
</tr>

</table>

<br>
<br>
<br>
<input type="submit" value="submit">
<br>
<br>
<input type="reset"  value="clear the form">

</form>
</body>
</html>

<!-- formerrorcheck.php
<html>
<body>
<?php

  $errors=0;
  if (!trim($nickname)) {
      echo "<br><b>nickname</b> is required.";
     $errors++;
  }
 
  if (!trim($firstname)) {
      echo "<br><b>first name</b> is required.";
     $errors++;
  }
 
  if (!trim($lastname)) {
      echo "<br><b>last name</b> is required.";
      $errors++;
  }
 
  if (!trim($email)) {
      echo "<br><b>primary email address</b> is required.";
      $errors++;
  }

  if ($errors > 0)
      echo "<br><br><br>please use your browser's back button " .
        "to return to the form, and correct error(s)";
 
?>

</body>
</html>


这是个简单的验证函数

 

<?php
    function phone_validate($data, $desc) {
        $regex = "/^([2-9][0-9]{2})[2-9][0-9]{2}-[0-9]{4}/i";
        if(preg_match($regex, $data) != 1) {
            return "the '$desc' field isn't valid!";
        }
        return true;
    }
?>
 

php教程 strtotime()计算今天与指定日期之天数

$date1 = strtotime('2011-04-30'); //把日期转换成时间戳
$date2 = time(); //取当前时间的时间戳

$nowtime=strftime("%y年-%m月-%d日 ",$date2); //格式化输出日期

$days=round(($date1-$date2)/3600/24); //四舍五入

echo "今天是<font color="red">".$nowtime."</font>";
echo "<br/>距".strftime("%y年-%m月-%d日 ",$date1)."还有<font colr="red">".$days."</font>天";

echo date("y-m-d h:i:s",strtotime("now")). "<br />";
echo date("y-m-d h:i:s",strtotime("10 september 2000")). "<br />";
echo date("y-m-d h:i:s",strtotime("+2 day")). "<br />";
echo date("y-m-d h:i:s",strtotime("+1 week")). "<br />";
echo date("y-m-d h:i:s",strtotime("+1 week 2 days 4 hours 2 seconds")). "<br />";
echo date("y-m-d h:i:s",strtotime("next thursday")). "<br />";
echo date("y-m-d h:i:s",strtotime("last monday")). "<br />";
echo date("y-m-d h:i:s",strtotime("+3 day",strtotime('2001-01-01')));

php教程中checkbox值获取,显示,多选值获取

最简单checkbox获取值代码

<html>
<head>
<title>checkbox demo</title>
</head>
<body>
<h1>checkbox demo</h1>

<h3>demonstrates checkboxes</h3>
<form action ="handleformcheckbox.php">

<ul>
  <li><input type ="checkbox" name ="chkfries" value ="11.00">fries</li>
  <li><input type ="checkbox" name ="chksoda"  value ="12.85">soda</li>
  <li><input type ="checkbox" name ="chkshake" value ="1.30">shake</li>
  <li><input type ="checkbox" name ="chkketchup" value =".05">ketchup</li>
</ul>
<input type ="submit">
</form>

</body>
</html>


<!-- handleformcheckbox.php
<html>
<head>
<title>checkbox demo</title>
</head>
<body>
<h3>demonstrates reading checkboxes</h3>
<?
print <<<here
chkfries: $chkfries <br>
chksoda: $chksoda <br>
chkshake: $chkshake <br>
chkketchup: $chkketchup <br>
<hr>

here;

$total = 0;

if (!empty($chkfries)){
  print ("you chose fries <br>");
  $total = $total + $chkfries;
}

if (!empty($chksoda)){
  print ("you chose soda <br>");
  $total = $total + $chksoda;
}

if (!empty($chkshake)){
  print ("you chose shake <br>");
  $total = $total + $chkshake;
}

if (!empty($chkketchup)){
  print ("you chose ketchup <br>");
  $total = $total + $chkketchup;
}

print "the total cost is $$total";

?>
</body>
</html>
-->

实例

<html>
<head>
    <title>using default checkbox values</title>
</head>
<body>
<?php
$food = $_get[food];
$self = htmlentities($_server['php_self']);
if (!empty($food)) {
    echo "the foods selected are:<br />";
    foreach($food as $foodstuf)
    {
        echo "<strong>".htmlentities($foodstuf)."</strong><br />";
    }
}
else
{
    echo ("<form action="$self" ");
    echo ('method="get">
    <fieldset>
        <label>italian <input type="checkbox" name="food[]" value="italian" />
</label>
        <label>mexican <input type="checkbox" name="food[]" value="mexican" />
</label>
        <label>chinese <input type="checkbox" name="food[]" value="chinese"
        checked="checked" /></label>
    </fieldset>
    <input type="submit" value="go!" >');
}
?>
</body>
</html>


多选checkbox

<?php
$options = array('option 1', 'option 2', 'option 3');

$valid = true;
if (is_array($_get['input'])) {
    $valid = true;
    foreach($_get['input'] as $input) {
        if (!in_array($input, $options)) {
            $valid = false;
        }
    }
    if ($valid) {
        //process input
    }
}
?>

实例checkbox多值获取

<html>
<head>
    <title>using default checkbox values</title>
</head>
<body>
<?php
$food = $_get["food"];
if (!empty($food)){
    echo "the foods selected are: <strong>";
    foreach($food as $foodstuff){
        echo '<br />'.htmlentities($foodstuff);
    }
    echo "</strong>.";
}
else {
    echo ('
    <form action="'. htmlentities($_server["php_self"]).'" method="get">
        <fieldset>
            <label>
                italian
                <input type="checkbox" name="food[]" value="italian" />
            </label>
            <label>
                mexican
                <input type="checkbox" name="food[]" value="mexican" />
            </label>
            <label>
                chinese
                <input type="checkbox" name="food[]" value="chinese" checked="checked" />
            </label>
        </fieldset>
        <input type="submit" value="go!" />
    </form> ');
    }
?>
</body>
</html>

$globals --- 保存所有全局变量(只在当前页面中的) get_defined_vars() --- 返回由所有已定义变量所组成的数组(包括全局变量,超全局变量等) get_defined_constants() --- 返回由所有已定义常量所组成的数组

function cleanglobal($global_array, $arg, $specialchars = true, $default = null) {
if(key_exists($arg, $global_array) && $global_array[$arg] != null && $global_array[$arg] != "") {
if($specialchars) {
return htmlspecialchars($global_array[$arg]);
} else {
return $global_array[$arg];
}
} else {
return $default;
}
}


table of contents
superglobals — superglobals are built-in variables that are always available in all scopes
$globals — references all variables available in global scope
$_server — server and execution environment information
$_get — http get variables
$_post — http post variables
$_files — http file upload variables
$_request — http request variables
$_session — session variables
$_env — environment variables
$_cookie — http cookies
$php教程_errormsg — the previous error message
$http_raw_post_data — raw post data
$http_response_header — http response headers
$argc — the number of arguments passed to script
$argv — array of arguments passed to script

 

[!--infotagslink--]

相关文章

  • php正确禁用eval函数与误区介绍

    eval函数在php中是一个函数并不是系统组件函数,我们在php.ini中的disable_functions是无法禁止它的,因这他不是一个php_function哦。 eval()针对php安全来说具有很...2016-11-25
  • php中eval()函数操作数组的方法

    在php中eval是一个函数并且不能直接禁用了,但eval函数又相当的危险了经常会出现一些问题了,今天我们就一起来看看eval函数对数组的操作 例子, <?php $data="array...2016-11-25
  • Python astype(np.float)函数使用方法解析

    这篇文章主要介绍了Python astype(np.float)函数使用方法解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下...2020-06-08
  • Python中的imread()函数用法说明

    这篇文章主要介绍了Python中的imread()函数用法说明,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-03-16
  • C# 中如何取绝对值函数

    本文主要介绍了C# 中取绝对值的函数。具有很好的参考价值。下面跟着小编一起来看下吧...2020-06-25
  • php中浮点型(float)和整型(integer)数据类型详解

    文章分析了关于php中浮点型(float)和整型(integer)数据类型的用法区别以及在那种情况下会出现数据长度不够。 取值只能为True或者False,当其他类型转化为boolean类...2016-11-25
  • C#学习笔记- 随机函数Random()的用法详解

    下面小编就为大家带来一篇C#学习笔记- 随机函数Random()的用法详解。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2020-06-25
  • c# 数据类型占用的字节数介绍

    本篇文章主要是对c#中数据类型占用的字节数进行了详细的介绍。需要的朋友可以过来参考下,希望对大家有所帮助...2020-06-25
  • Javascript类型转换的规则实例解析

    这篇文章主要介绍了Javascript类型转换的规则实例解析,涉及到javascript类型转换相关知识,对本文感兴趣的朋友一起学习吧...2016-02-27
  • 金额阿拉伯数字转换为中文的自定义函数

    CREATE FUNCTION ChangeBigSmall (@ChangeMoney money) RETURNS VarChar(100) AS BEGIN Declare @String1 char(20) Declare @String2 char...2016-11-25
  • Android开发中findViewById()函数用法与简化

    findViewById方法在android开发中是获取页面控件的值了,有没有发现我们一个页面控件多了会反复研究写findViewById呢,下面我们一起来看它的简化方法。 Android中Fin...2016-09-20
  • C++中 Sort函数详细解析

    这篇文章主要介绍了C++中Sort函数详细解析,sort函数是algorithm库下的一个函数,sort函数是不稳定的,即大小相同的元素在排序后相对顺序可能发生改变...2022-08-18
  • PHP用strstr()函数阻止垃圾评论(通过判断a标记)

    strstr() 函数搜索一个字符串在另一个字符串中的第一次出现。该函数返回字符串的其余部分(从匹配点)。如果未找到所搜索的字符串,则返回 false。语法:strstr(string,search)参数string,必需。规定被搜索的字符串。 参数sea...2013-10-04
  • PHP函数分享之curl方式取得数据、模拟登陆、POST数据

    废话不多说直接上代码复制代码 代码如下:/********************** curl 系列 ***********************///直接通过curl方式取得数据(包含POST、HEADER等)/* * $url: 如果非数组,则为http;如是数组,则为https * $header:...2014-06-07
  • php中的foreach函数的2种用法

    Foreach 函数(PHP4/PHP5)foreach 语法结构提供了遍历数组的简单方式。foreach 仅能够应用于数组和对象,如果尝试应用于其他数据类型的变量,或者未初始化的变量将发出错误信息。...2013-09-28
  • 深入理解PHP变量的值类型和引用类型

    在PHP中,大部分变量类型,如字符串,整型,浮点,数组等都是值类型的,而类和对象是引用类型,在使用的时候,需要注意这一点。看到网友在讨论PHP的&符号,要彻底理解它的用法,就有必要讨论一下变量的两种形式。PHP的变量在内存中是这样...2015-10-23
  • C语言中free函数的使用详解

    free函数是释放之前某一次malloc函数申请的空间,而且只是释放空间,并不改变指针的值。下面我们就来详细探讨下...2020-04-25
  • PHP函数strip_tags的一个bug浅析

    PHP 函数 strip_tags 提供了从字符串中去除 HTML 和 PHP 标记的功能,该函数尝试返回给定的字符串 str 去除空字符、HTML 和 PHP 标记后的结果。由于 strip_tags() 无法实际验证 HTML,不完整或者破损标签将导致更多的数...2014-05-31
  • SQL Server中row_number函数的常见用法示例详解

    这篇文章主要给大家介绍了关于SQL Server中row_number函数的常见用法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-12-08
  • Javascript中的数据类型之旅

    虽然Javascript是弱类型语言,但是,它也有自己的几种数据类型,分别是:Number、String、Boolean、Object、Udefined、Null。其中,Object属于复杂数据类型,Object 由无序的键值对组成。其余几种都属于简单数据类型。注意:变量...2015-10-21