0

I can't figure out how to use PHP variables

if( $xml = file_get_contents( $user '/docs.xml') ) {

It says unexpected ''/docs.xml'' (T_CONSTANT_ENCAPSED_STRING)

I've researched and can't find anything on adding variables to get_file_contents

Please Help

4

4 回答 4

4

You need to combine the variable and the string literal via concatenation (Wikipedia):

if( $xml = file_get_contents( $user . '/docs.xml') ) {

Also, if you use double quoted strings, you can place the variable inside of the string and have its value expanded:

if( $xml = file_get_contents("$user/docs.xml") ) {
于 2013-09-07T00:07:55.177 回答
4
if ($xml = file_get_contents( $user . '/docs.xml') ) {
}

Looks like you are trying to concatenate, or combine the $user variable with the literal string '/docs.xml'.

In PHP you combine strings with the period . operator.

$string = "Testing" . " to see" . " if this really works";
echo $string;
// Outputs: Testing to see if this really works.
于 2013-09-07T00:09:24.120 回答
2

There are many ways to concatenate strings in PHP. You should try this.

if( $xml = file_get_contents( $user . "/docs.xml") ) {

Read more on the documentation.

于 2013-09-07T00:08:09.457 回答
1

Probably quoting issue, so try this:

if( $xml = file_get_contents("$user/docs.xml") ) {
于 2013-09-07T00:07:55.567 回答