PHP | die()和sleep()函数

死()

null

这个 死() 是PHP中的内置函数。它用于打印消息并退出当前php脚本。相当于 退出() 函数。

语法:

die($message)

参数: 这个函数只接受一个必须传递的参数。

  • $message: 此参数表示退出脚本时要打印的消息。

返回值: 它没有返回值,但在退出脚本时打印给定的消息。

例如:

Input : die("code ends here")
Output : code ends here

Here, die() function ends the 
script with a message "code
ends here "

适用版本: 此功能适用于所有PHP4及更高版本。

节目:

<?php
// blank url of site
// so that die() is executed
$site = "" ;
// open url else die (exit)
fopen ( $site , "r" )
or die ( "Unable to connect to given site." );
?>


输出:

Unable to connect to given site.

睡眠()

这个 睡眠() 是PHP中的内置函数。它用于将程序的执行延迟给定的秒数。

语法:

int sleep(int $seconds)

参数: 此函数只接受一个参数,必须传递该参数。

  • $s: 此参数表示延迟时间(以秒为单位)。

返回值: 成功时返回零,错误时返回FALSE。如果调用被信号中断,此函数将返回非零值。

错误/例外: 当秒数为负时,此函数生成E_警告。

适用版本: 该函数适用于PHP4、PHP5和PHP7。

节目:

<?php
// initial timings
echo date ( 'h:i:s' ) . "" ;
// halt for 5 seconds
sleep(5);
// timings after halt
echo date ( 'h:i:s' );
?>


输出:

01:07:16
01:07:21

注意, 暂停(睡眠)后的计时比暂停(睡眠)前的计时多5秒。

参考资料: http://php.net/manual/en/function.die.php http://php.net/manual/en/function.sleep.php

© 版权声明
THE END
喜欢就支持一下吧
点赞10 分享