Thursday, March 9, 2023
HomeSoftware EngineeringHow one can Create Perform with A number of Returns in PHP

How one can Create Perform with A number of Returns in PHP


If it’s good to create a PHP perform that returns a number of values, then you are able to do one of many following.

Choice 1 – Returning an array

perform returnArray() {
  return array("one", "two");
}

// name the perform and print the response
var_dump(returnArray());

Choice 2 – Returning a conditional

perform returnConditional($x = true) {
  if ($x) return "one";
  else return "two";
}

// name the perform with a price and print the response
var_dump(returnConditional(false));

Choice 3 – Utilizing generator to yield values

perform multipleValues() {
  yield "one";
  yield "two";
}

$res = multipleValues();
foreach($res as $r) {
  echo $r; // first val=="one", second val=="two"
}
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments