PHP basic tutorial 6 functions and constants

Source: Internet
Author: User
This section describes the introduction of include, include_oncerequire, and require_once constants to the file and constants. in the previous article, we discussed the application of functions, but it is only used to call functions in a file. when we want to call functions in different files, the content in the previous section cannot be implemented, however, the PHP language provides the introduced concepts.

Content described in this section
  • Include and include_once

  • Require and require_once

  • Constant

  • Introduce file and constant integration case columns

  • Variable operation functions

  • Output statement

Preface

In the previous article, we discussed the application of functions, but only called functions in a file. when we want to call functions in different files, the content in the previous section cannot be implemented, but the PHP language provides the introduced concepts. This problem was solved perfectly.

File Reference

If a file, such as a. php file, needs to use the function of the B. php file, we need to introduce the function in file B to file. PHP bits can be implemented in four ways: include, include_once, require, and require_once.

There are four methods in total, but each method is different.

Include and include_once

From the name, we can see that the two imports both have the include feature. The two features are that when the introduced file fails (because you cannot guarantee that each imported file is correct) the program will not be terminated, but will be executed.

 

Result:

Include

Include, a form of introducing files. It has two features:

  1. When an error occurs in the file introduced by include, the program will not stop immediately and it will continue to run.

  2. If the file has already been introduced, it will be repeatedly introduced,

The first feature has been discussed above. The second feature, when we repeatedly use include in the codeSameFile, which will be repeatedly introduced.

  

Result:

As you can see above, when you use include, it will report an error, which means that you have already introduced the file and cannot introduce it again.

Include_once

Include_once is similar to include. in addition to introducing errors, it will continue to execute. In addition, when the file has been introduced, it will not be introduced again.

   

The above code does not return an error.

Require and require_once

Require and require_once are another file import method. Their common features are:

When an error occurs in the introduced file, the program will be terminated and will not be executed downstream.

    

Result:

Require

Require, a form of file introduction, has two features:

  1. When an error occurs in the file introduced by require, the program stops immediately.

  2. If the file has already been introduced, it will be repeatedly introduced,

The first feature has been discussed above. The second feature is introduced when require is repeatedly used in the code.SameFile, which will be repeatedly introduced.

     

Result:

As you can see above, when you use require, it will report an error, which means that you have already introduced the file and cannot introduce it again.

Require_once

Require_once is similar to require. in addition to introducing errors, the program will be terminated. In addition, when the file has been introduced, it will not be introduced again.

      

The above code does not return an error.

The system provides us with four methods, but how do we know which method to choose?

If we are introducing some files (such as getting database connections and opening files. We recommend that you use the _ once method to introduce files) to prevent resource waste. In development, we often use the require and require_once methods.

We recommend that you use require_once. This method can save resources and avoid repeated definition errors.

File import mechanism

File introduction: When another file is introduced into a file in PHP, the introduced file will be executed. we can return a value in the introduced file, you can also leave it blank until the end of the file.

       '; Require_once' B. php'; // introduce file B echo 'run this statement after the imported file is executed.
'; B. php file '; Return; ...... result ...... this is the introduced File. this is another file. execute this statement after the imported file is executed.

The code above shows that the return termination statement is used in the introduced file, and the termination of the main file is not terminated. Although they run in the same memory, they do not affect their respective operations.

File return

There are three points in summary about return:

  • When we introduce a file, 1 is returned successfully by default.

  • We can also return data based on the actual situation, such as an array.

  • During file import, when a return statement of the introduced file is encountered, the import process ends, the main file is returned, and the execution continues.

Constant

In our development process, we often need some global values, that is, we have determined and will not modify them in the future, such as the root directory path of the website. at this time, we can no longer use variables, because variables can be modified. Here we will introduce another method in PHP.Constant;

ConstantA constant can be considered as a special variable. once defined, it cannot be changed or canceled [I .e., it cannot be an unset constant].

So what can we use to define constants? PHP provides two methods to define constants.

  • Define (string $ name, constant value); defines a constant

    1. The first parameter is our constant name.

    2. The second parameter is the constant value. only scalar and null are allowed. The scalar type is integer, float, string, or boolean. It can also define the type of constant value as resource, but it is not recommended to do so, it may cause unknown conditions.

  • After PHP 5.3.0, the const keyword can be used to define constants outside the class definition.

We can use both of them when defining constants.

       '; Const TAX = 0.012; echo TAX; ...... result ...... 3.1415926 0.012

From the code above, we can see that the definitions of constants and variables are different. they are different.

Differences between constants and variables:

  1. There is no dollar sign before the constant ($ );

  2. Constants can only be defined using the define () function, but cannot be defined using the value assignment statement;

  3. Constants can be defined and accessed anywhere regardless of the scope of variables;

  4. Once defined, a constant cannot be redefined or canceled;

  5. The constant value can only be a scalar.

  6. Constants are generally named in uppercase.

In addition to processing our own defined constants, the PHP System also defines some constants for us. for example, we use PHP_INT_MAX to obtain the maximum value of an integer.

Magic constant

The PHP language can be said to have a special feature. Its syntax involves the concept of magic. The concept of magic exists in system constants. PHP calls it a magic constant. what is a magic constant?

Magic constant: PHP provides a large number of predefined constants to any script it runs. However, many constants are defined by different extension libraries. they only appear when these extension libraries are loaded, dynamically loaded, or included during compilation. (A lot of words that do not understand -_-);

To put it simplyThat is, the system provides a total of eight magic constants whose values change as they change their position in the code. For example, the value of _ LINE _ depends on the rows in the script. These special constants are case-insensitive.

PHP magic constant:

  1. The current row number in the _ LINE _ file.

  2. The complete path and FILE name of The _ FILE. If it is used in a file to be included, the file name to be included is returned. Since PHP 4.0.2, _ FILE _ always contains an absolute path (if it is a symbolic connection, it is an absolute path after resolution ), earlier versions sometimes contain a relative path.

  3. The directory where the _ DIR _ file is located. If it is used in included files, the Directory of the included files is returned. It is equivalent to dirname (_ FILE __). Unless it is the root directory, the name of the directory does not include the slash at the end. (Added in PHP 5.3.0) =

  4. _ FUNCTION name (new in PHP 4.3.0 ). Starting from PHP 5, this constant returns the name (case sensitive) when the function is defined ). In PHP 4, the value is always lowercase letters.

  5. _ CLASS name (new in PHP 4.3.0 ). Starting from PHP 5, this constant returns the name (case sensitive) when the class is defined ). In PHP 4, the value is always lowercase letters. The class name includes the declared zone (for example, Foo \ Bar ). Note that _ CLASS _ from PHP 5.4 also applies to trait. When used in the trait method, __class _ is the name of the CLASS that calls the trait method.

  6. _ TRAIT _ Trait name (new in PHP 5.4.0 ). Starting from PHP 5.4, this constant returns the name (case sensitive) of the trait definition ). The Trait name includes the declared region (for example, Foo \ Bar ).

  7. _ METHOD _ class METHOD name (new PHP 5.0.0 ). Returns the name (case sensitive) when the method is defined ).

  8. _ NAMESPACE _ name of the current NAMESPACE (case sensitive ). This constant is defined during compilation (new in PHP 5.3.0 ).

Example:

       '; Const TAX = 0.012; echo TAX .'
'; Echo _ DIR __; // use a magic constant to obtain the absolute path of the current file. ... Result... 3.1415926 0.012 D: \ mywamp \ Apache24 \ htdocs \ zendstudio \ yunsuanfu

When the magic constant _ DIR _ is used, the absolute path of the file is automatically obtained after running.

The above content is the use of constants, of course, more training. How can we use magic constants and files for development?

Best example of introducing files and magic constants

During development, when we introduce files, the file path is a key. we have two options: relative path and absolute path. During development, we often choose an absolute path, that is, the path of the file, such as c:/mywamp/apache24 /..., This format can be understood as an absolute path.

Here we will make a comprehensive application of the introduced files and magic constants:

The folder that we may use in the development process above. today we are simply using it, that is, the subsequent process.

  • There are some f function files in the lib folder, which contain the functions we use. we can define them as function. php.

  • In the lib init. in php, magic constants are used to define constants to represent the absolute path of a file. even if you change the path of the project, it is okay. at the same time, in init. introduce function libraries in php

  • Introduce the init. php file in index. php.

Function. php:

       

Init. php

        

In init, we use the magic constant _ DIR _ to dynamically obtain the directory where the file is located, and then use dirname () (This function returns a string containing a full path pointing to a file. the directory name after the file name is removed) to get the project root directory, then, obtain the absolute path of each folder based on the root directory, and introduce the function file through the absolute path.

Index. php

         '; Echo $ sub; ...... result ...... 33 5

You can see that the functions in the function file under lib can be normally used in the index. php file.

The above is the most basic usage of introducing files and magic constants.

Function End

Through the above introduction, we have basically finished introducing the usage of functions and constants. after learning the functions, we will review what we did not know before.

Operation variable functions
  • Isset (): checks whether the variable is set. if the value is undefined or null, false is returned.

  • Unset (): destroys the specified variable. You can input variables in the function to destroy them.

  • Empty (): determines whether a variable is considered null. If a variable does not exist, or its value is equivalent to FALSE, it is considered nonexistent.

  • Is_int (): There are many functions in the is series used to determine the type of a variable, such as is_int. if the variable is of the integer type, true is returned; otherwise, false is returned.

PHP output statementEcho

Echo is actually not a function. it is a PHP statement, so parentheses are not applicable. when you want to use echo to output multiple values, you can use commas to separate them. Echo does not return values.

Print

Print is used in the same way as echo, but echo is faster than print. print is not a function. It returns a value and always returns 1.

Print_r

Print_r (variable) prints easy-to-understand information about the variable. If the variable is string, integer, float, it will directly output its value. if the variable is an array, it will output a formatted array.

Printf

The printf function returns a formatted string. Its syntax is

printf(format,arg1,arg2)
Var_dump

Var_dump () is generally the content of the output variable, the content, type, and length of the type and string, so that we can see what the type of the variable is.

Summary

Through the two sections, the knowledge of functions and constants is over. we must be aware of the call process and operations of functions in future development.

The above is the content of functions and constants in PHP basic tutorial 6. For more information, see PHP Chinese Web (www.php1.cn )!

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.