DonJuma
DonJuma

Reputation: 2094

Print $name value in php

I am trying to print out the value of $name

yet it keeps printing out $name instead of the value here is the line:

header('Content-Disposition: attachment; filename="$name"');

Upvotes: 1

Views: 555

Answers (7)

lfx
lfx

Reputation: 1391

header('Content-Disposition: attachment; filename='.$name);

Upvotes: -1

Tarik
Tarik

Reputation: 81721

Why don't you just use " instead of ' like :

header("Content-Disposition: attachment; filename=$name");

Upvotes: 0

RobertPitt
RobertPitt

Reputation: 57268

I think this is what your looking for

header('Content-Disposition: attachment; filename="'.$name.'"');

This would give a header like so:

Content-Disposition: attachment; filename="some_file_name.ext"

Hope this helps

Another way to do this is using sprintf

$header = sprintf('Content-Disposition: attachment; filename="%s"',$name);
header($header);

The sprintf is not really needed but there to show you how the variable would be placed into the header string

Upvotes: 2

Ruel
Ruel

Reputation: 15780

When dealing with single-quoted strings, always concatenate variables.

header('Content-Disposition: attachment; filename="' . $name . '"');

Upvotes: 1

Ty W
Ty W

Reputation: 6814

PHP will only evaluate variables within double-quoted strings, not single quoted strings like you've used in your example.

Try this instead (if you need to output double quotes within a double-quoted string, you need to escape them with a backslash, otherwise PHP will treat them as the end-of-string delimiter):

header("Content-Disposition: attachment; filename=\"$name\"");

Upvotes: 4

Victor Nicollet
Victor Nicollet

Reputation: 24577

String interpolation does not happen between single quotes.

$name = "test";
echo "Value: $name"; // Value: test
echo 'Value: $name'; // Value: $name

More details here.

Upvotes: 2

CristiC
CristiC

Reputation: 22698

Try like this:

header('Content-Disposition: attachment; filename='.$name);

Upvotes: 0

Related Questions