TheDudeAbides
TheDudeAbides

Reputation: 165

php variable in file path string

I have the following php code:

stream_context_set_option($ctx, 'ssl', 'local_cert', 'file.pem');  

and I'm trying to do this:

stream_context_set_option($ctx, 'ssl', 'local_cert', '$var.pem');  

but it is not accessing the .pem file correctly when I do it that way.

How do I add a variable and append .pem to the end of it in this scenario?

Upvotes: 2

Views: 190

Answers (3)

Stuart Wagner
Stuart Wagner

Reputation: 2067

To expand on @Sourabh's answer, you can also use interpolation with explicit notation, like so:

stream_context_set_option($ctx, 'ssl', 'local_cert', "{$var}.pem");

Interpolation is slightly slower, but it is an option.

Upvotes: 2

Gene
Gene

Reputation: 179

Use double quotes instead of single quote. Single quote doesn't process php variable inside but that double quotes does.

stream_context_set_option($ctx, 'ssl', 'local_cert', "$var.pem");

Or you can do something like this:

stream_context_set_option($ctx, 'ssl', 'local_cert', $var.'.pem');

It's string concatenation if you really love that single quote.

Upvotes: 3

Sourabh Kumar Sharma
Sourabh Kumar Sharma

Reputation: 2827

use the below code:

stream_context_set_option($ctx, 'ssl', 'local_cert', $var.'.pem');  

what ever you specify between the single open inverted commas will be appended as it is to the $var variable. It can 'pem' or '.pem' or any other string.

Note: What ever that you give in the single inverted commas is always considered a string in PHP, even '$var' is considered as a string not a PHP variabe

Upvotes: 3

Related Questions