//this is some server-side debug stuff. if ($_SERVER['argc'] > 1) { $name = 'noshbar'; for ($commandLineIndex = 1; $commandLineIndex < $_SERVER['argc']; $commandLineIndex++) { $commandLineArgument = explode('=', $_SERVER['argv'][$commandLineIndex]); $commandLineArgumentKey = array_shift($commandLineArgument); $commandLineArgumentValue = implode('=', $commandLineArgument); switch ($commandLineArgumentKey) { case '-text': $message = $commandLineArgumentValue; break; case '-fileTime': $fileTime = $commandLineArgumentValue; break; case '-lineCount': $lineCount = $commandLineArgumentValue; break; } } } //if we don't have a valid session, don't do anything. if ($name == '') { die; } if ($message != '') { writeMessage($message); die; } if ($fileTime != '' && $lineCount != '') { getMessage(intval($fileTime), intval($lineCount)); }
<?php header("Content-type:text/html;charset=utf-8"); $login = array('xiaowei' => '小伟', 'xiaoshi' => '小师', 'xiaohao' => '小郝', 'xiaolu' => '小路', 'xiaodan' => '小丹', 'xiaofang' => '小方', 'xiaoguo' => '小郭'); session_start(); $content = $_POST['content']; date_default_timezone_set('Asia/Shanghai'); $content = date('Y-m-d H:i:s', time()) . "---" . $login[$_SESSION['username']] . ":" . $content; echo writeMessage($content); function writeMessage($msg) { if ($msg != "") { $file_handle = fopen("data.txt", "a+") or die("Unable to open file"); $txt = $msg . "\r\n"; fwrite($file_handle, $txt); fclose($file_handle); return true; } else { return false; } }
<html> <head> <title>Writing PHP Function</title> </head> <body> <?php /* Defining a PHP Function */ function writeMessage() { echo "You are really a nice person, Have a nice time!"; } /* Calling a PHP Function */ writeMessage(); ?> </body> </html>
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>网络留言板模式</title></head> <body> <?php /* 留言内容不是写入数据库而是写入文件.要求的php版本为5.2 */ $filename="text_data.txt"; //声明一个变量保存文件名,在这个文件中保存留言信息 if(isset($_POST["sub"])){ //判断用户是否按下提交按扭,用户提交后则条件成功 //接收表单中三条内容,并整合为一条,使用“||”分隔,使用“<|>”结尾 $message=$_POST["username"]."||".$_POST["title"]."||".$_POST["mess"]."<|>"; writeMessage($filename, $message); //调用自定义函数,将信息写入文件 } if(file_exists($filename)) //判断文件是否存在,如果存在则条件成立 readMessage($filename); //文件存在则调用自定义函数,读取数据 function writeMessage($filename, $message) { //自定义一个向文件中写入数据的函数 $fp = fopen($filename, "a"); //以追加模式打开文件 if (flock($fp, LOCK_EX)) { //进行排它型锁定(独占锁定) fwrite($fp, $message); //将数据写入文件 flock($fp, LOCK_UN); //同样使用flock()函数释放锁定 } else { //如果建立独占锁定失败 echo "不能锁定文件!"; //输出错误消息 } fclose($fp); //关闭文件资源 }
echo "{$filetext}"; echo "file name: {$filename}"; ?> <!--======= FUNCTIONS ======= --> <br> <br> <?php function writeMessage($durjoy, $imtiaz, $hasan, $anik) { // Defining a funciton. echo "This is a simpele text by using function <br>"; echo " My name is " . $imtiaz . " " . $hasan . ". My nick name is " . $durjoy . " .My another name is " . $anik; } writeMessage("Dujroy", "Imtiaz", "Hasan", "Anik"); // calling the function. ?> <br> <?php function addFunction($num1, $num2) { $sum = $num1 + $num2; echo "Sum of the two numbers is : {$sum}"; } addFunction(10, 20); ?>