php学习简要手记 (一)
time()
strftime() 格式化输出time
浮点数最大10E16 即2的32次方 存储2个字节 这样带不带符号位就清楚了
常量:常量不要加$
非0值为TRUE 0为FALSE
<?php
// setting true
$flag = TRUE;
$flag = 1==1;
$num = 10;
$isEven = $num % 2 == 0;
$c = "u";
$isVowel = $c == "a"|| $c == "e"|| $c == "i"|| $c == "o"|| $c == "u";
// setting false
$flag = FALSE;
$flag = 1==2;
$aBiggerThanB = 2 > 3; // $aBiggerThanB is set to false
?>
要查看某个类型,不要用 gettype(),而用 is_type函数
<?php
$int = 12;
if (is_int($int)) {
$int += 4;
}
?>
强制将一个变量强制转换为某类型,使用settype() 函数
整数溢出
如果给定的一个数超出了 integer 的范围,将会被自动解释为 float。同样如果
执行的运算结果超出了 integer 范围,也会自动返回 float。
PHP 中没有整除的运算符。1/2 产生出 float 0.5
<?php
var_dump(25/7); // float(3.5714285714286)
var_dump((int) (25/7)); // int(3)
var_dump(round(25/7)); // float(4)
?>
永远不要相信浮点数结果精确到了最后一位,也永远不要比较两个浮点数是否相
等。如果确实需要更高的精度,应该使用BC math任意精度数学函数或者 gmp 函
数。
<?php
(19.6*100) !== (double)1960;
?>
<?php
printf("%.15f", (19.6*100)); //Outputs: 1960.000000000000227 (not 1960
as somewhat expected)
?>
sizeof($user); user数组的成员个数
php函数不区分大小写
系统函数
float pow(float x , float y) X的y次方
随机数
/*本例加入时间的因素,以执行时的百万分之一秒乘以一百万当随机数的种子
<?php
srand((double)microtime()*1000000);
$randval=rand();
echo $randval;
?>
字符串函数:
string strtolower(string str) 全转为小写
string strtoupper(string str) 全转为大写
string trim(string str) 去字符串首尾的空格
string substr(string string,int start,int [length]) 取子串
int strlen(string str)取字符串长度
int strpos(string 母串, string 待查字符, int[offset]) 寻找某个字符串
中某字符的最先出现处
int strcmp(string str1, string str2) 字符串比较
string md5(string str)计算字符串的MD5哈希值
int filesize(string filename); 获取文件的大小(注意filename带全路径)
int feof(int fp);测试文件指针是否指到文件尾
int fopen(string filename , string mode); 打开文件或url(mode为读或
写,操作系统总共打开的文件数是有限定的)
int fclose(int fp); 关闭以打开的文件
string fread(int fp, int n); 读取文件的第n个字节
string fgets(int fp,int n); 取得文件指针所指的行
取所指的行,返回字符串长度=行的长度-1 (因为最后有个
换行符)
int fwrite(int fp,string string,int [n]);写入文件
int fputs(int fp, string str,int [n]);写入文件
<?php
mkdir("/tmp/mydir",0700);
rmdir("/tmp/mydir");
?>
int rename(string oldname, string newname);
int fsockopen(string hostname,int port,int [errno], string [errstr],
int [timeout]); 打开网络的socket链接,也是返回文件指针
<?php
$fp=fsockopen("
www.163.com",80,&$errno,&$errstr,10);
if(!$fp){
echo "$errstr ($errno)<br>\n";
}
else {
fputs($fp,"GET / HTTP / 1.0\n Host:www.163.com\n\n");
while(!feof($fp){
echo fgets($fp,128);
}
fclose($fp);
}
?>
string gethostbyname(string hostname); dns查询,返回ip地址
array gethostbynamel(string hostname); 返回机器名称的所有ip,返回到数
组变量中
<?php echo gethostbyaddr("
www.whu.edu.cn"); ?>
<?php
$web=gethostbyname("
www.sohu.com");
echo "sohu IP Address:<ol type=1>";
for($i=0; $i<count($web);$i++){
echo "<li>".$web[$i];
}
echo "</ol>";
?>
int mysql_connect(string [hostname] [:port],string [username],string
[password]); 连mysql
int mysql_select_db(string database_name , int [link_identified]); 选择
一个数据库
int mysql_query(string query, int [link_identified]); 执行SQL串
int mysql_close(int [link_identified]);关闭数据库连接
用户函数
function myfunc($arg_1,$arg_2...,$arg_n)
{
//执行一些步骤
return $retval;
}
这里的myfunc是任意的,但第一个开头的一定要是英文字符,不能为下划线或数
字