php.net |  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
 [2019-03-21 03:11 UTC] xxalfa at gmail dot com
Description:
------------
I don't understand why "goto" is not treated like "require_once"?

Test script:
---------------
<?php

    //-------------------------------------------------
    // 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; }

    //-------------------------------------------------
    // WORKS AS DESIRED
    //-------------------------------------------------

    $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;

    //-------------------------------------------------
    // DOES NOT WORK AS DESIRED
    //-------------------------------------------------

    $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' );

    //-------------------------------------------------
    // LOOKS UGLY, BUT WORKS
    //-------------------------------------------------

    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;

    exit;

    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;

?>


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2019-03-21 03:18 UTC] requinix@php.net
-Status: Open +Status: Not a bug
 [2019-03-21 03:18 UTC] requinix@php.net
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] requinix@php.net
-Status: Not a bug +Status: Duplicate
 [2019-03-21 22:35 UTC] requinix@php.net
Duplicate of request #72589.
 [2019-03-21 22:38 UTC] requinix@php.net
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] requinix@php.net
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 requinix@php.net
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] requinix@php.net
-Block user comment: No +Block user comment: Yes
 [2019-03-22 01:01 UTC] requinix@php.net
> Please explain in more detail
No. This is a bug tracker, not a discussion forum.

http://php.net/support.php
 
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Tue Oct 26 03:03:35 2021 UTC