|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #77776 if inline goto
Submitted: 2019-03-21 03:11 UTC Modified: 2019-03-22 01:01 UTC
From: xxalfa at gmail dot com Assigned:
Status: Duplicate Package: *General Issues
PHP Version: Irrelevant OS:
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
Block user comment
Status: Assign to:
Bug Type:
From: xxalfa at gmail dot com
New email:
PHP Version: OS:

Further comment on this bug is unnecessary.


 [2019-03-21 03:11 UTC] xxalfa at gmail dot com
I don't understand why "goto" is not treated like "require_once"?

Test script:

    // HEAD

    declare( strict_types = 1 );

    header( 'Content-Type:text/plain' );

    error_reporting( E_ALL );

    ini_set( 'display_errors', '1' );

    ini_set( 'html_errors', '0' );

    define( 'CORE_DIR', dirname( __FILE__ ) . DIRECTORY_SEPARATOR );

    isset( $argv ) or trigger_error( 'This is a command terminal application.', E_USER_ERROR );

    echo __FILE__ . ' [PID:' . getmypid() . '][PHP:' . phpversion() . ']' . PHP_EOL . PHP_EOL;

    function process_recording( $process_message ) { echo 'event ' . $process_message . PHP_EOL; }


    $test = 'foo';

    $file_path = CORE_DIR . 'test.php'; // file content: <?php $test = 'bar';

    file_exists( $file_path ) and process_recording( 'require_once ' . $file_path ) . ( require_once $file_path ) or process_recording( 'file_not_found ' . $file_path );

    echo $test . PHP_EOL;


    $action = 2;

    // Parse error: syntax error, unexpected 'goto' (T_GOTO)

    // $action === 1 and process_recording( 'goto_action_1' ) . goto goto_action_1;
    // $action === 2 and process_recording( 'goto_action_2' ) . goto goto_action_2;

    // $action === 1 and process_recording( 'goto_action_1' ) . ( goto goto_action_1 );
    // $action === 2 and process_recording( 'goto_action_2' ) . ( goto goto_action_2 );

    // $action === 1 and process_recording( 'goto_action_1' ) . goto( 'goto_action_1' );
    // $action === 2 and process_recording( 'goto_action_2' ) . goto( 'goto_action_2' );


    if ( $action === 1 ): process_recording( 'goto_action_1' ); goto goto_action_1; endif;
    if ( $action === 2 ): process_recording( 'goto_action_2' ); goto goto_action_2; endif;


    goto_action_1: echo 'action_1' . PHP_EOL . exit;

    goto_action_2: echo 'action_2' . PHP_EOL . exit;

    goto_end_of_file: echo 'end_of_file' . PHP_EOL . exit;



Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2019-03-21 03:18 UTC]
-Status: Open +Status: Not a bug
 [2019-03-21 03:18 UTC]
For the same reason you can't do it with a foreach or return: they are statements, not expressions.
 [2019-03-21 06:07 UTC] xxalfa at gmail dot com
That's right, with "foreach" and "return" it makes no sense. "require_once" returns a 0 or 1. Why does not "goto" do the same? Can I jump = 1, I can not jump = 0.
 [2019-03-21 22:35 UTC]
-Status: Not a bug +Status: Duplicate
 [2019-03-21 22:35 UTC]
Duplicate of request #72589.
 [2019-03-21 22:38 UTC]
If you want to continue the RFC process on your other request, read the How To you were linked that tells you exactly what you need to do.

If you need an explanation of the difference between a statement and an expression, try StackOverflow.
 [2019-03-21 23:34 UTC] xxalfa at gmail dot com
We are not talking about the same thing yet. The difference is well known to me, I ask more the question, why is "goto" can not be an expression. What would have to be done for "goto" to say the other side is there (or not), I'll jump (or not). I imagine a milder "goto" feature, which asks politely, can I jump? The current "goto" function absolutely wants to have a counterpart.
 [2019-03-21 23:39 UTC]
If there is no matching label then PHP will die. goto will never return.
 [2019-03-21 23:42 UTC] spam2 at rhsoft dot net
the better question is which sane code needs goto to begin with, it was nice as i where a young child coding on a C64 in basic but even not relevant 20 years ago in visual basic
 [2019-03-22 00:55 UTC] xxalfa at gmail dot com
Reply to
Please explain in more detail why PHP would die? If the counterpart can not be found, the program will simply continue running at that point. My wish is it, the counterpart is not found, goto returns false.

Reply to spam2 at rhsoft dot net
My idea of a possible area of application is in the direction of sequencer processing. With goto I jump to the program part and with popen, for example, opens two sub-process and monitors the runtime - while the main program is waiting for an event capture. Depending on the status of the sub-process, the main program will evaluate the status and take further steps. It would have to be determined what happens in case of: condition of success, termination condition and timeout.
 [2019-03-22 01:01 UTC]
-Block user comment: No +Block user comment: Yes
 [2019-03-22 01:01 UTC]
> Please explain in more detail
No. This is a bug tracker, not a discussion forum.
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Fri Jul 30 21:01:23 2021 UTC