ThinkPHP的自定义恒量和系统常量的使用
ThinkPHP的自定义常量和系统常量的使用
常量我们都知道就是定义好一个不变的量,在php中常量的定义规则是define("常量名",“常量值”);
即在所有的页面中就就可以去使用这个常量单纯的定义几个常量没有任何意义,重要的是要去使用常量区优化代码,我所说优化代码就是提高代码的可维护性,有时候还起到简化代码的作用,但这绝对不是主要的目的。
在传统的代码书写中,我们就采用的是直接书写的方式,比如url地址等,这些东西虽然说直接去些可以去实现功能呢但是,在程序的后期维护过程中,就先得非常的困难,虽然说可以取使用替换功能,但是这不是长久之计,而且存在比这个更好的方法,就是定义常量来使用,这样的,在后期的维护中,如果是更改盘符或者变量信息,就直接修改变量就可以,虽然说道理很简单,但是能够灵活的运用却不简单。
另外除了自定义常量以外,还有一些系统常量,如果想看到这些常量信息,可以用以下的代码去实现:
// echo "
";<br>// var_dump(get_defined_constants());<br>// echo "
你会卡到一大串东西,里面是所有的系统常量提供你去使用,但是说明一点的是,无论是我们自定义的常量还是系统常量,比较常用的都在后面那一片,这一点是要注意的:
下面是我定义常量的一份实例代码,以think模板为例
//定义网站域名常量 define("SET_URL","http://shop.com/"); //定义前台HOME模块的css,img。js的常量信息 define("CSS_URL",SET_URL."Public/Home/css/"); define("IMG_URL",SET_URL."Public/Home/img/"); define("JS_URL",SET_URL."Public/Home/js/"); //定义后台Admin模块的css,img。js的常量信息 define("ADMIN_CSS_URL",SET_URL."Public/Admin/css/"); define("ADMIN_IMG_URL",SET_URL."Public/Admin/img/"); define("ADMIN_JS_URL",SET_URL."Public/Admin/js/");
我们可以看到,定义常量也要有一定的技巧,一些标志性的东西要单独定义,如网站域名,还要注意常量的分组设计,做到合理有序,才能提高代码的维护性。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

The reason for the error is NameResolutionError(self.host,self,e)frome, which is an exception type in the urllib3 library. The reason for this error is that DNS resolution failed, that is, the host name or IP address attempted to be resolved cannot be found. This may be caused by the entered URL address being incorrect or the DNS server being temporarily unavailable. How to solve this error There may be several ways to solve this error: Check whether the entered URL address is correct and make sure it is accessible Make sure the DNS server is available, you can try using the "ping" command on the command line to test whether the DNS server is available Try accessing the website using the IP address instead of the hostname if behind a proxy

PHP function introduction—get_headers(): Overview of obtaining the response header information of the URL: In PHP development, we often need to obtain the response header information of the web page or remote resource. The PHP function get_headers() can easily obtain the response header information of the target URL and return it in the form of an array. This article will introduce the usage of get_headers() function and provide some related code examples. Usage of get_headers() function: get_header

Differences: 1. Different definitions, url is a uniform resource locator, and html is a hypertext markup language; 2. There can be many urls in an html, but only one html page can exist in a url; 3. html refers to is a web page, and url refers to the website address.

Nowadays, many Windows users who love games have entered the Steam client and can search, download and play any good games. However, many users' profiles may have the exact same name, making it difficult to find a profile or even link a Steam profile to other third-party accounts or join Steam forums to share content. The profile is assigned a unique 17-digit id, which remains the same and cannot be changed by the user at any time, whereas the username or custom URL can. Regardless, some users don't know their Steamid, and it's important to know this. If you don't know how to find your account's Steamid, don't panic. In this article

define defines a multi-line macro by using `\` to divide `do { \ printf("%d\n", x); \ } while (0)` into multiple lines for definition. In a macro definition, the backslash `\` must be the last character of the macro definition and cannot be followed by spaces or comments. When using `\` for line continuation, be careful to keep the code readable and make sure there is a `\` at the end of each line.

Use url to encode and decode the class java.net.URLDecoder.decode(url, decoding format) decoder.decoding method for encoding and decoding. Convert into an ordinary string, URLEncoder.decode(url, encoding format) turns the ordinary string into a string in the specified format packagecom.zixue.springbootmybatis.test;importjava.io.UnsupportedEncodingException;importjava.net.URLDecoder;importjava.net. URLEncoder

The importance and role of the define function in PHP 1. Basic introduction to the define function In PHP, the define function is a key function used to define constants. Constants will not change their values during the running of the program. Constants defined using the define function can be accessed throughout the script and are global. 2. The syntax of define function The basic syntax of define function is as follows: define("constant name","constant value&qu

Every year before Apple releases a new major version of iOS and macOS, users can download the beta version several months in advance and experience it first. Since the software is used by both the public and developers, Apple has launched developer and public versions, which are public beta versions of the developer beta version, for both. What is the difference between the developer version and the public version of iOS? Literally speaking, the developer version is a developer test version, and the public version is a public test version. The developer version and the public version target different audiences. The developer version is used by Apple for testing by developers. You need an Apple developer account to download and upgrade it.
