Home Backend Development PHP Tutorial PHP Expressions for PHP Learning_PHP Tutorial

PHP Expressions for PHP Learning_PHP Tutorial

Jul 21, 2016 pm 04:04 PM
php thing element exist study yes composition expression

PHP expression

Expression is the most important element of PHP. In PHP 3.0, almost everything you write is an expression. The simplest but precise definition of an expression is "anything that has a value". A simple example is constants and variables.
When you write "$a = 5", you assign the value '5' to $a. (In this case, '5' is an integer constant). Here, you want to assign $a to 5. So when you write $b = $a, the desired result is $b = 5. That is, $a is an expression with a value of 5. A simple example of a complex expression is a function.
For example, consider the following function:   function foo()   {    return 5; A function is an expression whose value is its return value. Because foo() returns 5, the expression 'foo()' evaluates to 5 .
PHP’s values ​​are of course not limited to integers, and usually aren’t. PHP supports three types of values: integer values, floating point values ​​and string values. PHP supports two mixed types (non-scalar): arrays and objects. Values ​​of both types can be assigned to variables or returned from functions.
PHP 3 is an expression-oriented language, so almost everything is an expression.
Consider the example we have discussed, '$a = 5'. It's easy to see that there are two values ​​here, the value of the integer constant '5', and the value of the variable $a, which is also assigned to 5. But there is actually an additional value here, which is the value of the assignment statement itself.
The value of the assignment statement itself is the value being assigned, which is 5 in this case. In fact, it means that regardless of what '$a = 5' does, it is an expression with a value of 5. Thus, writing statements such as '$b = ($a = 5)' is like '$a = 5; $b = 5;' (with a semicolon at the end of each statement). Because the order of assignment is from right to left, you can also write '$b = $a = 5'. ​

Another good example of the direction of expression evaluation is add first, add last and subtract first, then subtract. Users of PHP/FI and most other languages ​​are probably familiar with variable++ and variable--. This is the self-increment and self-decrement operation. In PHP/FI 2, the statement '$a++' has no value (it is not an expression), so you can neither assign to it nor use it in any way. PHP 3 turns them into the same expressions as in C, thereby enhancing the capabilities of auto-increment and auto-subtraction operations.
Similar to C, there are two types of self-add in PHP 3 - add first and add last. The essence of adding first and adding later is that the variables add themselves, and they have the same effect on the variables themselves. The difference is the value of the self-increasing expression. Add first in the form of '++$variable', calculate the value after the variable is added (PHP first adds the variable, and then read its value, which is also called 'add first'). Add in the form of '$variable++' Post-adding, the value of the original variable $variable is calculated first, and then self-adding is performed (PHP does self-adding after reading the value of the variable, so it is called 'post-adding').
The most common expression is comparison expression. This expression evaluates to 0 or 1, which means FALSE or TRUE respectively.
PHP supports > (greater than), >= (greater than or equal to), == (equal to), < (less than) and <= (less than or equal to). This kind of expression is usually used in conditional execution, such as IF statement.
The last expression we want to discuss here is the mixed assignment expression. You already know that if you want to increment $a, you can simply write '$a++' or '++$a'. But what if the value you want to increment is larger than 1, for example to make it increase by 3? You could write '$a++' a few more times, but this is obviously not an efficient or acceptable way. Another common approach is to write '$a = $a + 3'. First calculate the value of '$a + 3', and then assign it back to $a, so that $a is added to 3. In PHP 3, you can abbreviate it like you do in several other languages ​​(such as C), which makes it clearer, faster and easier to understand. Adding 3 to the current variable $a can be written as '$a += 3'. This sentence means "take the value of $a, add 3 to it, and assign it to $a". In addition to making the statement shorter and clearer, this also makes it execute faster. The value of the expression '$a += 3', like a strict assignment statement, is the assigned value. Note: It is not 3, but the value of $a plus 3 (this is what is assigned to $a). Any double operator can be used in this assignment mode, such as '$a -= 5' (variable $a minus 5), '$b *= 7' (variable $b multiplied by 7), etc. .
The last thing worth mentioning is the truth value of expressions. Many times (mainly in conditional execution and looping), you don't care about the specific value of an expression, but only whether it represents TRUE or FALSE (PHP does not have a dedicated Boolean type). PHP uses a perl-like method to calculate the truth value of an expression. Any non-zero value is TRUE, zero is FALSE. It is important to note that the value of negative zero is non-zero and is considered TRUE! The empty string can be FALSE; all other strings are TRUE. For non-quantitative values ​​(arrays and objects) - FALSE if its value does not contain any elements, TRUE otherwise.


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/315992.htmlTechArticlePHP expression Expression is the most important element of PHP. In PHP 3.0, almost everything you write is an expression. The simplest but precise definition of an expression is anything that has a value...
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

See all articles