String Operators
| . | Concatenation Operator |
$n = 5 ; $s = 'There were ' . $n . ' ducks.' ; | |
| Of course, you can also concatenate strings by using double quoted strings. | |
$n = 5 ;
$s = "There were $n ducks." ; // Same as above.
| |
| Be careful about mixing variable types. PHP doesn't care if you add or concatinate a number and a string but it treats them differently. | |
$n = 5 ; $x = "Babylon" ; echo $x . $n ; // will print Babylon5 echo $x + $n ; // will just print 5 echo ++$x ; // will print Babyloo |
String Functions
| strlen() | String Length function. Returns the number of characters in the string. |
$comments = "Good Job" ;
$len = strlen($comments) ; // $len is now 8
| |
| trim() | Trim function. Removes any blank characters from the beginning and end of a string |
| strtolower() | String to Lower function. Returns the string in all lowercase letters. |
| strtoupper() | String to Upper function. Returns the string in all uppercase letters. |
| substr() | Substring function. Returns a portion of the string based on the parameters given. It has the following basic syntax: |
$portion = substr( $string, start, length ) ; | |
| It is important to note that the "start" positions start with zero (0), not one (1) (i.e. it is zero based). Some examples: | |
$date = "02/28/2525" ;
$month = substr($date, 0, 2) ;
$day = substr($date, 3, 2) ;
$year = substr($date, 6) ; // $year is 2525
| |
| In the third example, since no length was specified, it continued to the end of the string. You may also specify negative starting values if you know how close to the end of the string you want to begin. | |
$year = substr($date, -4) ; // $year is 2525
|
The list of all the PHP operators is here.
Copyright © 2003
Henry H. Hartley
All Rights Reserved