Detailed explanation of PHP advanced tutorial

【Foreword】

      This article summarizes the advanced knowledge points of PHP. Regarding the basic knowledge, I made a summary in the detailed explanation of PHP basic tutorials .

 

【List】

(1) Multidimensional array

(2) Time and date

          (1) Get simple time; (2) Automatic copyright year; (3) Get simple time; (4) Get time zone;

          (5) Creation date; (6) Case: Calculate the number of days from the current National Day holiday

(3) Include file

           (1) Examples of include and require; (2) Differences between include and require; (3) Usage scenarios;

(4) File open, read, close

            (1) Open and read a file; (2) Open a file; (3) Read a file; (4) Read a single-line file

            (5) Check read line by line; (6) Read a single character; (7) Close the file

(5) File creation, writing, and overwriting

            (1) Create; (2) Write; (3) Overwrite

(6) File upload

            (1) Create file upload form; (2) Create upload script; (3) Upload limit; (4) Save uploaded file

(7) Cookies

             (1) Creation; (2) Introduction; (3) Retrieval

             (4) Get the cookie value; (5) Delete the cookie; (6) Solutions that do not support cookies

(8) Sessions

(9) E-mail

(10) Secure Email

(11) Error handling

(12) Exception

(13) Filter

 

【main body】

(1) Regarding multidimensional arrays, the previous article introduced

 

(2) Time and date

   JS:

    First mention the time and date in JS, and then a colleague recommended a library moment.js for processing time

var date = new Date();
console.log(date)
//Print the result, for example Sun Mar 11 2017 17:48:19 GMT+0800 (China Standard Time)

   PHP:

    date() function is used to format date or time

date(format,timestamp)
日期(格式,时间戳)

   具体详解我在后面文章PHP时间日期相关里做了总结

 

(三)Include文件

      服务器端包含 (SSI) 用于创建可在多个页面重复使用的函数、页眉、页脚或元素。

      include (或 require)语句会获取指定文件中存在的所有文本/代码/标记,并复制到使用 include 语句的文件中。如果需要在网站的多张页面上引用相同的 PHP、HTML 或文本的话,包含文件很有用。

(1)include 和 require 语句

通过 include 或 require 语句,可以将 PHP 文件的内容插入另一个 PHP 文件(在服务器执行它之前)。

include 和 require 语句是相同的,除了错误处理方面:

语法对比:

include 'filename';
require 'filename';

错误处理方面对比:

①require 会生成致命错误(E_COMPILE_ERROR)并停止脚本

②include 只生成警告(E_WARNING),并且脚本会继续

      因此,如果希望继续执行,并向用户输出结果,即使包含文件已丢失,就得使用 include。

      否则,在框架、CMS 或者复杂的 PHP 应用程序编程中,必须始终使用 require 向执行流引用关键文件。这有助于提高应用程序的安全性和完整性,在某个关键文件意外丢失的情况下。

      作用:包含文件省去了大量的工作。这意味着可以为所有页面创建标准页头、页脚或者菜单文件。然后,在页头需要更新时,您只需更新这个页头包含文件即可

(2)include实例

①直接引入

<?php include 'footer.php';?>

②嵌套引入

<div class="menu">
<?php include 'menu.php';?>
</div>

③案例:引入变量(通俗理解:引入即直接将整个代码引入,可以看做写在一个文件里)

假设one.php定义了变量:

<?php
$color='银色的';
$car='奔驰轿车';
?>

 two.php引入使用

<?php
include 'vars.php';
echo "我有一辆" . $color . $car "。";
?>

 (3)require举例

require语法与include相同,这里就不列举了

(4)include与require区别

<?php
include 'noFileExists.php';
echo "该文件不存在,请求资源错误";
?>

   ①include的持续性:用 include 语句引用某个文件并且 PHP 无法找到它,脚本会继续执行,echo 语句仍会继续执行

   ②require的终止性:如果改用 require 语句完成相同的案例,echo 语句不会继续执行,因为在 require 语句返回严重错误之后脚本就会终止执行

(5)使用场景

         ①require :当文件被应用程序请求时使用require,因为在框架、CMS或者复杂的PHP应用程序编程中,必须始终使用 require 向执行流引用关键文件。在某个关键文件意外丢失的情况下,这有助于提高应用程序的安全性和完整性。

         ②include:当文件不是必需,且应用程序在文件未找到时应该继续运行时使用include。因为如果希望继续执行,并向用户输出结果,即使包含文件已丢失,就得使用 include。

 

(四)文件打开,读取,关闭

    PHP拥有多种函数操作文件,包括创建、读取、上传以及编辑文件

    篇幅问题,我在后面文章PHP打开,读取,关闭文件里做了总结

 

(五)文件创建,写入,覆盖

   下面介绍下,如何在服务器创建并写入文件

(1)创建文件------------ fopen()

   fopen() 函数也用于创建文件。如果用 fopen() 打开并不存在的文件,此函数会创建文件,假定文件被打开为写入(w)或增加(a)。也许有点混乱,但是在 PHP 中,创建文件所用的函数与打开文件的相同。

  下面的例子创建名为 "test.txt" 的新文件,此文件将被创建于 PHP 代码所在的相同目录中,实例:

$myfile = fopen("test.txt", "w");

(2)写入文件-------------fwrite() 

   fwrite() 函数用于写入文件。第一个参数包含要写入的文件的文件名,第二个参数是被写的字符串

下面的例子把姓名写入名为 "newfile.txt" 的新文件中,实例:

<?php
$myfile = fopen("newfile.txt", "w") or die("Unable to open file!");
$txt = "Bill Gates\n";
fwrite($myfile, $txt);
$txt = "Steve Jobs\n";
fwrite($myfile, $txt);
fclose($myfile);
?>

   代码解析:

       ①以上脚本向文件 "newfile.txt" 写了两次。在发送的字符串 $txt 中,第一次包含 "Bill Gates",第二次包含 "Steve Jobs"

       ②在写入完成后,使用 fclose() 函数来关闭文件

   If you open the "newfile.txt" file, it should look like this:

Bill Gates
Steve Jobs

(3) Covering

   On the basis of the data written above, modify the written string, and when the page is refreshed again, the data in newfile.txt will be overwritten

For example, we put the above code

$txt = "Bill Gates\n";
$txt = "Steve Jobs\n";
change to
$txt = "Bill Gates---1\n";
$txt = "Steve Jobs---1\n";

, the previous data will be overwritten, and then open the newfile.txt file, you can see

Bill Gates1
Steve Jobs1

 

(6) File upload

 Divided into four steps (1) create a file upload form; (2) create an upload script; (3) upload restrictions; (4) save the upload file

 Specifically, I made a summary in the PHP upload file in the later article.

 

(7) Cookies

(1 Introduction

   Often used to identify users, cookies are small files that the server leaves on the user's computer. Whenever the same computer requests a page through the browser, it also sends the cookie. Through PHP, the ability to create and retrieve the value of a cookie

(2) Create

   The setcookie() function is used to set cookies, the function must be located before the <html> tag

setcookie(name, value, expire, path, domain);
   Example: Create a cookie named "user", assign it the value "Alex Porter", and specify that this cookie expires in one hour
<?php
setcookie("user", "Tony", time()+3600);
?>
<html>
<body>...</body>
</html>
   Note: When sending a cookie, the value of the cookie will be automatically URL encoded and automatically decoded when retrieved (to prevent URL encoding, setrawcookie() can be used instead)

(3) Search

   isset() function can check if a cookie has been set

<?php
if (isset($_COOKIE["user"])){
  echo "The cookie I set is user:" . $_COOKIE["user"] ;
}else{
  echo "No cookie data";
}
?>

(4) Get the cookie value

   The $_COOKIE variable is used to retrieve the value of the cookie

<?php
echo $_COOKIE["user"];// Get a corresponding cookie value
print_r($_COOKIE);// Get all cookies
?>
(5) Delete cookies

   Deleting a cookie simply changes the expiration date to a point in the past. example:

<?php
// set expiration date to one hour ago
setcookie("user", "", time()-3600);
?>
 (6) Solutions that do not support cookies

   If your application involves browsers that don't support cookies, you'll have to take other ways to pass information from one page to another within your application. Another way is to pass data from the form (the form and user input are covered in the previous article)

   Case: For example the form submits user input to "welcome.php" when the user clicks the submit button:

<form action="welcome.php" method="post">
姓名: <input type="text" name="name" />
年龄: <input type="text" name="age" />
<input type="submit"  value="提交"/>
</form>
   Get back the value in "welcome.php" like this:
Welcome <?php echo $_POST["name"]; ?>.<br />
You are <?php echo $_POST["age"]; ?> years old.
 

(8) Sessions

    The main content includes (1) introduction; (2) origin; (3) working mechanism; (4) opening a session;

                         (5) Store and obtain; (6) Store and obtain Session cases; (7) Delete/release Session

     Specifically, I will summarize in the following article.

     Note: The session_start() function must be located before the <html> tag, which means that there should be no output before it, otherwise an error will be reported

 

(9) E-mail

     Due to space limitations, I will summarize in the following article.

     

(10) Secure Email

      The best way to prevent e-mail injection is to validate the input

      Input can be validated using PHP filters:

      ①FILTER_SANITIZE_EMAIL Remove illegal characters of email from string

      ②FILTER_VALIDATE_EMAIL verification email address

 

(11) Error handling

 

 

 

 

 

 

【Summarize】

(1) Usage scenarios of include and require

   ①require :当文件被应用程序请求时使用require,因为在框架、CMS或者复杂的PHP应用程序编程中,必须始终使用 require 向执行流引用关键文件。在某个关键文件意外丢失的情况下,这有助于提高应用程序的安全性和完整性。

   ②include:当文件不是必需,且应用程序在文件未找到时应该继续运行时使用include。因为如果希望继续执行,并向用户输出结果,即使包含文件已丢失,就得使用 include。

(2)PHP操作文件失误常见错误

   ①编辑错误的文件;②被垃圾数据填满硬盘;③意外删除文件内容

 

 

 

先写到这里,稍后完善

.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326147101&siteId=291194637