在
PHP
网页的开头加入以下
<?
$time_start = getmicrotime();
function getmicrotime()
{
list($usec, $sec) = explode(" ",microtime());
return ((float)$usec + (float)$sec);
}
?>
然后到最后加入以下代码
<?
$time_end = getmicrotime();
printf ("[
页面执行时间
: %.2f
毫秒
]/n/n",($time_end - $time_start)*1000);
?>
$file = fopen($filename, 'r') or die("
抱歉,无法打开
: $filename");
or
在这里是这样理解的,因为在
PHP
中并不区分数据类型,所以
$file
既可以是
int
也可以
bool
,所以这样的语句不会报错。但其处理过程可能有些朋友不大明白。
其实在大多数的语言中, bool or bool 这样的语句中,如果前一个值为真后一个值就不会再判断了。这里也是的,所以如果 fopen 函数执行正确的话,会返回一个大于 0 的 int 值(这其实就是 “ 真 ” ),后面的语句就不会执行了。如果 fopen 函数执行失败,就会返回 false ,那么就会判断后面的表达式是否为真了。
结果执行了 die() 之后,不管返回什么,程序都已经停止执行了,并且显示指定的出错信息,也就达到了调试的目的。
其实在大多数的语言中, bool or bool 这样的语句中,如果前一个值为真后一个值就不会再判断了。这里也是的,所以如果 fopen 函数执行正确的话,会返回一个大于 0 的 int 值(这其实就是 “ 真 ” ),后面的语句就不会执行了。如果 fopen 函数执行失败,就会返回 false ,那么就会判断后面的表达式是否为真了。
结果执行了 die() 之后,不管返回什么,程序都已经停止执行了,并且显示指定的出错信息,也就达到了调试的目的。
php 复选框的问题
<form name="form1" method="post" action="d.php">
<input type="checkbox" name="checkbox[]" value="111">
111
<input type="checkbox" name="checkbox[]" value="222">
222
<input type="checkbox" name="checkbox[]" value="3333">
333
<input type="checkbox" name="checkbox[]" value="4444">
4444
<input type="checkbox" name="checkbox[]" value="5555">
555
<input type="checkbox" name="checkbox[]" value="6666">
6666
<input type="submit" name="Submit" value=" 提交 ">
</form>
d.php
<?
$fd=$_POST['checkbox'];
for ($i = 0; $i < (sizeof($fd)); $i++) {
$newhpt=$newhpt.$fd[$i].",";
}
echo $newhpt."<BR>";
?>
PHP
代码中函数前面的
@
是什么意思?
@
的作用是忽略调用该函数时产生的错误信息。
php
中日期相加的问题
$Form_Vote_ExpireDay=31;
$newtime=date("Y-m-d",mktime(0, 0, 0, date("m") , date("d")+$Form_Vote_ExpireDay, date("Y")));
关于
php
水印的问题
<?php
// 读取目标文件
$im = imageCreatefromjpeg("1[2].jpg");
// 设定混合模式
imagealphablending($im, true);
// 读取水印
$im2 = imagecreatefrompng("watermark.png");
// 下面是我写的字,可以不看
//$color = imagecolorallocate($im2, 0, 0, 0);
//imagestring($im2, 12, 1, 1, "i love u jingjing", $color);
// 字结束
// 拷贝水印到目标文件
imagecopy($im, $im2, 5, 5, 0, 0, 220, 34);
// 输出,可以输出到文件里
imagejpeg($im);
// 读取目标文件
$im = imageCreatefromjpeg("1[2].jpg");
// 设定混合模式
imagealphablending($im, true);
// 读取水印
$im2 = imagecreatefrompng("watermark.png");
// 下面是我写的字,可以不看
//$color = imagecolorallocate($im2, 0, 0, 0);
//imagestring($im2, 12, 1, 1, "i love u jingjing", $color);
// 字结束
// 拷贝水印到目标文件
imagecopy($im, $im2, 5, 5, 0, 0, 220, 34);
// 输出,可以输出到文件里
imagejpeg($im);
imagedestroy($im);
imagedestroy($im1);
?>
<?php
$im = imageCreatefromjpeg("1[2].jpg");
//$im = imagecreatefromgif("eee.gif");
//$im = imagecreatefrompng("1[2].png");
$black = imagecolorallocate ($im, 0, 140, 76);
$red = imagecolorallocate ($im, 255, 0, 0);
$string=chr(0xE5).chr(0xAE).chr(0x89).chr(0xE5).chr(0xBA).chr(0x86).chr(0xE6).chr(0x88).chr(0xBF).chr(0xE4).chr(0xBA).chr(0xA7).chr(0xE7).chr(0xBD).chr(0x91);
//imagestring ($im,12,10,10,$string,$red);
imagettftext($im,20,0,4,30,$red,"FZDHTJW.TTF",$string);
imagepng ($im);
imagedestroy ($im);
?>
<?
imagedestroy($im1);
?>
<?php
$im = imageCreatefromjpeg("1[2].jpg");
//$im = imagecreatefromgif("eee.gif");
//$im = imagecreatefrompng("1[2].png");
$black = imagecolorallocate ($im, 0, 140, 76);
$red = imagecolorallocate ($im, 255, 0, 0);
$string=chr(0xE5).chr(0xAE).chr(0x89).chr(0xE5).chr(0xBA).chr(0x86).chr(0xE6).chr(0x88).chr(0xBF).chr(0xE4).chr(0xBA).chr(0xA7).chr(0xE7).chr(0xBD).chr(0x91);
//imagestring ($im,12,10,10,$string,$red);
imagettftext($im,20,0,4,30,$red,"FZDHTJW.TTF",$string);
imagepng ($im);
imagedestroy ($im);
?>
<?
//
这个函数是把汉字转换成
UTF-8
后,就可以在
GD
中使用了!
//Program writen by sadly www.phpx.com
function gb2utf8($gb)
{
if(!trim($gb))
return $gb;
$filename="gb2312.txt/";
$tmp=file($filename);
$codetable=array();
while(list($key,$value)=each($tmp))
$codetable[hexdec(substr($value,0,6))]=substr($value,7,6);
function gb2utf8($gb)
{
if(!trim($gb))
return $gb;
$filename="gb2312.txt/";
$tmp=file($filename);
$codetable=array();
while(list($key,$value)=each($tmp))
$codetable[hexdec(substr($value,0,6))]=substr($value,7,6);
$utf8=/"/";
while($gb)
{
if (ord(substr($gb,0,1))>127)
{
$this=substr($gb,0,2);
$gb=substr($gb,2,strlen($gb));
$utf8.=u2utf8(hexdec($codetable[hexdec(bin2hex($this))-0x8080]));
}
else
{
$gb=substr($gb,1,strlen($gb));
$utf8.=u2utf8(substr($gb,0,1));
}
}
while($gb)
{
if (ord(substr($gb,0,1))>127)
{
$this=substr($gb,0,2);
$gb=substr($gb,2,strlen($gb));
$utf8.=u2utf8(hexdec($codetable[hexdec(bin2hex($this))-0x8080]));
}
else
{
$gb=substr($gb,1,strlen($gb));
$utf8.=u2utf8(substr($gb,0,1));
}
}
$ret=/"/";
for($i=0;$i<strlen($utf8);$i+=3)
$ret.=chr(substr($utf8,$i,3));
for($i=0;$i<strlen($utf8);$i+=3)
$ret.=chr(substr($utf8,$i,3));
return $ret;
}
}
function u2utf8($c)
{
for($i=0;$i<count($c);$i++)
$str=/"/";
if ($c < 0x80) {
$str.=$c;
}
else if ($c < 0x800) {
$str.=(0xC0 | $c>>6);
$str.=(0x80 | $c & 0x3F);
}
else if ($c < 0x10000) {
$str.=(0xE0 | $c>>12);
$str.=(0x80 | $c>>6 & 0x3F);
$str.=(0x80 | $c & 0x3F);
}
else if ($c < 0x200000) {
$str.=(0xF0 | $c>>18);
$str.=(0x80 | $c>>12 & 0x3F);
$str.=(0x80 | $c>>6 & 0x3F);
$str.=(0x80 | $c & 0x3F);
}
return $str;
}
{
for($i=0;$i<count($c);$i++)
$str=/"/";
if ($c < 0x80) {
$str.=$c;
}
else if ($c < 0x800) {
$str.=(0xC0 | $c>>6);
$str.=(0x80 | $c & 0x3F);
}
else if ($c < 0x10000) {
$str.=(0xE0 | $c>>12);
$str.=(0x80 | $c>>6 & 0x3F);
$str.=(0x80 | $c & 0x3F);
}
else if ($c < 0x200000) {
$str.=(0xF0 | $c>>18);
$str.=(0x80 | $c>>12 & 0x3F);
$str.=(0x80 | $c>>6 & 0x3F);
$str.=(0x80 | $c & 0x3F);
}
return $str;
}
Header(/"Content-type: image/gif/");
$im = imagecreate(400,300);
$bkg = ImageColorAllocate($im, 0,0,0);
$clr = ImageColorAllocate($im, 255,255,255);
$fnt = /"wb.ttf/";
//include(/"gb2utf8.php/");
$str = gb2utf8(/" 中国 /");
ImageTTFText($im, 20, 0, 10, 20, $clr, $fnt, $str);
ImageGif($im);
ImageDestroy($im);
?>
$im = imagecreate(400,300);
$bkg = ImageColorAllocate($im, 0,0,0);
$clr = ImageColorAllocate($im, 255,255,255);
$fnt = /"wb.ttf/";
//include(/"gb2utf8.php/");
$str = gb2utf8(/" 中国 /");
ImageTTFText($im, 20, 0, 10, 20, $clr, $fnt, $str);
ImageGif($im);
ImageDestroy($im);
?>
<?php
//
对
Javascript
和
select
部件的结合运用
// 在写程序的过程,我想实现在 select 对象选中选项后,
// 不需要提交按钮,而直接触发动作,且在动作发生后
// 的页面中要保持所选择的选项。经过形容,终于通过
// 使用 Javascript 及 select 对象的 onchange 属性及
//value 值而实现了这种功能。
// 代码如下 ( 文件名为 "test.php") :
// 在写程序的过程,我想实现在 select 对象选中选项后,
// 不需要提交按钮,而直接触发动作,且在动作发生后
// 的页面中要保持所选择的选项。经过形容,终于通过
// 使用 Javascript 及 select 对象的 onchange 属性及
//value 值而实现了这种功能。
// 代码如下 ( 文件名为 "test.php") :
switch ($mon){
case '': echo ' 请选择您的星座: ';break;
case '1':echo ' 您的星座是水瓶座 ';break;
case '2':echo ' 您的星座是双鱼座 ';break;
case '3':echo ' 您的星座是白羊座 ';break;
case '4':echo ' 您的星座是金牛座 ';break;
case '5':echo ' 您的星座是双子座 ';break;
case '6':echo ' 您的星座是巨蟹座 ';break;
case '7':echo ' 您的星座是狮子座 ';break;
case '8':echo ' 您的星座是处女座 ';break;
case '9':echo ' 您的星座是天平座 ';break;
case '10':echo ' 您的星座是天蝎座 ';break;
case '11':echo ' 您的星座是射手座 ';break;
case '12':echo ' 您的星座是魔蝎座 ';break;
default:break;
}
?>
<form name="form1">
<select name="month_select"
onChange="javascript:location.href=
this.options[this.selectedIndex].value">
<option>-- 请选择你的星座 --</option>
<option value="test.php?mon=1"
<?php if($mon=="1") echo " selected"; ?>> 水瓶座 </option>
<option value="test.php?mon=2"
<?php if($mon=="2") echo " selected"; ?>> 双鱼座 </option>
<option value="test.php?mon=3"
<?php if($mon=="3") echo " selected"; ?>> 白羊座 </option>
<option value="test.php?mon=4"
<?php if($mon=="4") echo " selected"; ?>> 金牛座 </option>
<option value="test.php?mon=5"
<?php if($mon=="5") echo " selected"; ?>> 双子座 </option>
<option value="test.php?mon=6"
<?php if($mon=="6") echo " selected"; ?>> 巨蟹座 </option>
<option value="test.php?mon=7"
<?php if($mon=="7") echo " selected"; ?>> 狮子座 </option>
<option value="test.php?mon=8"
<?php if($mon=="8") echo " selected"; ?>> 处女座 </option>
<option value="test.php?mon=9"
<?php if($mon=="9") echo " selected"; ?>> 天平座 </option>
<option value="test.php?mon=10"
<?php if($mon=="10") echo " selected"; ?>> 天蝎座 </option>
<option value="test.php?mon=11"
<?php if($mon=="11") echo " selected"; ?>> 射手座 </option>
<option value="test.php?mon=12"
<?php if($mon=="12") echo " selected"; ?>> 魔蝎座 </option>
</select>
</form>
<select name="month_select"
onChange="javascript:location.href=
this.options[this.selectedIndex].value">
<option>-- 请选择你的星座 --</option>
<option value="test.php?mon=1"
<?php if($mon=="1") echo " selected"; ?>> 水瓶座 </option>
<option value="test.php?mon=2"
<?php if($mon=="2") echo " selected"; ?>> 双鱼座 </option>
<option value="test.php?mon=3"
<?php if($mon=="3") echo " selected"; ?>> 白羊座 </option>
<option value="test.php?mon=4"
<?php if($mon=="4") echo " selected"; ?>> 金牛座 </option>
<option value="test.php?mon=5"
<?php if($mon=="5") echo " selected"; ?>> 双子座 </option>
<option value="test.php?mon=6"
<?php if($mon=="6") echo " selected"; ?>> 巨蟹座 </option>
<option value="test.php?mon=7"
<?php if($mon=="7") echo " selected"; ?>> 狮子座 </option>
<option value="test.php?mon=8"
<?php if($mon=="8") echo " selected"; ?>> 处女座 </option>
<option value="test.php?mon=9"
<?php if($mon=="9") echo " selected"; ?>> 天平座 </option>
<option value="test.php?mon=10"
<?php if($mon=="10") echo " selected"; ?>> 天蝎座 </option>
<option value="test.php?mon=11"
<?php if($mon=="11") echo " selected"; ?>> 射手座 </option>
<option value="test.php?mon=12"
<?php if($mon=="12") echo " selected"; ?>> 魔蝎座 </option>
</select>
</form>
<?
$UploadImgUrl="mag/";
$ShowDateFileName=strftime("%Y%m%d%H%M%S");
if($_GET["up"]=="up"){
$UploadImgName=$_FILES['pic']['name'];
$FileCount=$_FILES['pic']['size'];
$MAX_FILE_SIZE=$_POST["MAX_FILE_SIZE"];
if ($FileCount>$MAX_FILE_SIZE){
echo "<script>alert(/" 您上传的图片太大,超过指定的大小了 ! 请重新上传 !/")</script>";
}else{
$EndFileName=strrchr($UploadImgName,".");
$FilePath=$UploadImgUrl."Arc".$ShowDateFileName.$EndFileName;
if (strspn(strrchr($UploadImgName,"."),".jpgifJPGIF")>3){
move_uploaded_file($_FILES['pic']['tmp_name'],$FilePath);
?>
<script >
parent.input.bny.value+='[img]<?=$FilePath;?>[/img]<br>'
location.replace('upload.php')
</script>
<?
}else{
echo "<script>alert(/" 您刚才上传的图片类型不符合要求 !/");location.href=/"upload.php/"</script>";
}
}
}
?>
$UploadImgUrl="mag/";
$ShowDateFileName=strftime("%Y%m%d%H%M%S");
if($_GET["up"]=="up"){
$UploadImgName=$_FILES['pic']['name'];
$FileCount=$_FILES['pic']['size'];
$MAX_FILE_SIZE=$_POST["MAX_FILE_SIZE"];
if ($FileCount>$MAX_FILE_SIZE){
echo "<script>alert(/" 您上传的图片太大,超过指定的大小了 ! 请重新上传 !/")</script>";
}else{
$EndFileName=strrchr($UploadImgName,".");
$FilePath=$UploadImgUrl."Arc".$ShowDateFileName.$EndFileName;
if (strspn(strrchr($UploadImgName,"."),".jpgifJPGIF")>3){
move_uploaded_file($_FILES['pic']['tmp_name'],$FilePath);
?>
<script >
parent.input.bny.value+='[img]<?=$FilePath;?>[/img]<br>'
location.replace('upload.php')
</script>
<?
}else{
echo "<script>alert(/" 您刚才上传的图片类型不符合要求 !/");location.href=/"upload.php/"</script>";
}
}
}
?>
PHP 中解决只有半个汉字的问题
function char($str,$len){
$str=substr($str,0,$len);
$str_len=strlen($str);
$j=0;
for ($i=0;$i<=$str_len;$i++){
if (ord(substr($str,$i,1))<160){
$j++;
}
}
if ($j%2==0){
$str=substr($str,0,$len-1);
}
return $str;
}