I have a variable that i want to add doc- to it so it reads out doc-$user_role like this for advanced custom fields in wordpress.
i know how to do this with echo finding it difficult in this format some help please 🙂
<?php
$user_roles = $current_user->roles;
$user_role = array_shift($user_roles);
if(get_field('doc-$user_role')): ?>
<ul>
<?php while(has_sub_field('doc-$user_role')): ?>
<li>
<h3><?php the_sub_field('doc_name'); ?></h3>
<p><?php the_sub_field('doc_description'); ?></p>
<a href="<?php the_sub_field('download_doc'); ?>" target="_blank"><img src="<?php bloginfo('template_directory'); ?>/images/download.png" alt="download_button"></a>
</li>
<?php endwhile; ?>
</ul>
<?php endif; ?>
There is difference between “” and ” literals. When you put variable in “” it will be parsed by php because it’s string. When you put it into ” it’s treat as char array and variable will not be parsed that is why you need to use “” literal or sprintf() or you can concate two strings using dot operator.
so your options are:
sprintf is useful when you have long strings and you don’t want mess much with code.
http://php.net/manual/en/function.sprintf.php
here you have full explanation of how string works in PHP
http://php.net/manual/en/language.types.string.php
use the concatenation operator:
would be correct, as in strings surrounded by single quotes variables are not parsed. Only in strings with double quotes.