Reference File learning notes for getting started with PHP

Source: Internet
Author: User
Tags getting started with php
Referenced files are different from files referenced in asp in php. I will introduce using require and include to reference file instances in php. Referencing a file is one of the highlights of PHP.

Referenced files are different from files referenced in asp in php. I will introduce using require and include to reference file instances in php.

Referencing a file is one of the major characteristics of PHP. this method can put common functions and functions in one file. when other pages need to use these functions or functions, you can directly reference this file to call these functions. if you do not reference them, writing the same function on that page will greatly increase the workload of developers, it also increases the amount of code in the program, which is not conducive to later maintenance and secondary development.

There are two methods for PHP to reference files. the functions used are require () and include (). The effects of the two methods are the same, but the two functions have differences: if require does not return any value when referencing a file, an error is fatal and the program stops executing the file. when using this function for reference, make sure that the code is correct, when the include clause references a file, there is a reverse return value, and when an error occurs, it continues to execute the following code. Therefore, we recommend that you use the first function require to reference the file. it does not return the value, in terms of speed and efficiency, include is faster, while require is usually placed at the beginning of the PHP program. before executing the PHP program, it will first read the file specified by require, make it a part of the PHP web page, a common function, you can also introduce it into the web page.

The instance code is as follows:

  1. Require ('SQL. php'); // This function is usually placed at the beginning, for example, the file that references the SQL database connection function.
  2. Echo 'example of referencing Files ';
  3. Include ('Hello-world. php'); // This function is generally placed in the processing section of process control.
  4. ?>

Someone may ask, when a page references multiple files, and these referenced files also reference one or more identical files, sometimes there is no need to reference so many times, so how can we make PHP reference only once? Of course, PHP also has a corresponding method, that is, adding a "suffix" Declaration on the basis of the original function, that is, converting the function into require_once () and include_once () respectively, as shown in the following example:

  1. Require_once ('SQL. php'); // declare to reference only the SQL. php file once
  2. Echo 'example of referencing Files ';
  3. Include_once ('Hello-world. php'); // declare to reference only the hello-world.php file once
  4. ?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.