Miscellaneous Operators PHP

There are operators that don't fit into any of the previous categories: the concatenationoperator, the variable marker, the reference operator, and others. lists them.

The concatenation operator is similar to the addition operator except that it joins two strings. I find this operator indispensable. When issuing a print, it is convenient to concatenate several strings. I also use the concatenation operator to build database queries. is an example of doing this.

When variables were discussed earlier, it was shown that a dollar sign always precedes the name of a variable. This is true whether the variable is global, local, or a function argument. Theperator can be taken to mean, "Use the value stored in the named variable." If an ampersand precedes the dollar sign, it changes the meaning of the operation to be, "Use the memory set aside to store the data for the variable." This is similar to the new operator in C++ and other languages. This subtle difference is useful in declaring and calling functions.

Miscellaneous OperatorsMiscellaneous Operators

When a function is called with an argument, the value of the argument is passed to the function and put into the special argument variable in the function declaration. If a variable is used inside a function call, only the value of the variable is sent to the function. If you choose to change the value of an argument, the original variable will be unchanged.

However, if you put an ampersand before the dollar sign in a function declaration, the function will expect a reference to a variable. Inside the function the argument acts like an alias to the supplied variable; any change to the argument changes the variable named in the function call.

Outside of functions, the ampersand allows you to make more than one variable point to the same area of memory. This is like making an alias. Operations on either variable will change the underlying memory.

The Concatination Operator

<?
$Query = "SELECT LastName, FirstName " .
"FROM Clients " .
"WHERE Disposition = 'Pleasant' " .
"ORDER BY LastName ";
print($Query);
?>

The Reference Operator

The Reference  Operator

The dollar-sign operator may operate on the result of another dollar-sign operator. In the simplest case a variable holds the name of another variable. This is shown in Note that { and } is used for grouping as the parentheses are used for numbers. This eliminates the ambiguity that can arise when referencing arrays. It also allows you to specify elements of multidimensional arrays inside strings. But even when not strictly necessary, it's a good idea to use curly braces as I have in It's clear that I mean to use a variable to name another variable here.

The dollar-sign operator is unique because it is executed when placed inside double quotes. This allows you to avoid the extra code needed to break from a string to insert the value of a variable. But dollar signs inside double quotes do not behave exactly like dollar signs outside double quotes. When two or more dollar signs appear together, all but
the last will be treated as any other character with no meaning. To use one variable to name another, use curly braces. demonstrates the subtleties of this functionality.

The -> operator is used strictly to reference either methods or properties of classes. The left-hand side of the operator is the name of an instantiated class; the right-hand side is the name of a function or variable inside the class.

Using Variables to Name Variables

//set variables

The => operator is used in declaring arrays, discussed in Chapter 5. When creating an array with the array statement, you may specify the index for an element with the => operator. The left-hand side of the operator is the index and the right-hand side is the value. This operator is also used by the foreach statement in much the same way.

The ? operator is equivalent to an if statement. It is called a tertiary operator, because it takes three parameters: an expression that is evaluated to be TRUE or FALSE, an expression that's evaluated if the first is true, and an expression that's evaluated if the first is false.

The @ operator suppresses any error messages when it precedes an expression. Normally when a built-in function encounters an error, text is sent directly to the browser.

Sometimes this is just warning text. If you want to suppress any error or warning messages, place @ directly before the name of the function. You may also place @ before an expression if you anticipate an error condition, such as division by zero. Error messages may also be suppressed for all functions in a script with the error_reporting function.


Face Book Twitter Google Plus Instagram Youtube Linkedin Myspace Pinterest Soundcloud Wikipedia

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

PHP Topics