Changes

Jump to: navigation, search

Understanding PHP Variable Types

1,758 bytes added, 19:40, 25 May 2007
The PHP String Variable
== The PHP String Variable ==
 
The string variable type is used to hold strings of characters such as words and sentences. In addition to providing mechanisms for creating and changing entire string variable values, PHP allows you to extract and change parts of a string value.
 
A string can be assigned to variable either surrounding it in single qoutes (') or double qoutes ("). If your string itself contains either single or double qoutes you should use the opposite to wrap the string:
 
<pre>
<?php
 
myString = "A string of text";
 
myString2 = 'Another string of text';
 
myString3 = "This string contains 'single quotes'";
 
myString4 = 'This string contains "double quotes"';
 
?>
</pre>
 
You can also ''escape'' quotes in your string by preceeding them with a backslash (\), especially useful if your string contains both single and double quotes of its own that would otherwise confuse the PHP pre-processor:
 
<pre>
<?php
 
myString3 = 'This string contains \'single quotes\'';
 
myString4 = "This string contains \"double quotes\" and \'single quotes\'";
 
?>
</pre>
 
Double quoted strings also allow the insertion of sepcial ''control sequences'' that are interpreted to have special meaning for the PHP pre-processor (such as a tab or new line). The following table outlines the varipous control sequences and their respective descriptions:
 
<table border="1" cellspacing="0">
<th>Control Sequence<th>Description</th>
<tr>
<td>\n<td>New line</td>
<tr>
<td>\r<td>Carriage Return</td>
<tr>
<td>\t<td>Tab</td>
<tr>
<td>\\<td>Backslash Character</td>
<tr>
<td>\"<td>Double quotation mark</td>
<tr>
<td>\$<td>Dollar sign (prevent text from being treated as a variable name</td>
<tr>
<td>\034<td>Octal ASCII value</td>
<tr>
<td>\x0C<td>Hexadecimal ASCII Value</td>
<tr>
<td><td>
</table>
== Converting Between PHP Strings, Numbers and Floats ==

Navigation menu