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 Answers 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") ) {
Sign up to request clarification or add additional context in comments.

Comments

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.

2 Comments

There is no comma operator in PHP. You may be thinking of the echo statement, which can take multiple arguments separated with commas.
Thank you IMSoP that is what I meant.
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.

Comments

1

Probably quoting issue, so try this:

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

2 Comments

"Probably"? The question has invalid syntax, yours is (one version of) the correct syntax. You're not wrong, but have some confidence in your answer!
Thanks, I guess I've convinced myself not to trust me :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.