Microsoft Word - Core PHP Programming Using PHP to Build Dynamic Web Sites

(singke) #1

Retrieving Values


To use the value stored in a variable, use it anywhere where a value is required, such as
the argument to a function or in an expression. For example, if you wish to print the value
to the browser, you can type print($s) to print the value of a variable named s. You can
even set a variable with the value of another, such as $s = $t.


If a variable contains a string, you may refer to each character using square brackets.
Each character is numbered starting with zero. To refer to the seventh character in the s
variable, you would type $s[6]. This notation works both ways, in fact. You can set a
single character of a string with an expression like $s[6] ="x". Only the first character
of the value on the right-hand side will be used to replace the specified character. If the
variable on the left-hand side is not a string, it will be unchanged. Listing 2.5
demonstrates the use of square brackets to reference single characters.


Listing 2.5 Referencing a Single Character

Free download pdf