Req #77776 [Com]: if inline goto
| From: | xxalfa at gmail dot com | Date: | Thu, 21 Mar 2019 23:34:40 +0000 |
| Subject: | Req #77776 [Com]: if inline goto | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-220118@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=77776&edit=1
ID: 77776
Comment by: xxalfa at gmail dot com
Reported by: xxalfa at gmail dot com
Summary: if inline goto
Status: Duplicate
Type: Feature/Change Request
Package: *General Issues
PHP Version: Irrelevant
Block user comment: N
Private report: N
New Comment:
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.
Previous Comments:
------------------------------------------------------------------------
[2019-03-21 22:38:12] 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 22:35:10] requinix@php.net
Duplicate of request #72589.
------------------------------------------------------------------------
[2019-03-21 06:07:26] 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 03:18:46] 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 03:11:21] 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;
?>
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=77776&edit=1