当前位置:首页 > 代码 > 正文

php项目代码(php项目代码大全)[20240424更新]

admin 发布:2024-04-24 20:06 106


今天给各位分享php项目代码的知识,其中也会对php项目代码大全进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

php目录操作实例代码

这篇文章主要介绍了php目录操作实例代码,需要的朋友可以参考下

代码如下:

?php

/**

*

listdir

*/

header("content-type:text/html;charset=utf-8");

$dirname

=

"./final/factapplication";

function

listdir($dirname)

{

$ds

=

opendir($dirname);

while

(false

!==

($file

=

readdir($ds)))

{

$path

=

$dirname.'/'.$file;

if

($file

!=

'.'

$file

!=

'..')

{

if

(is_dir($path))

{

listdir($path);

}

else

{

echo

$file."br";

}

}

}

closedir($ds);

}

listdir($dirname);

核心:递归的经典应用,以及文件和目录的基本操作。

代码如下:

?php

/**

*

copydir

*/

$srcdir

=

"../fileupload";

$dstdir

=

"b";

function

copydir($srcdir,

$dstdir)

{

mkdir($dstdir);

$ds

=

opendir($srcdir);

while

(false

!==

($file

=

readdir($ds)))

{

$path

=

$srcdir."/".$file;

$dstpath

=

$dstdir."/".$file;

if

($file

!=

"."

$file

!=

"..")

{

if

(is_dir($path))

{

copydir($path,

$dstpath);

}

else

{

copy($path,

$dstpath);

}

}

}

closedir($ds);

}

copydir($srcdir,

$dstdir);

核心:copy函数。

代码如下:

?php

/**

*

deldir

*/

$dirname

=

'a';

function

deldir($dirname)

{

$ds

=

opendir($dirname);

while

(false

!==

($file

=

readdir($ds)))

{

$path

=

$dirname.'/'.$file;

if($file

!=

'.'

$file

!=

'..')

{

if

(is_dir($path))

{

deldir($path);

}

else

{

unlink($path);

}

}

}

closedir($ds);

return

rmdir($dirname);

}

deldir($dirname);

核心:注意unlink删除的是带path的file。

代码如下:

?php

/**

*

dirsize

*/

$dirname

=

"a";

function

dirsize($dirname)

{

static

$tot;

$ds

=

opendir($dirname);

while

(false

!==

($file

=

readdir($ds)))

{

$path

=

$dirname.'/'.$file;

if

($file

!=

'.'

$file

!=

'..')

{

if(is_dir($path))

{

dirsize($path);

}

else

{

$tot

=

$tot

+

filesize($path);

}

}

}

return

$tot;

closedir($ds);

}

echo

dirsize($dirname);

核心:通过判断$tot在哪里返回,理解递归函数。

高质量PHP代码的50个技巧(3)

42

43

44

45

/**

Method to execute a command in the terminal

Uses :

1. system

2. passthru

3. exec

4. shell_exec

*/

function terminal($command)

{

//system

if(function_exists('system'))

{

ob_start();

system($command , $return_var);

$output = ob_get_contents();

ob_end_clean();

}

//passthru

else if(function_exists('passthru'))

{

ob_start();

passthru($command , $return_var);

$output = ob_get_contents();

ob_end_clean();

}

//exec

else if(function_exists('exec'))

{

exec($command , $output , $return_var);

$output = implode("\n" , $output);

}

//shell_exec

else if(function_exists('shell_exec'))

{

$output = shell_exec($command) ;

}

else

{

$output = 'Command execution not possible on this system';

$return_var = 1;

}

return array('output' = $output , 'status' = $return_var);

}

terminal('ls');

上面的函数将运行shell命令, 只要有一个系统函数可用, 这保持了代码的一致性.

5. 灵活编写函数

?

1

2

3

4

5

6

function add_to_cart($item_id , $qty)

{

$_SESSION['cart']['item_id'] = $qty;

}

add_to_cart( 'IPHONE3' , 2 );

使用上面的函数添加单个项目. 而当添加项列表的时候,你要创建另一个函数吗? 不用, 只要稍加留意不同类型的参数, 就会更灵活. 如:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

function add_to_cart($item_id , $qty)

{

if(!is_array($item_id))

{

$_SESSION['cart']['item_id'] = $qty;

}

else

{

foreach($item_id as $i_id = $qty)

{

$_SESSION['cart']['i_id'] = $qty;

}

}

}

add_to_cart( 'IPHONE3' , 2 );

add_to_cart( array('IPHONE3' = 2 , 'IPAD' = 5) );

现在, 同个函数可以处理不同类型的输入参数了. 可以参照上面的例子重构你的多处代码, 使其更智能.

6. 有意忽略php关闭标签

我很想知道为什么这么多关于php建议的博客文章都没提到这点.

?

1

2

3

?php

echo "Hello";

//Now dont close this tag

这将节约你很多时间. 我们举个例子:

一个 super_class.php 文件

?

1

2

3

4

5

6

7

8

9

?php

class super_class

{

function super_function()

{

//super code

}

}

?

//super extra character after the closing tag

index.php

?

1

2

require_once('super_class.php');

//echo an image or pdf , or set the cookies or session data

这样, 你将会得到一个 Headers already send error. 为什么? 因为 “super extra character” 已经被输出了. 现在你得开始调试啦. 这会花费大量时间寻找 super extra 的位置。因此, 养成省略关闭符的习惯:

?

1

2

3

4

5

6

7

8

9

?php

class super_class

{

function super_function()

{

//super code

}

}

//No closing tag

这会更好.

7. 在某地方收集所有输入, 一次输出给浏览器

这称为输出缓冲, 假如说你已在不同的函数输出内容:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

function print_header()

{

echo "p id='header'Site Log and Login links/p";

}

function print_footer()

{

echo "p id='footer'Site was made by me/p";

}

print_header();

for($i = 0 ; $i 100; $i++)

{

echo "I is : $i ';

}

print_footer();

替代方案, 在某地方集中收集输出. 你可以存储在函数的局部变量中, 也可以使用ob_start和ob_end_clean. 如下:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

function print_header()

{

$o = "p id='header'Site Log and Login links/p";

return $o;

}

function print_footer()

{

$o = "p id='footer'Site was made by me/p";

return $o;

}

echo print_header();

for($i = 0 ; $i 100; $i++)

{

echo "I is : $i ';

}

echo print_footer();

为什么需要输出缓冲:

可以在发送给浏览器前更改输出. 如 str_replaces 函数或可能是 preg_replaces 或添加些监控/调试的html内容.

输出给浏览器的同时又做php的处理很糟糕. 你应该看到过有些站点的侧边栏或中间出现错误信息. 知道为什么会发生吗? 因为处理和输出混合了.

8. 发送正确的mime类型头信息, 如果输出非html内容的话.

输出一些xml.

?

1

2

3

4

5

6

$xml = '?xml version="1.0" encoding="utf-8" standalone="yes"?';

$xml = "response

code0/code

/response";

//Send xml data

echo $xml;

工作得不错. 但需要一些改进.

?

1

2

3

4

5

6

7

$xml = '?xml version="1.0" encoding="utf-8" standalone="yes"?';

$xml = "response

code0/code

简单PHP代码

$_env 是环境变量,通过环境方式传递给当前脚本的变量的数组。

$_ENV['defaultapp'] = array('portal.php' = 'portal', 'forum.php' = 'forum', 'group.php' = 'group', 'home.php' = 'home');

是赋值 , 你可以用 var_dump($_env['defaultapp']) 看赋值结果。

--------------------------------------------------------------------

$_ENV['hostarr'] = explode('.', $_SERVER['HTTP_HOST']);

环境变量 用.分隔 主域名(你可以 echo $_server['HTTP_HOST']里面有什么)

----------------------------------------------------------------

$url = $domainroot.'forum.php?mod=groupfid='.$domain['id'].'page=1';

构造一个URL 直白点 结果就是: ;fid=1page=1

----------------------------------------

$url = empty($_ENV['domain']['app']['default']) ? (!empty($_ENV['domain']['defaultindex']) ? $_ENV['domain']['defaultindex'] : 'forum.php') : 'http://'.$_ENV['domain']['app']['default'];

结构简化 $url = $a ? (!$b? $c : $d) :$e; 2个3元运算嵌套, 至于看起来复杂的变量都是多维数组的值

php最经典,最基础的代码,适合入门的

PHP是一种可以嵌入到HTML中的运行在服务器端的脚本语言,所以为了体现PHP的特性我们可以分两种模式来实现PHP代码

1、 PHP嵌入到HTML中,例如index.php

html

head/head

body

    !--因为PHP嵌入到HTML中,所以需要完全区分PHP代码和HTML代码--

    ?php

        //输出hello world

        echo 'hello world;

    ?

/body

/html

2、 PHP独立文件,只有PHP代码,例如index.php

?php

    //输出

    echo 'hello world';

    

    //不需要闭合标签

php项目代码的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于php项目代码大全、php项目代码的信息别忘了在本站进行查找喔。

版权说明:如非注明,本站文章均为 AH站长 原创,转载请注明出处和附带本文链接;

本文地址:http://ahzz.com.cn/post/473.html


取消回复欢迎 发表评论:

分享到

温馨提示

下载成功了么?或者链接失效了?

联系我们反馈

立即下载