简体   繁体   English

在Codeigniter中创建我自己的帮助器

[英]Creating my own Helper in Codeigniter

Just learning PHP and Codeigniter at the moment and I'm stuck on a trivial one. 目前,仅学习PHP和Codeigniter,而我却陷入了琐碎的困境。 I created a helper to calculate a percentage of two numbers: 我创建了一个助手来计算两个数字的百分比:

    # Below is the Helper

    function percentage($num1 = NULL, $num2 = NULL)
    {           
        $res = $num1/$num2*100;

        return $res;
    }

Now I call the helper function in the controller using the below code: 现在,我使用以下代码在控制器中调用辅助函数:

    public function tester()
    {
        $this->load->helper('sums_helper');

        $num1 = 36;
        $num2 = 137;

        percentage($num1, $num2);

        // Need to print the result here.
        // Tried: $percentage->$res;

    }

I call the function and pass over the two required variables however cannot print the result. 我调用该函数并传递了两个必需的变量,但是无法打印结果。 Any suggestions would be greatfuly appreciated. 任何建议将不胜感激。

Thank you in advance!! 先感谢您!! Niall 尼尔

You need to assign function return value to the variable: 您需要将函数返回值分配给变量:

$result = percentage($num1, $num2);

so you can print it: 因此您可以打印它:

print_r($result);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM