PHP Expressions for PHP Learning_PHP Tutorial
Jul 21, 2016 pm 04:04 PM
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.

Hot AI Tools

Undress AI Tool
Undress images for free

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

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

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 method to get the current session ID in PHP is to use the session_id() function, but you must call session_start() to successfully obtain it. 1. Call session_start() to start the session; 2. Use session_id() to read the session ID and output a string similar to abc123def456ghi789; 3. If the return is empty, check whether session_start() is missing, whether the user accesses for the first time, or whether the session is destroyed; 4. The session ID can be used for logging, security verification and cross-request communication, but security needs to be paid attention to. Make sure that the session is correctly enabled and the ID can be obtained successfully.

To extract substrings from PHP strings, you can use the substr() function, which is syntax substr(string$string,int$start,?int$length=null), and if the length is not specified, it will be intercepted to the end; when processing multi-byte characters such as Chinese, you should use the mb_substr() function to avoid garbled code; if you need to intercept the string according to a specific separator, you can use exploit() or combine strpos() and substr() to implement it, such as extracting file name extensions or domain names.

UnittestinginPHPinvolvesverifyingindividualcodeunitslikefunctionsormethodstocatchbugsearlyandensurereliablerefactoring.1)SetupPHPUnitviaComposer,createatestdirectory,andconfigureautoloadandphpunit.xml.2)Writetestcasesfollowingthearrange-act-assertpat

In PHP, the most common method is to split the string into an array using the exploit() function. This function divides the string into multiple parts through the specified delimiter and returns an array. The syntax is exploit(separator, string, limit), where separator is the separator, string is the original string, and limit is an optional parameter to control the maximum number of segments. For example $str="apple,banana,orange";$arr=explode(",",$str); The result is ["apple","bana

JavaScript data types are divided into primitive types and reference types. Primitive types include string, number, boolean, null, undefined, and symbol. The values are immutable and copies are copied when assigning values, so they do not affect each other; reference types such as objects, arrays and functions store memory addresses, and variables pointing to the same object will affect each other. Typeof and instanceof can be used to determine types, but pay attention to the historical issues of typeofnull. Understanding these two types of differences can help write more stable and reliable code.

std::chrono is used in C to process time, including obtaining the current time, measuring execution time, operation time point and duration, and formatting analysis time. 1. Use std::chrono::system_clock::now() to obtain the current time, which can be converted into a readable string, but the system clock may not be monotonous; 2. Use std::chrono::steady_clock to measure the execution time to ensure monotony, and convert it into milliseconds, seconds and other units through duration_cast; 3. Time point (time_point) and duration (duration) can be interoperable, but attention should be paid to unit compatibility and clock epoch (epoch)

In PHP, to pass a session variable to another page, the key is to start the session correctly and use the same $_SESSION key name. 1. Before using session variables for each page, it must be called session_start() and placed in the front of the script; 2. Set session variables such as $_SESSION['username']='JohnDoe' on the first page; 3. After calling session_start() on another page, access the variables through the same key name; 4. Make sure that session_start() is called on each page, avoid outputting content in advance, and check that the session storage path on the server is writable; 5. Use ses

ToaccessenvironmentvariablesinPHP,usegetenv()orthe$_ENVsuperglobal.1.getenv('VAR_NAME')retrievesaspecificvariable.2.$_ENV['VAR_NAME']accessesvariablesifvariables_orderinphp.iniincludes"E".SetvariablesviaCLIwithVAR=valuephpscript.php,inApach
