PHP5->PHP7更新时可能需要改的代码

上传文件时得不到$upFile_name的值时候

HTML里
<input type="file" name="upFile" size="54" >
PHP里
//echo "<meta charset='utf-8'><pre>";
//print_r ($_FILES);
//echo "</pre>";exit;

$upFile_name = $_FILES['upFile']['name'];
$upFile_size = $_FILES['upFile']['size'];

Warning: A non-numeric value encountered

// 使用intval()转换即可
$year_tit=date('Y',(time()+intval($flag)*86400));

mysqli_insert_id() 不能用时

// 在mysqli扩展中,insert_id是mysqli对象的一个属性,而不是一个方法
// 假设$this->dbObj->linker 为数据库连接对象
//$schedule_no=mysqli_insert_id();php5
$schedule_no=$this->dbObj->linker->insert_id;//php7

不能用split时

//$list=split('/',$php_SELF_); php5
$list=preg_split('~/~',$php_SELF_); php7

不能用eregi时

// 换做preg_match,而且要加定界符~
if(preg_match('~android~',$user_agent)) // android
	$result = true;

获取所有数据表

$result_this = mysqli_query(数据库连接对象,"SHOW TABLES FROM $数据库名");

获取一个表的所有字段,并且干入array

$tot_column = array();
$con = mysqli_connect($this->hostName, $this->usrName, $this->passWd,$this->dbName) or die("Can not connect to a MySQL Server");
$sql = "SELECT * FROM $this->memoTable";
if ($res=mysqli_query($con,$sql)) {
	// 获取一个表的所有字段
	$code_field=mysqli_fetch_fields($res);
	foreach ($code_field as $val) {
		array_push($tot_column, $val->name);
	}
}
mysqli_close($con);

不能使用mysql_result时

//$row = @mysql_result($res, $row, $field);PHP5
$row = self::mysql_result_new($res, $row, $field);//php7

// 自定义方法
function mysql_result_new($result, $number, $field=0) {
	mysqli_data_seek($result, $number);
	$row = mysqli_fetch_array($result);
	return $row[$field] ?? '';
}

Warning: Illegal string offset ‘id’ 。。。错误

//php5
if ($board_member->detail_user_auth($member_info['id'], $rowMng['auth_seq'])){

// php7
$member_info_id = '';
// 如果$member_info这个数组根本没有的话
// $member_info = array();
if (is_array($member_info)) {
	$member_info_id = $member_info['id'];
}
if ($board_member->detail_user_auth($member_info_id, $rowMng['auth_seq'])){

方法调用call_user_method()不能用时

// 应该采用 call_user_func() 或 call_user_func_array()
// 例子:
// 原来的
$info = call_user_method("方法名", 对象);
// 新的
$info = call_user_func(array(对象,"方法名"),参数...);

ereg_replace 改为preg_replace 后因为" "而出现问题时

//preg_replace里面不能放" ",暂时换做"/\xC2\xA0/is",有待验证!
方法1if (Trim($search) !== '') {
    $newstring  = preg_replace($search,$replace,$newstring);
}
方法2$str = str_replace(chr(194) . chr(160), "a", $str);  // 解决方法1
$str = preg_replace('/\xC2\xA0/is', "a", $str);  // 解决方法2

Delimiter must not be alphanumeric or backslash 问题及解决

正则表达式没有加左右侧的斜线

URL中的参数获取

//比如 URL 如下:
//http://asdasd.com/member/login.php?action=login

//在PHP5中,可以直接在php里用$action获取其值,但在PHP7里不行,此时如下即可
$action = $_REQUEST['action'];

使用count()时

//for ($i = 0; $i < count($tmp_auth); $i++) { php5
for ($i = 0; $i < count((array)$tmp_auth); $i++) { php7 得加(array)强制转换

如果直接使用$DOCUMENT_ROOT话不行

// 这样再使用即可
$DOCUMENT_ROOT = $_SERVER['DOCUMENT_ROOT'];

如果直接使用$SCRIPT_NAME话不行

$SCRIPT_NAME = $_SERVER['SCRIPT_NAME'];

方法参数不能使用&时

//$auth = new Auth(&$info['host_name'],&$info['user_id'],&$info['user_pw'],&$info['db_name']);php5

// php7
$host_name41 = &$info['host_name'];
$user_id41 = &$info['user_id'];
$user_pw41 = &$info['user_pw'];
$db_name41 = &$info['db_name'];
$auth = new Auth($host_name41,$user_id41,$user_pw41,$db_name41);

Invalid argument supplied for foreach() in 。。。

//php5
foreach($data as $value){
//。。。
}
// php7 我们只要在foreach之前判断一下数据源即可
if(is_array($data)){foreach($data as $value){...}}

常量是否定义

// php5里没定义就等于‘__asdasd__’,但php7里没这情况,得判断。
//if ( __asdasd__ == 'sdsdsdsdsd'){

// php7
if (!defined('__asdasd__')) {
	define('__asdasd__', '__asdasd__');
}

Declaration of xxxxxx should be compatible with yyyyyy

上图所示的意思是:在PHP 大于5.4的版本中,使用了 PHP的严格模式 检查程序,检测结果为 Index模块中的 clearcache()方法 必须兼容 父类Mytpl类中 定义的clearcache()方法,即 子类覆写父类的方法时 必须一致,包括 参数个数一致、参数如果没有值 则需要定义其默认值!
————————————————
版权声明:本文为CSDN博主「日月天方csdn」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/u010244099/article/details/78139528
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值