dt3674
2014-02-23 16:06
浏览 35
已采纳

PHP:访问存储在函数中的变量

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

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dozr162106 2014-02-23 16:16
    已采纳

    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>
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • dpauf28808 2014-02-23 16:08

    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.

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题