长——Chinese literally means: long-lasting, unchanged for a long time.
Constant is easy to translate: a long-lasting value.
[Key knowledge] We define the knowledge level as: silent level
The scope of use of constants is very wide. We will use constants in the future to define our working directory, define some characteristics of account passwords, version numbers, etc. So this piece of knowledge is very important.
The definition and writing method of constants in the code:
define (constant name, constant value)
Note:
1. The constant value can only be the scalar we talked about in the previous chapter.
2. Constant names can be lowercase, but usually uppercase
3. Constant names can be without quotation marks, but usually with quotation marks.
4. When calling a constant in a string, it must be outside the quotation marks
5. It is recommended that only letters and underscores be used for constant names
Let’s experiment with code:
1. Try defining and calling a constant once
<?php
define('MY_NAME','PHP中文网');
echo MY_NAME;
//下面是错误的调用方式
echo '我的名字是MY_NAME';
//正确的调用方式该这么写
echo '我的名字是' . MY_NAME;
?>
2. Pay attention to project experiments
<?php
//其实可以小写,但是不好区分,所以我们规定通常大写
define('xiaoxie',true);
echo xiaoxie;
//常量可以在外面不加引号
define(YH,'不要对未来迷茫,迷茫的时候静下心来coding');
echo YH;
//只能用标量,我在后面用了一个数组,大家学一下就行,会报错的哟
define('BIAO',array(1,2,3));
?>
In addition, the system has also prepared some built-in constants for us. These constants are specified. Let’s get familiar with a few first. There are more system constants that we have studied in the previous volume. After getting started, we will slowly add and learn them.
Constant name | Description |
---|
##LINE | The current line |
FILE | The path of the current file on the server |
FUNCTIOIN | Current function name |
CLASS | Current class name |
METHOD | Current member method name |
PHP_OS | The operating system that PHP runs on |
PHP_VERSION | Current PHP version |
TRAIT | Trait name ,php5.4 new addition |
DIR | The directory where the file is located |
NAMESPACE | The name of the current namespace (case sensitive) |
defined() function is used as a security mechanism
You don’t need to master the knowledge here. After learning the function, you can look at this part of the code again.
defined() Let’s learn this usage, mainly to prevent others from bypassing the security check file.
Function: defined (constant)
Function: Pass the constant after the brackets of the function. If the constant is defined, it returns true, otherwise it returns false
[Scenario Simulation] Assume, we This set of online e-mall software requires payment. Checking whether the payment is made is done by checking the software authorization. The file version.php has the function of checking authorization. We have stipulated in the software that there is no authorization to check the file version. PHP cannot use this software. All code includes version.php. And in order to prevent someone from piracy, I can also encrypt the version.php code.
We have two files:
1. There is a version number, version statement and authorization statement in the middle of one file. The file name is version.php
2. A file has specific business functions. For example: user registration, login, etc., the file name is users.php
What should we do? ——That is to say, if the version.php file is not included, the code after users.php will be executed.
Let’s conduct an experiment:
version.php file
<?php
//此处是检查是否是否授权的业务部份代码xxxx
define('AUTH',true);
//略过模拟代码xxx行
?>
users.php
<?php
//尝试将include 'version.php'这一行代码注释后再执行看看,对比结果
include 'version.php';
if(!defined('AUTH')){
echo '非法!非法!你尝试跳过授权文件';
exit;
}
//模拟后面用户注册和用户登陆的代码xxx行
echo '用户注册';
?>
The experimental results show that version.php must be included, otherwise it will not be displayed The following echo 'User registration';
include
pronunciation: [ɪnˈklud]
explanation: contains
version
pronunciation: [ˈvɜ:ʃn]
Explanation: Version
user
Pronunciation: [ˈjuzɚ]
Explanation: Users
Plural: users
define
Pronunciation: [dɪˈfaɪn]
Explanation: Specifies
Function: include('Input file path and file name')
Function: The function of this function is to pass in the file with the specified path and let PHP include it for execution
Note: In the following chapters, we will specifically explain and experiment with include
Next Section<?php
define('MY_NAME','PHP中文网');
echo MY_NAME;
//下面是错误的调用方式
echo '我的名字是MY_NAME';
//正确的调用方式该这么写
echo '我的名字是' . MY_NAME;
?>
- Chapter1Why choose this course to learn PHP
- Why learn PHP?
- What is PHP
- You can learn even with z...
- Why can't some people lea...
- Chapter2PHP environment installation
- What is the development e...
- Windows environment insta...
- Linux environment install...
- Other development environ...
- Tool selection for writin...
- Chapter3php basic syntax
- PHP basic syntax
- Our first piece of PHP co...
- Variables in php - you wi...
- echo display command
- Learning php annotations
- Data types are not myster...
- PHP integer type is an in...
- PHP data type Boolean (ac...
- PHP data type string
- PHP data type floating po...
- PHP flow control if else ...
- PHP data type NULL type
- php data type array
- Resource type of php data...
- PHP data type viewing and...
- Automatic conversion and ...
- Object (will learn later)
- PHP constants and variabl...
- PHP constants and variabl...
- PHP constants and variabl...
- PHP constants and variabl...
- Variable references for P...
- PHP basic syntax arithmet...
- PHP basic syntax assignme...
- PHP basic syntax: self-in...
- PHP basic syntax comparis...
- Logical operations of php...
- PHP basic syntax bit oper...
- PHP basic syntax: ternary...
- Chapter4PHP process control
- Process control in PHP
- PHP process control if co...
- PHP flow control if state...
- Nested if...else...elseif...
- Multiple nesting of if st...
- Use of branch structure s...
- Use of loop statements in...
- while loop
- The difference between do...
- PHP flow control for loop...
- PHP flow control goto syn...
- Chapter5Basic function syntax of PHP
- Basic function syntax of ...
- PHP function basic syntax...
- PHP custom function callb...
- PHP custom function varia...
- PHP custom function anony...
- Internal function of php ...
- Variable scope of php cus...
- Reference to parameters o...
- PHP custom function recur...
- Static variables of php c...
- php uses system built-in ...
- php file contains functio...
- PHP math commonly used fu...
- PHP function to obtain pe...
- php date validation funct...
- PHP gets localized timest...
- PHP program execution tim...
- PHP string common functio...
- Chapter6PHP arrays and data structures
- PHP arrays and data struc...
- php array definition
- PHP array calculation
- php for loop traverses in...
- php foreach traverses as...
- PHP list, each function t...
- PHP commonly used array m...
- Common functions for php ...
- Chapter7Regular expressions in PHP
- Regular expressions in PH...
- Delimiter expressed by ph...
- Atoms in php regular expr...
- Metacharacters in php reg...
- Pattern modifiers in php ...
- Tips and commonly used re...
- PHP uses regular expressi...
- Chapter8php file system
- File system
- php read file
- php creates and modifies ...
- php creates temporary fil...
- php move, copy and delete...
- php detect file attribute...
- Common functions and cons...
- php file locking mechanis...
- php directory processing ...
- php file permission setti...
- php file path function
- PHP implements file guest...
- PHP implementation exampl...
- Chapter9PHP file upload
- PHP file upload
- When uploading files, you...
- Steps to upload php files
- Precautions for php file ...
- php completes file upload...
- php multiple file upload
- PHP file upload progress ...
- Chapter10PHP image processing
- PHP image processing
- PHP image processing gd2 ...
- PHP uses image processing...
- PHP development verificat...
- php image scaling and cro...
- PHP image watermark proce...
- Chapter11PHP error handling
- Error handling
- PHP error handling prohib...
- PHP error handling error ...
- PHP error handling error ...
- PHP error handling custom...
- Chapter12Getting started with MySQL
- Getting Started with MySQ...
- Mysql database introducti...
- Mysql entertainment expla...
- mysql database installati...
- Data statement operation ...
- Mysql connect to database
- Mysql database operation
- Mysql data table operatio...
- Mysql data field operatio...
- Mysql data type
- Mysql character set
- Mysql table engine
- Mysql index
- Mysql add, delete, modify...
- Mysql add, delete, modify...
- Mysql multi-table joint q...
- Mysql addition, deletion,...
- Mysql add, delete, modify...
- DCL statement
- Learn commonly used Engli...
- Chapter13PHP operates mysql database
- PHP operates mysql databa...
- PHP database connection s...
- PHP operates the database...
- PHP database operation: m...
- PHP database operation: p...
- PHP database operation: b...
- PHP database operation to...
- The ultimate solution to ...
- Chapter14php session management and control
- session overview
- Overview of Cookies for P...
- php session control Cooki...
- PHP session control using...
- php SESSION application e...
- Session management and co...
- Chapter15Making a thief program through cURL
- php curl usage methods an...
- php curl custom get metho...
- php curl uses post to sen...
- Making a thief program th...
- Chapter16Learn commonly used English words in PHP
- List of commonly used Eng...