如何将全局脚本转换为函数?

时间:2015-10-12 17:52:06

标签: php global-variables fatal-error

嗨,伙计们,当我尝试将现有的脚本转换为简单的函数时,我遇到了一个小问题。我将保留下面的代码。

PHP:

function service_check($postcode) {
    $op_postcodes = array(... "DG10","DG3","DG4" ...);

    if(isset(htmlentities($postcode))) {
        $postcode_checker = trim(strtoupper(htmlentities($postcode)));
        $trim_postcode = trim(substr(htmlentities($postcode_checker, 0, -3)));

        if(empty($postcode_checker)) {
            $error = "We require your postcode to check our service in your area.";
        } else if(!valid_postcode($postcode_checker)) {
            $otp = "The postcode you entered is invalid.";
        } else if(!in_array($trim_postcode, $op_postcodes)) {
            $otp = "Sorry, but we don't provide our service's in your area, just yet.";
        } else {
            $otp = "Great news! We're in your area and you are eligible to order our services!";
            $_SESSION['customer_postcode'] = $postcode_checker;
        }
    } else {
        $otp = "To get started please enter your postcode.";
    }
}

我目前对该功能的使用是<?php service_check($_POST['service_check']); ?>

我的错误是:

  

致命错误:无法在/home/domain.com/public_html/controller/application/functions/locale中对函数调用的结果使用isset()(可以使用“null!== func()”)第27行的.SM.php

1 个答案:

答案 0 :(得分:1)

更改此

 if(isset(htmlentities($postcode))) {

到这个

$pc = htmlentities($postcode);
if(isset($pc)) {

如果您有时间阅读此内容 - http://php.net/manual/en/function.isset.php http://php.net/manual/en/function.htmlentities.php

由于您的问题未完成,我现在假设编辑。更好的方法是在if条件上使用!empty()而不是isset()。

更好的是,从你的if块中调用htmlentities方法调用,然后在实际需要时使用html实体。