Skip to content

Using goto to skip code #4673

Discussion options

You must be logged in to vote

I suppose I could do something with exceptions and finally if I have to

To save others the bother for this bit:

https://phpstan.org/r/392661a4-f07f-4756-870b-4e7976f9ddeb:

<?php declare(strict_types = 1);

/** 
  * @return string|false
  */
function string_or_false(){
	if (rand(1,2)==1)
		return "string";
	return false;
}

/**
  * @param string $s
  * @return void
  */
function takes_string(string $s){
	echo $s;
}

try{
	$a = string_or_false();
	if ($a === false)
		throw new Exception("x");

	takes_string($a); 
}catch(Exception $e){
	echo $e->getMessage();
}finally{
	echo "finished";
}

No errors.

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@ondrejmirtes
Comment options

@pauljherring
Comment options

Answer selected by ondrejmirtes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Support
Labels
None yet
2 participants