0

I am not exactly sure how to word this in the form of a short question...the above called up many different answers that were not related, but I apologize if this has been asked before.

I am trying to make a variable name itself based the value of another.

I could simply create if statements for each one, but if I were able to declare the variable the way I want, it would save me about 20-30 lines of code, and make future additions much easier.

Here is a better description. This is the code that I am using at the moment. It is within a shortcode function for wordpress, to create a button based on user-given parameters.

extract(shortcode_atts(array(
      'size' => 'medium',
      'link' => '#',
      'text' => ''
    ), $atts));    
$large_button_img = of_get_option('large_button_arrow_upload');
        $button_pos = of_get_option('button_image_position');
        if($button_pos == 'right' && !empty($button_img)){
            $the_button = "<a href='" . $link . "' class='" . $size . "_button custom_button" . $button_pos ."'>" . $text . "<img src='" . $large_button_img . "' id='button_img' alt='button image' /></a>"; 
        }elseif($button_pos == 'left' && !empty($button_img)){
            $the_button = "<a href='" . $link . "' class='" . $size . "_button custom_button" . $button_pos ."'><img src='" . $large_button_img . "' id='button_img' alt='button image' />" . $text . "</a>"; 
        }else
        {
            $the_button = "<a href='" . $link . "' class='" . $size . "_button custom_button'>" . $text . "</a>"; 
        }
        return $the_button;

In the above: The function pulls the value of "size", "link" and "text" from the user given shortcode and generates a button. It sets the class based on the size... At the moment I have it to where the user can set different images for a large, medium and small button.

Question: IS IT POSSIBLE to return the name of the image source based on what size is set to. soooo basically the equivalent of?

img src = '" . $($size)_button_img . "'

Where it places value of $size in the name of the variable that it pulls to tell it which image source to pull afterwards? (so the proper equivalent of the above would produce something like

img src= '" . $large_button_img ."'

or, if the user has medium selected

img src= '" . $medium_button_img . "'

If possible this save myself having to write if-statements for every possible option (basically copy the set of if-ifelse-else from above, everytime I have a new size setting available)...which eventually may become more of an efficiency issue.

Thanks in advance for any help that can be given :)

ALSO Please ignore any syntax errors in the above set of code...I am working on this as you read this, so more than likely, if you see something wrong, it has already been fixed.

4
  • 3
    ${"{$size}_button_img"} should do it Commented Dec 11, 2013 at 14:58
  • Very Nice @onetrickpony That worked perfectly. Thank you very much. Commented Dec 11, 2013 at 15:10
  • Please remove the wordpress tag. The fact that you ran into this problem while working with wordpress, doesn't make it a wordpress question. Commented Dec 11, 2013 at 15:14
  • Nic. Not sure why the wordpress tag was there. I had only added "php" and "variables"...did not realize it automatically added wordpress. Apologies. Commented Dec 11, 2013 at 15:15

3 Answers 3

2

You can simple use function for your logic:

function somethingWithImage($img, $pos) {
    if ($pos == 'right' && !empty($button_img)) {
        $the_button = "<a href='" . $link . "' class='" . $size . "_button custom_button" . $pos ."'>" . $text . "<img src='" . $img . "' id='button_img' alt='button image' /></a>"; 
    } elseif ($pos == 'left' && !empty($button_img)) {
        $the_button = "<a href='" . $link . "' class='" . $size . "_button custom_button" . $pos ."'><img src='" . $img . "' id='button_img' alt='button image' />" . $text . "</a>"; 
    } else {
        $the_button = "<a href='" . $link . "' class='" . $size . "_button custom_button'>" . $text . "</a>"; 
    }

    return $the_button;
}

And just call it whenever you want with any arguments.

Sign up to request clarification or add additional context in comments.

1 Comment

I may give this a try, that way I can put the button image into the shortcode...instead of (like a dummy) I have it in the theme options panel -- so the user would have to set the image and position in the panel, and then go create the shortcode. Using your example, I could definitely make the shortcode more flexible and easy to use :P Thank you.
0

What you are looking for, are variable variable names.

Short example:

$hello_text = 'Hello World!';
$bye_text = 'Goodbye World!';

$varname = 'hello';

echo ${$varname}_text; // Hello World!

// Is the same as:
echo $hello_text;      // Hello World!

Comments

0

As I mentioned, you can use interpolation to create variable variables:

$src = ${"{$size}_button_img"};

But may I suggest using arrays instead? You get cleaner and understandable code:

$sizes = array(
 'large'  => ...,
 'medium' => ...,
);

if(isset($sizes[$size]))
  $src = $sizes[$size];

Comments

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.