0

I have a set of PHP variables (that start with $cta). I use a PHP 'If' test, to change the values of these variables depending on the values of other variables.

I use these variables in several locations in my PHP file (and wrap the variables in different HTML code depending on the location), so I want to store the 'If' testing code in a function.

That way, my code will be more efficient, as the 'If' test will be in one place.

Here is my function:

function calltoaction {

 if ($cta_settings == 'cta_main') {
                $cta_prompt = 'We are ready for your call';
                $cta_button = 'Contact Us';
            }
($cta_settings == 'cta_secondary') {
                $cta_prompt = 'Call us for fast professional service';
                $cta_button = 'Call Us';
            }
}

Now that I have the function, how do I access the $cta variables inside of it?

E.g. The following doesn't work:

<?php 
calltoaction();
print '<p>' . $cta_prompt . '</p>;
print '<span>' . $cta_button . '</span>;
?>

(The examples presented above are a cut down version, my full code is a bit more complex).

4
  • declare $cta_prompt as global variable Commented Feb 23, 2014 at 16:07
  • global $cta_prompt, $cta_button; (inside the function) Commented Feb 23, 2014 at 16:08
  • 4
    Don't use globals, both of the preceding comments are offering very bad advice. Commented Feb 23, 2014 at 16:09
  • or (better) have your fonction take your $cta_settings as input parameter and return a pair of values for $cta_prompt and $cta_button Commented Feb 23, 2014 at 16:09

2 Answers 2

5

Now that I have the function, how do I access the $cta variables inside of it?

You don't. That isn't how functions are meant to work. You also have a second bug in that $cta_settings won't be set inside your function, as you haven't passed it in or declared it as a global, meaning your prompt/button variables are never set anyways.

Your functions should accept inputs, and return outputs. If you want to communicate a value out of a function for use elsewhere, you should return it. If you need to return a complex result, use an array or an object.

You should not use global variables for this. Using global variables breaks the encapsulation the function is meant to provide.

In your case, I would use something like this:

function calltoaction($settings) {
  if ($settings == 'cta_main') {
    return array('We are ready for your call', 'Contact Us');
  } else if ($settings == 'cta_secondary') {
    return array('Call us for fast professional service', 'Call Us');
  }
}

list($prompt, $button) = calltoaction($cta_settings);

Note that the function accepts an argument ($settings) instead of referencing some global variable, and returns two values for use in the calling code. It's up to the calling code to decide on variable names for its local variables, as it should be - the function shouldn't be injecting variables into the global scope.

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

2 Comments

How do I access the prompt and button parts individually, so I can wrap them in HTML (I can't include the HTML inside the function, as it is different each time the function is used).
@big_smile They're individually available as variables called $prompt and $button after the above code. That is what list does, destructures an array into a set of variables.
1

Perhaps you wanted to do something like this:

<?php
function callToAction($cta_settings)
{

    if ($cta_settings == 'cta_main') {
        $cta_prompt = 'We are ready for your call';
        $cta_button = 'Contact Us';
    } elseif ($cta_settings == 'cta_secondary') {
        $cta_prompt = 'Call us for fast professional service';
        $cta_button = 'Call Us';
    } else {
        $cta_prompt = 'Call us for fast professional service';
        $cta_button = 'Call Us';
    }
    return ["cta_prompt" => $cta_prompt, "cta_button" => $cta_button];
}

$cta1 = callToAction('cta_main');
?>

<p><?php echo $cta1['cta_prompt']; ?></p>
<span><?php echo $cta1['cta_button']; ?></span>

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.