2

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?

3 Answers 3

3

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

3

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.

2

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.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.