PHP round() 函数四舍五入php函数
x,是要处理的浮点型数据
prec,保存几位小数
提示和注释
注释:PHP 默认不能正确处理类似 "12,300.2" 的字符串。
注释:prec 参数是在 PHP 4 中被引入的。。
*/
//实例浮点数的截断函数,返回值仍为浮点数。
$foo = round(3.4); // $foo == 3.0
$foo = round(3.5); // $foo == 4.0
$foo = round(3.6); // $foo == 4.0
$foo = round(3.6, 0); // equivalent with above
$foo = round(1.95583, 2); // $foo == 1.96
$foo = round(1241757, -3); // $foo == 1242000
//round实例二
echo(round(0.68));
echo(round(0.40));
echo(round(0.3));
echo(round(-3.30));
echo(round(-4.90));
/*
1
0
0
-3
-5
*/
//返回将 x 根据指定精度 prec (十进制小数点后数字的数目)进行四舍五入的结果。prec 也可以是负数或零(默认值
if($_POST[submit]){
$username= str_replace(" ","",$_POST[username]);
$sql="select * from user_list where `username` = '$username'";
$query=mysql教程_query($sql);
$us=is_array($row=mysql_fetch_array($query));
$ps教程= $us ? md5($_POST[password].ALL_PS)== $row[password] : FALSE;
$pw=$ps?$_SESSION["code"]==$_POST[code]:FALSE;
if($ps){
$_SESSION[uid]=$row[uid];
$_SESSION[user_shell]=md5($row[username].$row[password].ALL_PS);
$_SESSION[times]=mktime();
if($pw){
echo "登陆成功";
}
else{
echo "验证码错误";
session_destroy();
}
}else{
echo "密码或者用户名错误";
session_destroy();
}
}
?>
<form action="" method="post">
用户名:<input type="text" name="username" style="height:23px" /><br>
密 码:<input type="password" name="password" style="height:23px" /><br>
验证码:<input type="code" name="code" size="10" style="height:23px" />
<img src="imgcode.php">
<br><br>
<input type="submit" name="submit" value="登陆"/>
</form>
数据库教程结构
--
-- 表的结构 `user_list`
--
CREATE TABLE `user_list` (
`uid` int(11) NOT NULL auto_increment,
`m_id` varchar(5) NOT NULL,
`username` varchar(25) NOT NULL,
`password` varchar(64) NOT NULL,
PRIMARY KEY (`uid`)
) ENGINE=MyISAM DEFAULT CHARSET=gb2312 AUTO_INCREMENT=11 ;
--
-- 导出表中的数据 `user_list`
--
INSERT INTO `user_list` (`uid`, `m_id`, `username`, `password`) VALUES
(9, '1', 'admin', '75cf565ad38d0f837c43363e402b1a00'),
(10, '2', 'admin2', '75cf565ad38d0f837c43363e402b1a00');
<?php教程
$arr=array(
0=>array('title' => '新闻1', 'viewnum' => 123, 'content' => 'ZAQXSWedcrfv'),
1=>array('title' => '新闻2', 'viewnum' => 99, 'content' => 'QWERTYUIOPZXCVBNM')
);
?>
如果你想统计数组$arr的长度,也就是说该二维数组只有两条新闻,你想要的数字也是2,但是如果使用count($arr)不同版本的php,统计的结果是不一样的;
后来在php手册中发现,count函数还有第二个参数,解释如下:
count函数有两个参数:
0(或COUNT_NORMAL)为默认,不检测多维数组(数组中的数组);
1(或COUNT_RECURSIVE)为检测多维数组,
所以如果要判断读取的数组$arr是不是有新闻信息,就要这样写了:
<?php
if(is_array($arr) && count($arr,COUNT_NORMAL)>0 )
{
.....
} else {
.....
}
?>
你可以使用这样的代码来测试该函数:
<?php
$arr=array(
0=>array('title' => '新闻1', 'viewnum' => 123, 'content' => 'ZAQXSWedcrfv'),
1=>array('title' => '新闻2', 'viewnum' => 99, 'content' => 'QWERTYUIOPZXCVBNM')
);
echo '不统计多维数组:'.count($arr,0);//count($arr,COUNT_NORMAL)
echo "<br/>";
echo '统计多维数组:'.count($arr,1);//count($arr,COUNT_RECURSIVE)
?>
好了,到此为止,已经解决php中获取二维或多维数组的第一维长度的问题!
方法一
<?php教程
echo strtotime('last Monday');
echo '<br />';
echo strtotime('next Sunday');
?>
方法二
<?
02 function getMonSun(){
03 $curTime=time();
04
05 $curWeekday = date('w');
06
07 //为0是 就是 星期七
08 $curWeekday = $curWeekday?$curWeekday:7;
09
10
11 $curMon = $curTime - ($curWeekday-1)*86400;
12 $curSun = $curTime + (7 - $curWeekday)*86400;
13
14 $cur['Mon'] = $curMon;
15 $cur['Sun'] = $curSun;
16
17 return $cur;
18 }
19 $cur = getMonSun();
20
21 echo date('Y-m-d',$cur['Mon']);
22 echo "rn";
23 echo date('Y-m-d',$cur['Sun']);
24 ?>
方法三
<?
02 function getMonSun(){
03 $curTime=time();
04
05 //求出当前是星期几:
06 $curWeekday = date('w');
07
08 //如果是周一则减上7*86400周二减上6*86400,依此类推得到周一的时间戳:
09 switch ($curWeekday) {
10 case 0:
11 $curMon = $curTime-7*86400;
12 $curSun = $curTime;
13 break;
14 case 1:
15 $curMon = $curTime;
16 $curSun = $curTime+6*86400;
17 break;
18 case 2:
19 $curMon = $curTime-1*86400;
20 $curSun = $curTime+5*86400;
21 break;
22 case 3:
23 $curMon = $curTime-2*86400;
24 $curSun = $curTime+4*86400;
25 break;
26 case 4:
27 $curMon = $curTime-3*86400;
28 $curSun = $curTime+3*86400;
29 break;
30 case 5:
31 $curMon = $curTime-4*86400;
32 $curSun = $curTime+2*86400;
33 break;
34 case 6:
35 $curMon = $curTime-5*86400;
36 $curSun = $curTime+1*86400;
37 break;
38 }
39 $cur['Mon'] = $curMon;
40 $cur['Sun'] = $curSun;
41
42 return $cur;
43 }
44 $cur = getMonSun();
45 echo date('Y-m-d',$cur['Mon']);
46 echo "rn";
47 echo date('Y-m-d',$cur['Sun']);
48 ?>
try{
$tor = new TorHttp('127.0.0.1', 9050);
$data = array('username' => 'liujun');
$headers = array('Cookie' => 'php教程id=123456; xx=v');
echo $tor->get('http://host.com/testsocks.php', $headers);
$tor->newId('123456');
}catch(Exception $e){
if($e->getCode() == 9999){
$tor->newId('123456');
}
echo $e->getMessage() . "n";
}
*/
class Tool_TorHttp
{
/**
* Tor提供的socks服务器
*
* @var <string
*/
private $_host;
/**
* socks服务的连接
*
* @var <stream>
*/
private $_sock;
/**
* 构造函数
*
* @param <string> $host socks服务器地址
* @param <int> $port
*/
public function __construct($host = '127.0.0.1', $port = 9050)
{
$this->_host = $host;
@ $this->_sock = fsockopen($host, $port, $errorCode, $error, 5);
if($errorCode){
throw new Exception('不能连接代理服务器');
}
//建立应用层的连接
fwrite($this->_sock, pack('C3', 5, 1, 0));
$resp = fread($this->_sock, 1024);
$resp = unpack('Cversion/Cmethod', $resp);
if($resp['version'] != 5 || $resp['method'] != 0){
$this->_sock = null;
throw new Exception('代理服务器不可用或者需要连接密码');
}
}
/**
* 连接目标主机
*
* @param <type> $host
* @param <type> $port
*/
private function _connect($host, $port)
{
//ip和域名描述的服务器用不同的报文格式
$lip = ip2long($host);
if(empty($lip)){
$pack = pack('C5', 5, 1, 0, 3, strlen($host)) . $host . pack('n', intval($port));
}else{
$pack = pack('C4Nn', 5, 1, 0, 1, $lip, $port);
}
fwrite($this->_sock, $pack);
$resp = '';
$counter = 0;
while(true){
if(feof($this->_sock)){
break;
}
$resp .= fread($this->_sock, 1024);
if(!empty($resp) || $counter == 50){
break;
}
$counter += 1;
}
$resp = unpack('Cversion/Crep', $resp);
if(($resp['version'] != 5) || ($resp['rep'] != 0)){
throw new Exception("请求的服务[$host:$port]暂时不可用,或者Tor不能到达,如果反复发生,请尝试重启Tor", 9999);
}
}
/**
* 发起一次http的get请求
*
* @param <type> $url
* @return <string>
*/
public function get($url, $headers = array())
{
$ua = parse_url($url);
if(empty($ua['port'])){
$ua['port'] = 80;
}
$this->_connect($ua['host'], $ua['port']);
$requestUri = $ua['path'];
if(!empty($ua['query'])){
$requestUri .= '?' . $ua['query'];
}
$headers['Host'] = $ua['host'];
if(!isset($headers['User-Agent'])){
$headers['User-Agent'] = "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.0.10) Gecko/2009042316 Firefox/3.0.10 GTB5";
}
$headers['Connection'] = 'close';
fwrite($this->_sock, "GET {$requestUri} HTTP/1.1n");
foreach ($headers as $key => $val){
fwrite($this->_sock, "{$key}: {$val}n");
}
fwrite($this->_sock, "n");
$resp = '';
while(!feof($this->_sock)){
$resp .= fread($this->_sock, 1024);
}
return $resp;
}
/**
* 发起一次http的post请求
*
* @param <type> $url
* @param <type> $data
* @param <type> $headers
* @return <type>
*/
public function post($url, $data, $headers = array())
{
$ua = parse_url($url);
if(empty($ua['port'])){
$ua['port'] = 80;
}
$this->_connect($ua['host'], $ua['port']);
if(isset($ua['path']) && !empty($ua['path'])){
$requestUri = $ua['path'];
}else{
$requestUri = '/';
}
if(!empty($ua['query'])){
$requestUri .= '?' . $ua['query'];
}
if(!isset($headers['User-Agent'])){
$headers['User-Agent'] = "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.0.10) Gecko/2009042316 Firefox/3.0.10 GTB5";
}
$headers['Connection'] = 'close';
$data = $this->_parseData($data);
fwrite($this->_sock, "POST {$requestUri} HTTP/1.1n");
fwrite($this->_sock, "Host: {$ua['host']}n");
fwrite($this->_sock, "Content-Type: application/x-www-form-urlencodedn");
fwrite($this->_sock, "Content-Length: " . strlen($data) . "n");
foreach ($headers as $key => $val){
fwrite($this->_sock, "{$key}: {$val}n");
}
fwrite($this->_sock, "n");
fwrite($this->_sock, $data . "n");
$resp = '';
while(!feof($this->_sock)){
$resp .= fread($this->_sock, 1024);
}
return $resp;
}
/**
* 更新Tor的身份
*
* @param <type> $password
* @param <type> $port
* @return <type>
*/
public function newId($password = '', $port = 9051)
{
if(!empty($password) && $password[0] != '"'){
$password = '"' . $password . '"';
}
//创建到tor控终端的连接
@ $sock = fsockopen($this->_host, $port, $errorCode, $error, 5);
if($errorCode){
throw new Exception('不能连接代理服务器控制端,请检查端口号');
}
fwrite($sock, "AUTHENTICATE {$password}n");
$resp = fread($sock, 1024);
if(!preg_match('/^250/', $resp)){
throw new Exception('Tor控制认证失败,请确认密码正确');
}
fwrite($sock, "SIGNAL NEWNYMn");
$resp = fread($sock, 1024);
if(!preg_match('/^250/', $resp)){
throw new Exception('更新身份失败,请重试');
}
return true;
}
private function _parseData($data)
{
if(empty($data) || !is_array($data)){
return $data;
}
$encoded = '';
while (list($k, $v) = each($data)) {
$encoded .= $k . "=" . $v . '&';
}
return substr($encoded, 0, -1);
}
public function __destruct()
{
fclose($this->_sock);
$this->_sock = null;
}
}
相关文章
- eval函数在php中是一个函数并不是系统组件函数,我们在php.ini中的disable_functions是无法禁止它的,因这他不是一个php_function哦。 eval()针对php安全来说具有很...2016-11-25
- 在php中eval是一个函数并且不能直接禁用了,但eval函数又相当的危险了经常会出现一些问题了,今天我们就一起来看看eval函数对数组的操作 例子, <?php $data="array...2016-11-25
Python astype(np.float)函数使用方法解析
这篇文章主要介绍了Python astype(np.float)函数使用方法解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下...2020-06-08- 这篇文章主要介绍了Python中的imread()函数用法说明,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-03-16
- 本文主要介绍了C# 中取绝对值的函数。具有很好的参考价值。下面跟着小编一起来看下吧...2020-06-25
- 下面小编就为大家带来一篇C#学习笔记- 随机函数Random()的用法详解。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2020-06-25
- 这篇文章主要介绍了源码分析系列之json_encode()如何转化一个对象,对json_encode()感兴趣的同学,可以参考下...2021-04-22
- PHP去除html、css样式、js格式的方法很多,但发现,它们基本都有一个弊端:空格往往清除不了 经过不断的研究,最终找到了一个理想的去除html包括空格css样式、js 的PHP函数。...2013-08-02
- 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函数详细解析,sort函数是algorithm库下的一个函数,sort函数是不稳定的,即大小相同的元素在排序后相对顺序可能发生改变...2022-08-18
- strstr() 函数搜索一个字符串在另一个字符串中的第一次出现。该函数返回字符串的其余部分(从匹配点)。如果未找到所搜索的字符串,则返回 false。语法:strstr(string,search)参数string,必需。规定被搜索的字符串。 参数sea...2013-10-04
- index.php怎么打开?初学者可能不知道如何打开index.php,不会的同学可以参考一下本篇教程 打开编辑:右键->打开方式->经文本方式打开打开运行:首先你要有个支持运行PH...2017-07-06
PHP函数分享之curl方式取得数据、模拟登陆、POST数据
废话不多说直接上代码复制代码 代码如下:/********************** curl 系列 ***********************///直接通过curl方式取得数据(包含POST、HEADER等)/* * $url: 如果非数组,则为http;如是数组,则为https * $header:...2014-06-07- Foreach 函数(PHP4/PHP5)foreach 语法结构提供了遍历数组的简单方式。foreach 仅能够应用于数组和对象,如果尝试应用于其他数据类型的变量,或者未初始化的变量将发出错误信息。...2013-09-28
- free函数是释放之前某一次malloc函数申请的空间,而且只是释放空间,并不改变指针的值。下面我们就来详细探讨下...2020-04-25
- PHP 函数 strip_tags 提供了从字符串中去除 HTML 和 PHP 标记的功能,该函数尝试返回给定的字符串 str 去除空字符、HTML 和 PHP 标记后的结果。由于 strip_tags() 无法实际验证 HTML,不完整或者破损标签将导致更多的数...2014-05-31
SQL Server中row_number函数的常见用法示例详解
这篇文章主要给大家介绍了关于SQL Server中row_number函数的常见用法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-12-08PHP中func_get_args(),func_get_arg(),func_num_args()的区别
复制代码 代码如下:<?php function jb51(){ print_r(func_get_args()); echo "<br>"; echo func_get_arg(1); echo "<br>"; echo func_num_args(); } jb51("www","j...2013-10-04- 分享一个PHP加密解密的函数,此函数实现了对部分变量值的加密的功能。 加密代码如下: /* *功能:对字符串进行加密处理 *参数一:需要加密的内容 *参数二:密钥 */ function passport_encrypt($str,$key){ //加密函数 srand(...2015-10-30