Multiply and assign, divide and assign, and modulus and assign

Numeric assignments using the multiply and assign operator may be performed using *=. The value held by the variable i will be 4:

$i = 2 $i *= 2 

The multiply and assign operator may be used to duplicate a string held in a variable:

$string = 'one' 
$string *= 2 

The value on the right-hand side of the *= operator must be numeric or must be able to convert to a number. For example, a string containing the number 2 is acceptable:

$string = 'one' 
$string *= '2' 

Using a string that is unable to convert to a number results in an error, as follows:

PS> $variable = 'one'
PS> $variable *= 'one'

Cannot convert value "one" to type "System.Int32". Error: "Input string was not in a correct format."
At line:2 char:1
+ $variable *= 'one'
+ ~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvalidCastFromStringToInteger

The multiply and assign operator may be used to duplicate an array held in a variable. In the following example, the variable will hold the values 1, 2 , 1, and 2 after this operation:

$variable = 1, 2 
$variable *= 2 

The assign and divide operator is used to perform numeric operations. The variable will hold a value of 1 after the following operation:

$variable = 2 
$variable /= 2 

The remainder and assign operator assigns the result of the remainder operation to a variable:

$variable = 10 
$variable %= 3 

After the preceding operation, the variable will hold a value of 1, which is the remainder when dividing 10 by 3.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.50.183