php开发总结

Jun 23, 2016 pm 02:32 PM

1.mysql取得新生成自动编号的ID

$id=mysql_insert_id(); //取得刚插入的ID

 

2.php 如何判断网络是否连通

$url = "http://www.163.com/test.html";
$file=@fopen($url,"r");
if (!$file){ 
echo "";
                exit;
}
?>

3.判断任意日期是星期几


$date="2009-03-22";  
$datearr=explode("-",$date);  
$year=$datearr[0];  
$month=sprintf('%02d',$datearr[1]);  
$day=sprintf('%02d',$datearr[2]);  
$hour=$minute=$second=0;  
$dayofweek=getdate(mktime($hour,$minute,$second,$month,$day,$year));  
$weekday=$dayofweek['weekday'];  
$wday=$dayofweek['wday'];  
echo   $weekday."
";;                       //得到星期几的英文名称  
echo   $wday."
";
?>

4.mysql 查询区分大小写的解决方案

 

今天突然发现一条查询语句执行时,居然区分大小写

如:select * from 表 where abc='BITS'      如果 abc='bits' 就查不出结果

于是上网查询,可惜查不到结果,只说在windows下mysql不区别字段大小写, 倒是有很多告诉你如何去设置区分大小的方法.

郁闷~

于是,我换了一张表,发现这张表不区分大小写, 我想问题出在字串编码上,查了一下,正常的这个表的字段是gbk_chinese_ci,出错的字段是gbk_bin

答案找到了

5.PHP-Javascript“返回上一页”无缓存问题

很多用PHP写脚本的朋友都会遇到这样的问题,比如一个注册页面(不使用任何AJAX),需要填写账号密码等信息,填写完后需要到服务端验证,如果验证不通过,就要让用户重新填写资料,这对很多用户来说这是一大煎熬,有可能就因此放弃了注册。遇到这种问题,一般有这几种解决方法:

1.仍然调用刚才的页面,输出错误提示,并把中 value的值改为刚才用户输入的值。这应该是最好的方法,但缺点是要在这个页面的的VALUE进行处理,比较麻烦。

2.还有很多懒人像我一样,遇到验证不通过,直接输出一个出错提示页面,并在页面中加入JavaScript的代码:

返回

返回
这个代码是返回上一页的代码,返回后,除了密码类型的所有数据均会保留在网页中,这算是比较友好了。

但有很多朋友反应使用JS的返回后,网页上并没有数据的缓存,有时候却又有,真让人捉摸不定。前几天我在开发的时候也碰到了这个问题,上网Google许久未果,只好又从自己的代码分析入手。这时候,一句session_start(); 引起了我的注意。session_start(); 是开启 $_SESSION 会话的函数,开启SESSION后,似乎是每次访问一个网页都要重新调用一次网页。我把这句话去掉以后,问题就解决了。如果遇到网页数据不能缓存的,不妨去掉SESSION试试

6.计算当前日期所在月的第一天,最后一天的日期

 

php计算当前日期所在周的第一天,最后一天的日期.
function w_fl($i_date)
{
        $w_last=date("Y-m-d",strtotime("Sunday",strtotime($i_date)));
        return array(
                date("Y-m-d",strtotime("-6 days",strtotime($w_last))),
                $w_last
        );
}
 

php计算当前日期所在月的第一天,最后一天的日期.
function m_fl($i_date){
       $m_first=date("Y-m-01",strtotime($i_date));
       return array($m_first,date("Y-m-d",strtotime("+1 month -1day",strtotime($m_first))));
}

$ok=m_fl(date('Y-m-d'));
echo $ok[0]; //月初
echo $ok[1];//月末

 7. php无法删除cookie的解决方案

 

昨天在作logout.php的cookie删除时,发现怎么也无法清除cookie

注册cookie时用以下代码:

setcookie("iwho","耿良",0,"/","");

按照手册上的标准退出代码为:    setcookie("iwho", "", time() - 3600);// 将过期时间设为一小时前

但发现根本无法起作用,翻到以前的php4的书,打开查了用 setcookie("iwho") 直接删除,倒是起效果了,但是发现程序再登陆却无法注册cookie了,

看来php4根本无法与php5兼容.今天试了一下这个代码:

setcookie("iwho", "", time() - 3600,"/","");

起效,一切正常了,看来,应该按照注册时的格式(Cookie 必须用和设定时的同样的参数才能删除。),注明要删除的cookie路径

8.如何用php取得一个网页的html代码?

  
    $url="http://www.myukt.com/index.php";  
    $html=implode("\n",file($url));  
echo $html; 
?>

9.取整函数ceil,floor,round,intval

经常用到的PHP取整函数,主要是:ceil,floor,round,intval

ceil -- 进一法取整
说明
float ceil ( float value )
返回不小于 value 的下一个整数,value 如果有小数部分则进一位。ceil() 返回的类型仍然是 float,因为 float 值的范围通常比 integer 要大。

例子 1. ceil() 例子

echo ceil(4.3);     // 5
echo ceil(9.999);     // 10
?>


floor -- 舍去法取整
说明
float floor ( float value )
返回不大于 value 的下一个整数,将 value 的小数部分舍去取整。floor() 返回的类型仍然是 float,因为 float 值的范围通常比 integer 要大。

例子 1. floor() 例子

echo floor(4.3);     // 4
echo floor(9.999); // 9
?>


round -- 对浮点数进行四舍五入
说明
float round ( float val [, int precision] )
返回将 val 根据指定精度 precision(十进制小数点后数字的数目)进行四舍五入的结果。precision 也可以是负数或零(默认值)。

例子 1. round() 例子

echo round(3.4);             // 3
echo round(3.5);             // 4
echo round(3.6);             // 4
echo round(3.6, 0);         // 4
echo round(1.95583, 2);     // 1.96
echo round(1241757, -3); // 1242000
echo round(5.045, 2);     // 5.05
echo round(5.055, 2);     // 5.06
?>

intval---对变数转成整数型态

例子intval()

echo intval(4.3);    //4

echo intval(4.6); // 4  

?>

 10.php使用ImageCreateFromJPEG() 颜色丢失的非常厉害

 

使用ImageCreateFromJPEG() ,改变尺寸输出,颜色丢失的非常厉害!
而ImageCreateFromGIF() 和 ImageCreateFromPNG() 就没事。

 

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

How to automatically set permissions of unixsocket after system restart? How to automatically set permissions of unixsocket after system restart? Mar 31, 2025 pm 11:54 PM

How to automatically set the permissions of unixsocket after the system restarts. Every time the system restarts, we need to execute the following command to modify the permissions of unixsocket: sudo...

How to debug CLI mode in PHPStorm? How to debug CLI mode in PHPStorm? Apr 01, 2025 pm 02:57 PM

How to debug CLI mode in PHPStorm? When developing with PHPStorm, sometimes we need to debug PHP in command line interface (CLI) mode...

Explain the concept of late static binding in PHP. Explain the concept of late static binding in PHP. Mar 21, 2025 pm 01:33 PM

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

How to send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

Framework Security Features: Protecting against vulnerabilities. Framework Security Features: Protecting against vulnerabilities. Mar 28, 2025 pm 05:11 PM

Article discusses essential security features in frameworks to protect against vulnerabilities, including input validation, authentication, and regular updates.

See all articles