big_smile
big_smile

Reputation: 1523

PHP: Accessing variables stored in a function

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).

Upvotes: 0

Views: 71

Answers (2)

Tomanow
Tomanow

Reputation: 7357

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>

Upvotes: 1

user229044
user229044

Reputation: 239230

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.

Upvotes: 5

Related Questions