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

php語法技巧代碼實例

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

1. DIRECTORY_SEPARATOR 與 PATH_SEPARATOR

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

2.set_include_path 與 get_include_path

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

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

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

$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 調用用戶自定義方法,第一個參數(shù)為要調用的方法名,第二個參數(shù)開始為調用方法要傳遞的參數(shù)。

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

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

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

調用類方法

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() 返回調用方法的傳入?yún)?shù)個數(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.使用冒號表示語句塊

流程控制的書寫模式有兩種語法結構。一種用大括號表示語句塊,一種用冒號表示語句塊。前者一般用于純代碼中,后者一般用于代碼和HTML結合時。

大括號表示語句塊

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

冒號表示語句塊

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

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

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

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

當求余的數(shù)超過這個范圍,就會出現(xiàn)溢出。從而出現(xiàn)負數(shù)。

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

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

解決方法是使用浮點數(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.設置時區(qū)

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

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

相關文章

最新評論