Tuesday, 7 July 2015

Casting a Variable

Changing Type by Casting
By placing the name of a data type in brackets in front of a variable, you create a
copy of that variable's value converted to the data type specified. The principal
difference between settype() and a cast is the fact that casting produces a copy,
leaving the original variable untouched. Listing 4.6 illustrates this.
Listing 4.6: Casting a Variable
1: <html>
2: <head>
3: <title>Listing 4.6 Casting a variable</title>
4: </head>
5: <body>
6: <?php
7: $undecided = 3.14;
8: $holder = ( double ) $undecided;
9: print gettype( $holder ) ; // double
10: print " -- $holder<br>"; // 3.14
11: $holder = ( string ) $undecided;
12: print gettype( $holder ); // string
13: print " -- $holder<br>"; // 3.14
14: $holder = ( integer ) $undecided;
15: print gettype( $holder ); // integer
16: print " -- $holder<br>"; // 3
17: $holder = ( double ) $undecided;
18: print gettype( $holder ); // double
19: print " -- $holder<br>"; // 3.14
20: $holder = ( boolean ) $undecided;
21: print gettype( $holder ); // boolean
22: print " -- $holder<br>"; // 1
23: ?>
24: </body>
25: </html>
We never actually change the type of $undecided, which remains a double
throughout. In fact, by casting $undecided, we create a copy that is then converted
to the type we specify. This new value is then stored in the variable $holder.
Because we are working with a copy of $undecided, we never discard any
information from it as we did in Listing 4.5.
Operators and Expressions
You can now assign data to variables. You can even investigate and change the data
type of a variable. A programming language isn't very useful, though, unless you
can manipulate the data you can store. Operators are symbols that make it possible
to use one or more values to produce a new value. A value that is operated on by an
operator is referred to as an operand.
NEW
TERM
An operator is a symbol or series of symbols that, when used in
conjunction with values, performs an action and usually produces a
new value.
NEW
TERM
An operand is a value used in conjunction with an operator. There
are usually two operands to one operator.
Let's combine two operands with an operator to produce a new value:
4 + 5
4 and 5 are operands. They are operated on by the addition operator (+) to produce
9. Operators almost always sit between two operands, though you will see a few
exceptions later in this hour.
The combination of operands with an operator to manufacture a result is called an
expression. Although most operators form the basis of expressions, an expression
need not contain an operator. In fact in PHP, an expression is defined as anything
that resolves to a value. This includes integer constants such as 654, variables such
as $user, and function calls such as gettype(). The expression (4 + 5), therefore is
an expression that consists of two further expressions and an operator.
NEW
TERM
An expression is any combination of functions, values, and
operators that resolve to a value. As a rule of thumb, if you can use
it as if it were a value, it is an expression.
Now that we have the principles out of the way, it's time to take a tour of PHP4's
more common operators.
The Assignment Operator
You have met the assignment operator each time we have initialized a variable. It
consists of the single character =. The assignment operator takes the value of its
right-hand operand and assigns it to its left-hand operand:
$name ="matt";
The variable $name now contains the string "matt". Interestingly, this construct is
an expression. It might look at first glance that the assignment operator simply
changes the variable $name without producing a value, but in fact, a statement that
uses the assignment operator always resolves to a copy of the value of the right
operand. Thus
print ( $name = "matt" );
prints the string "matt" to the browser in addition to assigning "matt" to $name.
Arithmetic Operators
The arithmetic operators do exactly what you would expect. Table 4.2 lists these
operators. The addition operator adds the right operand to the left operand. The
subtraction operator subtracts the right-hand operand from the left. The division
operator divides the left-hand operand by the right. The multiplication operator
multiplies the left-hand operand by the right. The modulus operator returns the
remainder of the left operand divided by the right.
Table 4.2: Arithmetic Operators
Operator Name Example Example Result
+ Addition 10+3 13
− Subtraction 10− 3 7
/ Division 10/3 3.3333333333333
* Multiplication 10*3 30
% Modulus 10%3 1
The Concatenation Operator
The concatenation operator is a single dot. Treating both operands as strings, it
appends the right-hand operand to the left. So
"hello"." world"
returns
"hello world"
Regardless of the data types of the operands, they are treated as strings, and the
result always is a string.
More Assignment Operators
Although there is really only one assignment operator, PHP4 provides a number of
combination operators that transform the left-hand operand as well as return a
result. As a rule, operators use their operands without changing their values.
Assignment operators break this rule. A combined assignment operator consists of
a standard operator symbol followed by an equals sign. Combination assignment
operators save you the trouble of using two operators yourself. For example,
$x = 4;
$x += 4; // $x now equals 8
is equivalent to
$x = 4;
$x = $x + 4; // $x now equals 8
There is an assignment operator for each of the arithmetic operators and one for the
concatenation operator. Table 4.3 lists some of the most common.
Table 4.3: Some Combined Assignment Operators
Operator Example Equivalent to
+= $x += 5 $x = $x + 5
− = $x − = 5 $x = $x − 5
/= $x /= 5 $x = $x / 5
*= $x *= 5 $x = $x * 5
%= $x%=5 $x = $x % 5
.= $x .= "test" $x = $x" test"
Each of the examples in Table 4.3 transforms the value of $x using the value of the
right-hand operand.
Comparison Operators
Comparison operators perform tests on their operands. They return the boolean
value true if the test is successful, or false otherwise. This type of expression is
useful in control structures, such as if and while statements. You will meet these in
Hour 5.
To test whether the value contained in $x is smaller than 5, for example, you would
use the less than operator:
$x < 5
If $x contained 3, this expression would be equivalent to the value true. If $x
contained 7, the expression would resolve to false.
Table 4.4 lists the comparison operators.
Table 4.4: Comparison Operators
Operator Name Returns
True if
Example Result
== Equivalence Left is
equivalent
to right
$x == 5 false
!= Non-equivalence Left is not
equivalent
to right
$x != 5 true
=== Identical Left is
equivalent
to right and
they are the
same type
$x === 5 false
> Greater Left is than
greater
than right
$x >4 false
>= Greater than or
equal to
Left is
greater
than or
equal to
right
$x >= 4 true
< Less than Left is less
than right
x < 4 false
<= Less than or equal
to
Left is less
than or
equal to
right
$x <= 4 true
These operators are most commonly used with integers or doubles, although the
equivalence operator is also used to compare strings.
Creating More Complex Test Expressions with the Logical Operators
The logical operators test combinations of booleans. The or operator, for example
returns true if either the left or the right operand is true.
true || false
would return true.
The and operator only returns true if both the left and right operands are true.
true && false
would return false. It's unlikely that you would use a logical operator to test boolean
constants, however. It would make more sense to test two or more expressions that
resolve to a boolean. For example,
( $x > 2 ) && ( $x < 15 )
would return true if $x contained a value that is greater than 2 and smaller than 15.
We include the parentheses to make the code easier to read. Table 4.5 lists the
logical operators.

No comments:

Post a Comment