/// A structure whose contents are public, so external users can construct/// instances of it.#[derive(Debug)]pubstructExposedStruct{pubdata:Vec<u8>,/// Additional data that is required only when the `schema` feature/// is enabled.#[cfg(feature ="schema")]pubschema:String,}
firstName lastName
---------- ----------
John Doe
Peter Miller
Nick Anderson
Jane Doe
Peter Doe
Arithmetic operators are used for calculations. The following are available: + (addition), - (subtraction), * (multiplication), / (division), and % (modulo). In addition, these operators include the increment and decrement operators (++ and --), which add or subtract 1 from a number. A power of a number can be calculated by using **.
The assignment operator is the equal sign (=). You’ve already encountered this operator whenever we’ve set values for variables. In addition, combined operators allow you to use the value of a variable in an expression and then assign the result of that expression as a new value (shown later in Listing 15.17). These operators include += (for addition), -= (for subtraction, *= (for multiplication), /= (for division), %= (for modulus calculation), **= (for exponentiation), and .= (the string operator for concatenation).
Bit operators allow you to check and manipulate specific bits in an integer. The following bit operators are available: & (bitwise AND), | (bitwise OR), ^ (bitwise XOR), ~ (bitwise NOT), << (bitwise shift to the left), and >> (bitwise shift to the right).
Comparison operators allow you to compare values directly. The following operators are available: == (equal to), === (identical to), != or <> (not equal to), !== = (not identical to), < (less than), > (greater than), <= (less than or equal to), >= (greater than or equal to), and <=> (spaceship, see box). “Identical to” in PHP means not only is the value identical but also its type. Refer to Listing 15.12 for more information on this topic.
<?php
$a = '1';
$b = 1;
$c = $a == $b; // Returns true.
$d = $a === $b; // Returns false because the type comparison fails.
Listing 15.12 Comparisons of Equality and Identity
Operator for program execution: Backticks (``) can be used to execute external programs. Note that backticks are not simple execution characters and that execution will fail if the shell_exec() function has been disabled. The line $file_list = `ls -al`; tries to execute the ls command line tool, which lists all files and directories of the current folder.
Logical operators are for using Boolean values. The following operators can be used: and or && (logical AND), or or || (logical OR), xor (exclusive OR), and ! (negation).
Array operators are used to compare or manipulate arrays. $a + $b unites two arrays. == returns true if both arrays contain the same key-value pairs. === returns true if both arrays contain the same key-value pairs in the same order and they are of the same type. Accordingly, != or <> mean that two arrays are not equal. !== in turn means “not identical,” where a type comparison is also performed.
Type operator: The instanceof keyword checks if an object belongs to a certain class.
The ternary operator (shown in Listing 15.13) is another comparison operator that returns an expression and replaces a longer if statement. This operator also available in the short version, where the middle part can be omitted, as shown in Listing 15.14.
<?php
$action = ( empty( $_POST['action'] ) ) ? 'standard' : $_POST['action'];
// The above is identical to the IF statement below.
if (empty($_POST['action'])) {
$action = 'standard';
} else {
$action = $_POST['action'];
}
Listing 15.13 Ternary Operator
<?php
$firstName = '';
$firstName = $firstName ?: 'John'; // Contains 'John'.
$firstName = 'Anne';
$firstName = $firstName ?: 'John'; // Contains 'Anne'.
Listing 15.14 Short Ternary Operator
Quite similar to the ternary operator is the null coalescing operator. This operator is also an assignment operator in combination with NULL, as shown in Listing 15.15. In particular, the result of the left side does not give any hint or warning if the value does not exist.
<?php
$firstName = $firstName ?? 'John'; // Returns 'John'
$firstName = 'Anne';
$firstName = $firstName ?? 'John'; // Returns 'Anne'
Listing 15.15 Null Coalescing Operator
<?php
$action = ( empty( $_POST['action'] ) ) ? 'standard' : $_POST['action'];
// The above is identical to the IF statement below.
if (empty($_POST['action'])) {
$action = 'standard';
} else {
$action = $_POST['action'];
}