English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Detailed Explanation of thinkPHP Expression Query Usage

This article illustrates the expression query of ThinkPHP. Shared for reference, the details are as follows:

ThinkPHP Expression

The expression mentioned here refers to the special expression in the ThinkPHP framework. These expressions are used for the where conditions of query or update and delete operations, as well as in template tags.

The expression is used in the Where condition

The format of the Where condition expression is:

$map['field name'] = array('expression', 'operation condition');

Where $map is a common array variable and can be named according to your needs. The expression in the above format actually represents the meaning of the operator:

Comparison table of ThinkPHP operator and SQL operator
TP operator SQL operator Example Actual query condition
eq = $map['id'] = array('eq',100); Equivalent to: $map['id'] = 100;
neq != $map['id'] = array('neq',100); id != 100
gt > $map['id'] = array('gt',100); id > 100
egt >= $map['id'] = array('egt',100); id >= 100
lt < $map['id'] = array('lt',100); id < 100
elt <= $map['id'] = array('elt',100); id <= 100
like like $map['username'] = array('like','Admin%'); username like 'Admin%'
between between and $map['id'] = array('between','1,8); id BETWEEN 1 AND 8
not between not between and $map['id'] = array('not between','1,8); id NOT BETWEEN 1 AND 8
in in $map['id'] = array('in','1,5,8); id in(1,5,8);
not in not in $map['id'] = array('not in','1,5,8); id not in(1,5,8);
and (default) and $map['id'] = array(array('gt',1),array('lt',10)) (id > 1) AND (id < 10);
, 'or', , 'or', $map['id'] = array(array('gt',3),array('lt',10), 'or'); (id > 3) OR (id < 10);
xor (exclusive or) xor Result is true when only one of the two inputs is true, otherwise false. Example omitted. 1 xor 1 = 0
exp Comprehensive expression $map['id'] = array('exp','in(1,3,8); $map['id'] = array('in','1,3,8);

Supplementary instructions

Like SQL, ThinkPHP operators are case-insensitive, eq is the same as EQ.

between, in conditions support strings or arrays, which are equivalent to the following two writing methods:

$map['id'] = array('not in','1,5,8);
$map['id'] = array('not in',array('1','5','8'));

exp expression

The exp in the table above is not an operator, but a composite expression to support more complex condition settings. The operation conditions of exp will not be treated as strings and can use any SQL supported syntax, including using functions and field names.

exp is not only used for where conditions, but can also be used for data updates, such as:

$Dao = M("Article");
// Build the data array for save, article click count+1
$data['aid'] = 10;
$data['counter'] = array('exp','counter',+1);
// Save the modified data according to the conditions
$User->save($data);

Note: For addition and subtraction of numeric fields, you can directly use

Readers who are interested in more content related to ThinkPHP can check the special topics on this site: 'ThinkPHP入门教程', 'ThinkPHP模板操作技巧总结', 'ThinkPHP常用方法总结', 'smarty模板入门基础教程', and 'PHP模板技术总结'.

I hope the description in this article will be helpful to everyone's PHP program design based on the ThinkPHP framework.

Statement: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been manually edited, and does not assume relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#oldtoolbag.com (Please replace # with @ when sending an email for reporting, and provide relevant evidence. Once verified, this site will immediately delete the content suspected of infringement.)

You May Also Like