weixin_33720078 2014-07-13 09:07 采纳率: 0%
浏览 23

退货被完全忽略[重复]

This question already has answers here:
                </div>
            </div>
                    <div class="grid--cell mb0 mt4">
                        <a href="/questions/10107144/difference-between-php-echo-and-return-in-terms-of-a-jquery-ajax-call" dir="ltr">Difference between php echo and return in terms of a jQuery ajax call [closed]</a>
                            <span class="question-originals-answer-count">
                                (4 answers)
                            </span>
                    </div>
            <div class="grid--cell mb0 mt8">Closed <span title="2014-07-13 09:13:44Z" class="relativetime">5 years ago</span>.</div>
        </div>
    </aside>

I have the following code

<?php   
    $token = '';
    if(!empty($_POST['token'])) $token = $_POST['token'];

    $url = '';
    if(!empty($_REQUEST['url'])) $url = Sanitize($_REQUEST['url']);

    if(!filter_var($url, FILTER_VALIDATE_URL)) return 'erro1';
?>

This page is supposed to be executed by AJAX so I'm coding error codes, now if the URL is not valid, I expect it to return erro1

The problem is, it never returns erro1. I tried dumping the result of the filter_var and it was false as expected! The page simply executes and return nothing even though the URL is not valid, but if I switch return with an echo, it works.

Why is that? Why is echo working but not return?

</div>
  • 写回答

2条回答 默认 最新

  • weixin_33736649 2014-07-13 09:08
    关注

    You need to use echo instead of return. Your Web server executes the PHP code and serve its output, which you produce using echo and other printing functions.

    Outside of a function, return interrupts the script.

    评论

报告相同问题?