faqts : Computers : Programming : Languages : PHP : kms : General

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

51 of 117 people (44%) answered Yes
Recently 5 of 10 people (50%) answered Yes

Entry

How are strings constructed in PHP?
What quotes can I use to make strings in PHP?
Can I include a variable reference in a string?
Can I reference array variables inside a quoted string?
Why won't multi-dimensional array references work in a quoted string?
Why does '$foo' not print the value of the variable $foo?
How do I include the value of a variable or array element in a string?
Will php in near future support complex quote like statements(like in perl), i.e. qq(This is the str

Apr 11th, 2002 04:48
Mike Ford, Nathan Wallace, Aman Patel,


Basically a string in PHP is anything between matching quotes:

    'I am a string in single quotes'

    "I am a string in double quotes"

You can include quotes inside the string that do not match the quote
that started the string.  So you can include single quotes in a string
that was started (and finishes) with double quotes.  So, these will
work:

    "I am a 'single quote string' inside a double quote string"

    'I am a "double quote string" inside a single quote string'

PHP thinks it has reached the end of the string as soon as it sees that
matching quote.  So the example

    "Why doesn't "this" work?"

is actually seen by PHP as being

    "Why doesn't " - a string

    this           - crap letters that PHP doesn't recognize

    " work?"       - a string

It is also helpful to know that you can include a quote character of the
same type inside a string if you escape it.  The escape tells PHP that
the next character should just be used as part of the string.  For
example:

    "Why doesn't \"this\" work?"

is a single string that contains double quote characters.

Double quote strings and single quote strings are treated differently by
PHP.  Double quote strings are interpreted by PHP while single quotes
strings are treated exactly as is.  For example:

    $foo = 2;
    echo "foo is $foo";     // this prints:  foo is 2
    echo 'foo is $foo';     // this prints:  foo is $foo
    echo "foo is $foo\n";   // this prints:  foo is 2 (with a <CR>)
    echo 'foo is $foo\n';   // this prints:  foo is $foo\n

That's right, even backslashes aren't expanded inside single quotes,
either except for \\ and \'.

So, you should use double quotes when you need variable expansion inside
the string.  Otherwise, feel free to use single quotes.

Note that, if you wish to use $-substitution for anything other than a
simple variable name, you should use {} to enclose it; for example:

    "... ${a[$i]} ..."

or:

    "... ${a[$i][$j]} ..."

You can also use {} to enclose a simple variable name if you need to
separate it from adjacent characters; for example, this will work:

    echo "Today is the ${day}th day of the month";

but this won't:

    echo "Today is the $dayth day of the month";

The last thing to know about strings is that you can join them together
easily using the concatenate operator (.).  Here is an example:

    $var = "crappy ending";
    echo "this " . 'is an' . ' example ' . "with a " . $variable;