September 21, 2009
Self-taught Php: String Functions Quick Start
Posted by: admin : Category: PHP & HTML
Self-Taught PHP – String Functions Quick Start
by Bill Hamilton
An on-the-fly kind of programmer looking at an unfamiliar lanquage needs first of all a reference of the set of necessary and commonly used functions. This tutorial provides a quick reference of the essential string functions followed by some condensed notes and then a very brief list of PHP operators and process control.
Functions
substr($S,start,length) portion of string S from start with length
strstr($S,$lookfor) first part of string S, up to string lookfor
strstr($S,$lookfor,True) last part of string S, beginning with string lookfor
strrchr($S,$lookfor) last part of string S, beginning with last character lookfor
strpos($S,$lookfor) first position of string lookfor in string S
strrpos($S,$lookfor) last position of string lookfor in string S
strlen($S) Length of string S
chr(ascii) Character specified by ascii code
substr_replace($S,$R,s,L) Put $R into $S, beginning at position s (optional length L)
str_replace($F,$R,$S) find and replace string $F in string $S, replacing with $R
trim($S) strip whitespaces from end and beginning of $S (also ltrim and rtrim)
$S.$T concantentate strings, place string $T at the end of $S
echo “hello world” writes “hello world”
Here are some translation examples for Visual Basic programmers:
Basic PHP Returns
right(“abcde”,3) substr(“abcde”,-3) “cde”
left(“abcde”,3) substr(“abcde”,0,3) “abc”
mid(“abcde”,3,2) substr(“abcde”,2,2) “cd” (count starts at 0)
len(“abcde”) strlen(“abcde”) 5
replace(“abcd”,”bc”,”XY”) str_replace(“bc”,”XY”,”abcd”) “aYXd”
instr(“abcd”,”c”) strpos(“abcd”,”c”) 2 (starts at 0)
chr(27) chr(27) escape character
ltrim(” abc”) ltrim(” abc”) “abc”
These are the basic string functions you expect of a scripting lanquage – finding a substring, replacing portions of a string, and breaking a string up. All of the indexes into the string are zero-based (the first character is at position 0). A handy feature to keep in mind is that the starting positions can be negative, which means you count from the end of the string rather than the beginning. The final necessary function is combining strings, which in PHP is simply a period operator.
Process Control
PHP implements a very basic set of process control statements, which behave in the usual fashion. They are the familiar If statement, While loop, For loop, Case block (switch), the Foreach loop, and the break and continue.
The syntax is a little different. Where I have {code} below it means any line or lines of PHP code including the curly brackets. Each line must end with a semicolon ;. The brackets group the lines into a unit. For example, the {code} after the If statement or While statement can be either a single line with a semicolon without brackets, or one or more lines with brackets. The continue command which skips the rest of the current loop iteration, has an added feature: add a number to skip the rest of nested loops. For example, continue 2 skips the current iteration and also a loop enclosing the current loop.
if ($A==”value”) {code} If statement to execute the bracketed code. Use === three equals to test boolean values
while ($A==”value”){code} While loop executing the bracketed code
for ($i = 1; $i $V) if you need to access the array element and not just the value,
{code} $key is a variable for the element of $A array, $V is the value
include ‘scriptfilename’ inserts the contents of a file into code
The Arithmetic, logical and boolean operators are exactly as one would expect and listed below to round out this reference:
Arithmetic
-$a negative of $a
$a + $b add
$a * $b multiply
$a / $b divide (float value unless both are integer)
$a % $b remainder
Operators
&& logical AND bitwise and is &
|| logical OR bitwise or is |
! logical NOT bitwise not is ~
xor logical XOR bitwise xor is ^
Bill Hamilton is a former Database Administrator for United News and Media, and VNU inc. He currently operates several php/mysql driven websites including www.mysticgemcreations.com