V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
shyy228
V2EX  ›  PHP

新手请教关于 return 的使用方法

  •  
  •   shyy228 · 2016-04-07 09:01:32 +08:00 · 2567 次点击
    这是一个创建于 2913 天前的主题,其中的信息可能已经有所发展或是发生改变。

    $a=3;

    if($a>1){
        return true;
    }
    
    echo "hello";
    

    我并没有用 else,我知道 if , else 那个成立就执行那个语句块。但是我 return 以后,后面还有一句输出,为什么不输出了?请大家帮忙解释下 return 这个关键字的用法。

    $a=3;
    
    if($a>1){
        echo $a;
    }
    
    echo "hello";
    

    这里不 return ,结果就输出了 3hello

    小弟刚自学 php 请大家帮忙看看,谢谢!

    8 条回复    2016-04-07 10:00:51 +08:00
    shiji
        1
    shiji  
       2016-04-07 09:13:39 +08:00
    和 C 一样,在一个 function 里面, return 了之后,后面的都不会被执行。

    return returns program control to the calling module. Execution resumes at the expression following the called module's invocation.

    If called from within a function, the return statement immediately ends execution of the current function, and returns its argument as the value of the function call. return also ends the execution of an eval() statement or script file.

    If called from the global scope, then execution of the current script file is ended. If the current script file was included or required, then control is passed back to the calling file. Furthermore, if the current script file was included, then the value given to return will be returned as the value of the include call. If return is called from within the main script file, then script execution ends. If the current script file was named by the auto_prepend_file or auto_append_file configuration options in php.ini, then that script file's execution is ended.
    kchum
        2
    kchum  
       2016-04-07 09:16:07 +08:00 via iPhone
    Return 了,就不会执行后面的代码。
    Echo 只是输出字符串,没有返回,该运行的代码还运行。
    shiji
        3
    shiji  
       2016-04-07 09:20:18 +08:00
    如果在一个函数中调用 return 语句,将立即结束此函数的执行并将它的参数作为函数的值返回。 return 也会终止 eval() 语句或者脚本文件的执行。

    如果在全局范围中调用,则当前脚本文件中止运行。如果当前脚本文件是被 include 的或者 require 的,则控制交回调用文件。此外,如果当前脚本是被 include 的,则 return 的值会被当作 include 调用的返回值。如果在主脚本文件中调用 return ,则脚本中止运行。如果当前脚本文件是在 php.ini 中的配置选项 auto_prepend_file 或者 auto_append_file 所指定的,则此脚本文件中止运行。

    更多信息见返回值。

    Note: 注意既然 return 是语言结构而不是函数,因此其参数没有必要用括号将其括起来。通常都不用括号,实际上也应该不用,这样可以降低 PHP 的负担。
    Note: 如果没有提供参数,则一定不能用括号,此时返回 NULL 。如果调用 return 时加上了括号却又没有参数会导致解析错误。
    Note: 当用引用返回值时永远不要使用括号,这样行不通。只能通过引用返回变量,而不是语句的结果。如果使用 return ($a); 时其实不是返回一个变量,而是表达式 ($a) 的值(当然,此时该值也正是 $a 的值)。
    Ellen
        4
    Ellen  
       2016-04-07 09:20:28 +08:00
    return 之后的语句都不会被执行,从一个地方调用函数,就跳到这个函数里面去执行了,当执行到 return 语句之后将返回原先调用的位置继续执行接下来的语句。可以把它想象成一个执行权,调用函数前执行权在自己手里,调用函数就把执行权给函数,执行到 return 之后执行权交还给我。所以它在 return 之后的语句就不没有执行权,因此没有输出。
    335774855
        5
    335774855  
       2016-04-07 09:37:03 +08:00 via Android
    执行 return 后函数就结束了,不管 return 后面有多少语句都不会执行
    yvanhe
        6
    yvanhe  
       2016-04-07 09:48:17 +08:00
    如果你用 Java , return 执行之后,也可以运行 finally 里的语句。细软跑
    zqhong
        7
    zqhong  
       2016-04-07 09:51:15 +08:00
    http://php.net/manual/zh/function.return.php

    这是我的摘抄:
    如果在一个函数中调用 return 语句,将立即结束此函数的执行并将它的参数作为函数的值返回。 return 也会终止 eval() 语句或者脚本文件的执行。

    例子 1 :
    ```
    <?php
    function return_num() {
    return 100;
    }

    // 输出 100
    print return_num();
    ```

    =============

    如果在全局范围中调用,则当前脚本文件中止运行。

    例子 2 :
    ```
    <?php
    $a = 3;

    if ($a > 1) {
    return;
    }

    // 没有任何输出,脚本在上面 return 后便结束。这个和楼主所说的情况类似
    echo "hello world";
    ```

    =============

    如果当前脚本文件是被 include 的或者 require 的,则控制交回调用文件。此外,如果当前脚本是被 include 的,则 return 的值会被当作 include 调用的返回值。

    例子 3 :
    ```
    // include.php
    <?php
    return 100;

    ```

    ```
    <?php
    // main.php
    print include('include.php');

    print require('include.php');

    // 输出结果: 100100
    ```

    这里执行 `$ php main.php`,会输出: 100100

    =============


    如果在主脚本文件中调用 return ,则脚本中止运行。

    例子 4 :
    ```
    // include.php
    <?php
    return 100;

    ```

    ```
    <?php
    // main.php
    return; // 主脚本文件在调用 return 之后,就中止运行了。
    print include('include.php');

    ```

    这里同样执行 `$ php main.php`,不会有任何输出。

    =============


    如果当前脚本文件是在 php.ini 中的配置选项 auto_prepend_file 或者 auto_append_file 所指定的,则此脚本文件中止运行。

    例子 5 :
    打开 php.ini ,作如下的修改: auto_prepend_file="/path/to/include.php"。(修改成你 include.php 文件的绝对路径就好)

    ```
    // include.php
    <?php
    print "include";
    return;

    ```

    ```
    // main.php
    <?php
    print 'hello world';

    ```

    同样执行 `$ php main.php`,这里的输出结果是: includehello world 。


    =============

    其他:
    1. `$ php main.php`,这里的 $ 是命令提示符。
    2. 遇到问题比较好的办法是看官方手册,中文手册可能内容不全。适当的时候切换到英文版。
    shyy228
        8
    shyy228  
    OP
       2016-04-07 10:00:51 +08:00
    @shiji
    @kchum
    @Ellen
    @yvanhe
    @zqhong
    @335774855
    谢谢各位的详细,解答,我明白了!
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5361 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 07:31 · PVG 15:31 · LAX 00:31 · JFK 03:31
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.