dongya2030 2018-05-30 16:48
浏览 37

可以使用echo和return函数

I just want to know if is ok to use echo And return instead one function

The below code is one of my function

if(!function_exists("un_block_ad_ajax")) {

    function un_block_ad_ajax() {
        global $db;
        $is_admin = is_admin();
        $check_user = true;
        $request = $_POST["data"]["request"] ?? "";
        if(!in_array($request,["block","publish"])) {
            return false;
        }
        $ad_id = $_POST["data"]["ad_id"] ?? "";
        $ad_id = (int) $ad_id;
        if(empty($ad_id)) {
            return false;
        }

        if($is_admin === true) {
            $check_user = false;
        }

        if($check_user === true) {
            if(is_login() === false) {
                return false;
            }
            $get_ad_info = $db->select("SELECT ad_publisher FROM ads WHERE id = ? LIMIT 1",[$ad_id],["%i"]);
            $get_ad_info = $get_ad_info[0];
            if($get_ad_info["ad_publisher"] != is_login()->id) {
                // User-undesired activity
                return false;
            }
        }

        $update_status = $db->update("ads",["ad_status" => $request],["%s"],["id" => $ad_id],["%i"]);
        if($update_status) {
            echo json_encode(["success" => true]);
        }
        return true;
    }

}

I call function from one file

$method = $_POST["method"] ?? : "";
if(!empty($method)) {
    $allowed_methods = array(
        "signup_ajax",
        "signin_ajax",
        "add_ad_ajax",
        "upload_ad_image",
        "send_message",
        "get_message",
        "get_cnv",
        "del_add_to_fav",
        "delete_ajax",
        "un_block_ad_ajax",
    );
    if(in_array($method,$allowed_methods)) {
        if(call_user_func($method) === false) {
            exit(http_response_code("400"));
        }
    }else{
        exit(http_response_code("404"));
    }
}else{
    exit(http_response_code("404"));
}

So as you see in code I need to use return and echo

  • 写回答

1条回答 默认 最新

  • dongwo5110 2018-05-30 16:58
    关注

    Technically, you can do as you may need, however, I advise you maintain a standard, as in:

    Let's say you want to return true to an operation now, but tomorrow you need true and a wholesome set of data of, for example, what was updated.

    You may want to build a generic response for your application, where you can use the same function (or logic) to read every response you may eventually get.

    A response containing of:

    {
     'success' => true,
     'message' => [...],
     'visibleToHumans' => true,
     'content' => [...]
    }
    

    You can have an UI that handles the response of your request, that can read the content based off success.

    Extra: I'd like to advise to look up PDOStatements, to secure your queries.

    http://php.net/manual/en/class.pdostatement.php

    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题