Rumah > pembangunan bahagian belakang > tutorial php > 介绍php相关语法技巧

介绍php相关语法技巧

jacklove
Lepaskan: 2023-03-31 11:04:01
asal
2211 orang telah melayarinya

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

1

2

3

set_include_path('include');

include('route.php');

include('config.php');

Salin selepas log masuk

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

1

2

3

4

5

$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');

Salin selepas log masuk

查看include_path可以使用 get_include_path()

3.call_user_func call_user_func_array

call_user_func 调用用户自定义方法,第一个参数为要调用的方法名,第二个参数开始为调用方法要传递的参数。

1

2

3

4

function foo($a,$b){

    echo $a.' '.$b;

}

call_user_func('foo',100,200); // 输出:100 200

Salin selepas log masuk

call_user_func_array 与 call_user_func一样,调用用户自定义方法,第一个参数为要调用的方法名,第二个参数是一个数组,数组内每一个元素是传递给调用方法的参数。这样比call_user_func更清晰。

1

2

3

4

function foo($a,$b){

    echo $a.' '.$b;

}

call_user_func_array('foo', array(100,200)); // 输出:100 200

Salin selepas log masuk

调用类方法

1

2

3

4

5

6

7

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

Salin selepas log masuk

4.func_num_argsfunc_get_argfunc_get_args

func_num_args() 返回调用方法的传入参数个数,类型是整型
func_get_arg() 返回指定的参数值
func_get_args() 返回所有参数值,类型是数组

1

2

3

4

5

6

7

8

9

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);

Salin selepas log masuk

5.使用php解释js文件

在apache httpd.conf中加入:

1

AddType application/x-httpd-php .js

Salin selepas log masuk

6.使用冒号表示语句块

流程控制的书写模式有两种语法结构。一种用大括号表示语句块,一种用冒号表示语句块。前者一般用于纯代码中,后者一般用于代码和HTML结合时。

大括号表示语句块

1

2

3

4

5

6

7

if ($value) {

 // 操作;

} elseif($value) {

 // 操作;

} else {

 // 操作;

}

Salin selepas log masuk

冒号表示语句块

使用冒号“:”来代替左边的大括号“{”;使用endif; endwhile; endfor; endforeach; 和endswitch; 来代替右边的大括号“}”。

1

2

3

4

5

6

7

if ($value) :

  // 操作

elseif ($value) :

  // 操作

else :

  // 操作

endif

Salin selepas log masuk

7.php 求余出现负数处理方法

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

当求余的数超过这个范围,就会出现溢出。从而出现负数。

1

2

3

<?php

echo 3701256461%62; // -13

?>

Salin selepas log masuk

即使使用floatval 方法把数值转型为浮点数,但php的求余运算默认使用整形来计算,因此一样有可能出现负数。

解决方法是使用浮点数的求余方法 fmod

1

2

3

4

<?php

$res = floatval(3701256461);

echo fmod($res,62); // 53

?>

Salin selepas log masuk

8.使用file_get_contents post 数据

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

<?php

$api = &#39;http://demo.fdipzone.com/server.php&#39;;

$postdata = array(

    &#39;name&#39; => &#39;fdipzone&#39;,

    &#39;gender&#39; => &#39;male&#39;

);

$opts = array(

    &#39;http&#39; => array(

        &#39;method&#39; => &#39;POST&#39;,

        &#39;header&#39; => &#39;content-type:application/x-www-form-urlencoded&#39;,

        &#39;content&#39; => http_build_query($postdata)

    )

);

$context = stream_context_create($opts);

$result = file_get_contents($api, false, $context);

echo $result;

?>

Salin selepas log masuk

9.设置时区

1

ini_set(&#39;date.timezone&#39;,&#39;Asia/Shanghai&#39;);

Salin selepas log masuk

本篇文章讲解了介绍php相关语法技巧,更多相关内容请关注php中文网。

相关推荐:

如何通过php 根据url自动生成缩略图

介绍php output_buffering 缓存使用的方法

如何通过php 实现BigPipe分块输出

Atas ialah kandungan terperinci 介绍php相关语法技巧. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Label berkaitan:
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan