Introduction to PHP

Strings in PHP are a sequence of characters, such as "We hold these truths to be self evident," or "Once upon a time," or even "111211211." When you read data from a file or output it to a web browser, your data is represented as strings.

Individual characters in strings can be referenced with array subscript style notation, as in C.
The first character in the string is at index 0. For example:

$neighbor = 'Hilda';
print $neighbor[3];

However, PHP strings differ from C strings in that they are binary-safe (i.e., they can contain null bytes) and can grow and shrink on demand. Their size is limited only by the amount of memory that is available.
You can initialize strings three ways, similar in form and behavior to Perl and the Unix shell:
with single quotes, with double quotes, and with the "here document" (heredoc) format. With single-quoted strings, the only special characters you need to escape inside a string are backslash and the single quote itself:

print 'I have gone to the store.';
print 'I\'ve gone to the store.';
print 'Would you pay $1.75 for 8 ounces of tap water?';
print 'In double-quoted strings, newline is represented by \n';
I have gone to the store.
I've gone to the store.
Would you pay $1.75 for 8 ounces of tap water?
In double-quoted strings, newline is represented by \n

Because PHP doesn't check for variable interpolation or almost any escape sequences in single-quoted strings, defining strings this way is straightforward and fast.

Posted on: 30/11/2009

If you want to leave a comment please Login or Register
IP Board Greek language file......
Distribute Smarty......
Processing a string a character......
Introduction to PHP......