-1

I wonder if it is possible to exit a function e.g. by return in the second statement of a null coalescing operator in PHP.

I want to use this to check some POST data in a function and exit the function if a POST var is not set.

This would be clean code without the painful if(isset(...)).

Example script file:

 <?php

// Controller

$id = $_POST['id'] ?? add();

function add(){
  $var1 = $_POST['var1'] ?? return;  // <-- is this possible or some similar solution?
  $var2 = $_POST['var2'] ?? return;

  // All POST vars are set: do something
  // ...
}
  • Not sure how this logic would work - especially as you are trying to set `$id` from the return value. IMHO this isn't probably isn't the best way of doing it, using more standard structures would allow control over the problems (i.e. do you want some sort of message saying why `add()` didn't do it's job etc.). – Nigel Ren Jun 08 '20 at 07:51
  • This is a controller file which checks a http request. This request can contain an id. If the id is included in the http request, the db data for this id should be fetched (i did not include this in the code example). Otherwise the add() function should check other form post data and, if valid, uses a model class to insert the form data into the database. – user12558800 Jun 08 '20 at 07:57
  • What if `$_POST['var1']` isn't set? This will just return and assume that `$id` now contains the id of the newly added record, which it doesn't. – Nigel Ren Jun 08 '20 at 07:59
  • I see. So the null coalescing operator is always and only used to set vars and not to do routing if vars are not set. – user12558800 Jun 08 '20 at 08:03
  • I am wondering, what is your goal? – Markus Zeller Jun 08 '20 at 08:09
  • To write clean code without repeating. The "isset" concept consists of more code and is not DRY. I must write if(isset(_POST['var1']) { $var1 = _POST['var1']; } . So I always have to refer to _POST['var1'] twice: once for evaluation, once for setting the variable in the function for further processing. So I thought if I could clean my code by using a solution with the null coalescing operator. – user12558800 Jun 08 '20 at 08:15
  • Accessing a variable to check its content before using it is not WET. It's a good practice – Cid Jun 09 '20 at 08:59

1 Answers1

-1

No, this is not possible. Code below may not be a one line solution as you initially wanted but is still shorter than:

I must write if(isset(_POST['var1']) { $var1 = _POST['var1']; } . So I always have to refer to _POST['var1'] twice: once for evaluation, once for setting the variable in the function for further processing.

function add() {
    if (!$var1 = $_POST['var1'] ?? null) {
        return;
    }
    // $var1 declared and has value here
}

You can also throw an exception like this:

// ...
$var1 = $_POST['var1'] ?? throw new \Exception('...');
// ...
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Sep 20 '22 at 12:41