欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

php語法技巧代碼實(shí)例

 更新時(shí)間:2021年01月25日 10:36:20   作者:傲雪星楓  
這篇文章主要介紹了php語法技巧代碼實(shí)例,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,有感興趣的同學(xué)可以看一下

1. DIRECTORY_SEPARATOR 與 PATH_SEPARATOR

DIRECTORY_SEPARATOR:路徑分隔符,linux上就是‘/'    windows上是‘\'
PATH_SEPARATOR:include多個(gè)路徑使用,在windows下,當(dāng)你要include多個(gè)路徑的話,你要用”;”隔開,但在linux下就使用”:”隔開的。

2.set_include_path 與 get_include_path

此方法可以設(shè)置文件的include路徑,設(shè)置后,include文件會(huì)先在include_path中查找,如沒再按設(shè)定的路徑查找。
例如:include目錄下有個(gè)router.php與config.php,可以這樣include

set_include_path('include');include('route.php');include('config.php');

另外,此方法可以指定多個(gè)include_path,用PATH_SEPARATOR分隔。
例如有 ./a ./b ./c 三目錄,每個(gè)目錄下分別有a.php,b.php,c.php,include 3個(gè)目錄的文件

$inc_path = array('a','b','c');
set_include_path(get_include_path().PATH_SEPARATOR.implode(PATH_SEPARATOR,$inc_path));
include('a.php');
include('b.php');
include('c.php');

查看include_path可以使用 get_include_path()

3.call_user_func 與 call_user_func_array

call_user_func 調(diào)用用戶自定義方法,第一個(gè)參數(shù)為要調(diào)用的方法名,第二個(gè)參數(shù)開始為調(diào)用方法要傳遞的參數(shù)。

function foo($a,$b){
 echo $a.' '.$b;
}
call_user_func('foo',100,200); // 輸出:100 200

call_user_func_array 與 call_user_func一樣,調(diào)用用戶自定義方法,第一個(gè)參數(shù)為要調(diào)用的方法名,第二個(gè)參數(shù)是一個(gè)數(shù)組,數(shù)組內(nèi)每一個(gè)元素是傳遞給調(diào)用方法的參數(shù)。這樣比call_user_func更清晰。

function foo($a,$b){
 echo $a.' '.$b;
}
call_user_func_array('foo', array(100,200)); // 輸出:100 200

調(diào)用類方法

class Foo{
 function show($a, $b){
 echo $a.' '.$b;
 }
}
call_user_func(array('Foo','show'), 100, 200); // 輸出 100 200
call_user_func_array(array('Foo','show'), array(300,400)); // 輸出 300 400

4.func_num_args 與 func_get_arg 與 func_get_args

func_num_args() 返回調(diào)用方法的傳入?yún)?shù)個(gè)數(shù),類型是整型
func_get_arg() 返回指定的參數(shù)值
func_get_args() 返回所有參數(shù)值,類型是數(shù)組

function foo(){
 $num = func_num_args();
 echo $num; // 2
 for($i=0; $i<$num; $i++){
 echo func_get_arg($i); // 1 2
 }
 print_r(func_get_args()); // Array
} 
foo(1,2);

5.使用php解釋js文件 在apache httpd.conf中加入:

AddType application/x-httpd-php .js

6.使用冒號(hào)表示語句塊

流程控制的書寫模式有兩種語法結(jié)構(gòu)。一種用大括號(hào)表示語句塊,一種用冒號(hào)表示語句塊。前者一般用于純代碼中,后者一般用于代碼和HTML結(jié)合時(shí)。

大括號(hào)表示語句塊

if ($value) {
 // 操作; 
} elseif($value) {
 // 操作; 
} else {
 // 操作;
}

冒號(hào)表示語句塊

使用冒號(hào)“:”來代替左邊的大括號(hào)“{”;使用endif; endwhile; endfor; endforeach; 和endswitch; 來代替右邊的大括號(hào)“}”。

if ($value) :
 // 操作
elseif ($value) :
 // 操作
else :
 // 操作
endif

7.php 求余出現(xiàn)負(fù)數(shù)處理方法

php int 的范圍是 -2147483648 ~ 2147483647,可用常量 PHP_INT_MAX 查看。

當(dāng)求余的數(shù)超過這個(gè)范圍,就會(huì)出現(xiàn)溢出。從而出現(xiàn)負(fù)數(shù)。

<?php
echo 3701256461%62; // -13
?>

即使使用floatval 方法把數(shù)值轉(zhuǎn)型為浮點(diǎn)數(shù),但php的求余運(yùn)算默認(rèn)使用整形來計(jì)算,因此一樣有可能出現(xiàn)負(fù)數(shù)。

解決方法是使用浮點(diǎn)數(shù)的求余方法 fmod。

<?php
$res = floatval(3701256461);
echo fmod($res,62); // 53
?>

8.使用file_get_contents post 數(shù)據(jù)

<?php 
$api = 'http://demo.fdipzone.com/server.php'; 
$postdata = array(
 'name' => 'fdipzone',
 'gender' => 'male'
);
$opts = array(
 'http' => array(
 'method' => 'POST',
 'header' => 'content-type:application/x-www-form-urlencoded',
 'content' => http_build_query($postdata)
 )
);
$context = stream_context_create($opts);
$result = file_get_contents($api, false, $context);
echo $result;
?>

9.設(shè)置時(shí)區(qū)

ini_set('date.timezone','Asia/Shanghai');

到此這篇關(guān)于php語法技巧代碼實(shí)例的文章就介紹到這了,更多相關(guān)php語法技巧內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論