[main] [misc] [graphics] [page design] [site design] [xhtml] [css] [xml] [xsl] [schema] [javascript] [php] [mysql]

HVCC Home
Blackboard HVCC
Blackboard Manual
Faculty Association

php main
1. what is php
2. http basics
3. php basics
4. php expressions
a. php operators
b. php math operators
* c. php other operators
5. php client side
6. php flow control
7. php manual


print version

Note that all external links will open up in a separate window.

This is a stripped down version of these pages for older browsers. These pages are really meant to be viewed in a standards compliant browser.

Directions for surfing with audio.

The Other Operators

These tutorials are about PHP and its use for server-side Web programming.

Other Operators

This section discusses the non-arithmetic operators.

Relational Operators

Relational operators allow you to test the relationship between two operands. They can test for equality between operands. They can compare operands to determine the relationship between them. There are even relational operators that test whether one operand is a property of another or of a given class of object.

The first relational operator you should know about is the equality operator.

The equality operator is represented by the double equals sign ( == ) It is used to assess whether two operands have the same value. If they do, then it returns a value of true. Otherwise it returns a value of false.

The equality operator will do data type conversion if necessary.

All of the following statements equate to true:

'1' == '1'
'1' ==  1
'1' == true  
// true equals one when converted to a number

Note that objects and arrays are compared by reference. Two objects are only equal if they are both references to the same object. If you want to test the properties of two different objects for equality, then you have to test the properties themselves, not the object.

Different arrays are never equal, even if they contain the same information. If you want to test the elements of the arrays for equality, you have to test the individual elements.

There is also an identity operator, represented by a triple equals sign ( === ), which only equates to true when both operands have the same value and are of the same type.

'1' === '1'   // true
'1' ===  1    // false
'1' === true  // false

Equality operators also have their opposites.

The inequality operator ( != returns true where equality would return false. The non-identity operator ( !== ) returns true where identity would return false.

'1' !== '1'   // false
'1' !==  1    // true

'1' != '2'    // true
'1' != '1'    // false

(a != b) == !(a == b)

The next type of relational operator are the comparison operators. Comparison operators test the relationship, or relative order, of two values. These are:

  • less than ( < )
  • greater than ( > )
  • less than or equal to ( <= )
  • greater than or equal to ( >= )

String Operators

There is really only one specific string operator, although the string object has many methods you can access.

The only string operator is the concatenation operator. It is represented by the period ( . ). The concatenation is a special case of operator. It is an overloaded operator, since it serves more than once function depending on context. It concatenates strings, but it also serves as a decimal point and a separator for object property relationships.

When working with strings, the comparison operators test on a character by character basis, so two strings that are the same except for some trailing spaces, or a difference in capitalization, will be treated as different strings. This means that comparisons can get confusing. This is especially true of strings that contain numeric values.

// compare two numeric strings
// compared as number to number
'11' < '3'       // false

// compare a numeric string and a number
// compared as number to number
'11' < 3        // false

// compare a non-numeric string to a number
// compared as a number
// 0 (or no number) is less than 3
'eleven' < 3    // true

// compare a non-numeric strings
// compared as a string
'eleven' < 'three'    // true

// compare a numeric string to a  
// non-numeric string
// compare as a string
// ACSCII 'e' is greater than ASCII '3'
'eleven' < '3'  // false

Logical Operators

There are four logical operators. Logical operators work with boolean values. The double characters for AND and OR are important, otherwise the operators mean something else.

logical AND ( && or and )
The logical AND (double ampersands) compares two boolean operands and equates to true if they are both true.
logical OR ( || or or )
The logical OR (double vertical bars) compares two boolean operands and equates to true if either one is true.
logical XOR ( xor )
The logical XOR compares two boolean operands and equates to true if one and only one is true. XOR can be simulated with (a || b) && (!(a && b))
logical NOT ( ! )
The logical NOT (exclamation point) negates the value of boolean operand, which is to say it returns true if the operand has a value of false.

Note that and, xor, and or have a significantly different position in order of precendence than && and || do. Logical operators, without parentheses, will be processed in the following order: ! && || and xor or. The fact that an AND operator and an OR operator occur twice at different points in the order of precendence can be useful in complex statements that include conditionals.

Since the expressions are evaluated in order, you can speed processing time of your scripts by coding the operand most likely to be the deciding factor first. For logical AND, if either one is false, than it returns false. Therefore, the operand most likely to be false should come first. For logical OR, if either is true, it returns true, so it makes sense to put the operand most likely to be true first.

Note that when this happens, the second operand never gets evaluated. This means you should not have any value changes in the second that may affect other parts of the code, since the expressions in the second operand may not get executed.

Casting Operators

Although PHP handles implicit casting, there are times when you may want to determine the data type of a given variable or even specify the type you want to cast a variable to. PHP has two functions and some operators to help you with this.

gettype() and settype()

gettype() and settype() allow you to determine the data type of a variable and set the data type of a variable, respectively.

gettype() returns a string that represents the name of that data type. In order to access that value, you either need to assign it to something or echo the result of the function.

$result = gettype($mysterymeat);
echo gettype($whoknows);

settype() takes two arguments. The first is the variable to be cast. The second is a string representing the name of the data type to be cast to. The data type names are the same as the names of the casting operators below. So to cast something to an integer, you could use the string "integer" or "int". The settype() function changes the type of the variable it is passed, it does not return a new value of that type.

settype($myNum, "string");
settype($myStr, "float");

The casting operators also allow you to cast variables to new types. Unlike the settype function, they return a value that is the value of the variable cast in the new type. The operator is used with the assignment operator as follows:

$result = (operator) $original;
$res = (integer) $myStr;

The casting operators are:

  • (int) or (integer) to cast to an integer
  • (float) or (real) to cast to a floating-point number
  • (string) to cast to a string
  • (bool) or (boolean) to cast to a boolean
  • (array) to cast to an array
  • (object) to cast to an object

Be aware of the fact that casting will may change the value of the variable as well as its type. For instance, casting a floating-point number to an integer will truncate any fractional part.

$a = (int) 4.1715   // $a == 4
$b = (int) "123abc" // $b == 123

Some casts may also not produce any meaningful results. For instance, casting an Array to a string yields a string whose value is "Array". However, you can switch back and forth between arrays and objects freely with the index values becoming the properties. If properties of the array cannot be cast to object properties, PHP will even keep those values so that they will be restored when the variable is cast back to an array. This most commonly occurs because it is possible to have index names that are not valid property names.

A few more operators

Just three more operators worth mentioning right now.

The ampersand ( & ) before a variable (&$abc) overrides pass by value to specify that you want to pass the variable by reference instead. The ampersand is an overloaded operator and character in PHP, so use carefully.

The at sign ( @ ) before an operator or function call tells PHP to not report any errors from it should any errors arise.

The backtick, or grave accent ( ` ), can be used to define a string as a command to be passed to the server. The string is executed as a command and its results are returned.

$dirListing = `ls -l`;

These three are all advanced topic operators and will be dealt with in more detail elsewhere.

[top]