// note that I named the arguments $a, $b and $c to show that
// they don't need to be named $x, $y and $z
function getXYZ(&$a, &$b, &$c)
{
$a = 4;
$b = 5;
$c = 6;
}
getXYZ($x, $y, $z);
class nameCheck{
public $name;
public function __construct(){
$this->name = $name;
}
function firstName(){
// If a name has been entered..
if(!empty($this->name)){
$name = $this->name;
$errflag = false;
// Return a array with both the name and errflag
return array($name, $errflag);
// If its empty..
}else if(empty($this->name)){
$errmsg = 'Please enter a name.';
$errflag = true;
// Return both the Error message and Flag
return array($errmsg, $errflag);
}
}
}
if($_POST['submit']){
$a = new nameCheck;
$a->name = $_POST['name'];
// Assign a list of variables from the firstName function
list($name, $err) = $a->firstName();
// Display the values..
echo 'Name: ' . $name;
echo 'Errflag: ' . $err;
}
?>
<form method="post" action="<?php $_SERVER['PHP_SELF']; ?>" >
<input name="name" />
<input type="submit" name="submit" value="submit" />
</form>
这将为您提供一个输入字段和提交按钮,一旦提交,如果name输入字段为空,它将返回错误标志和一条消息。如果name字段有值,它将返回值/name,如果false =无错误,则返回错误标志0。
希望这能有所帮助!< / p >
function hasMultipleValues() {
yield "value1";
yield "value2";
}
$values = hasMultipleValues();
foreach ($values as $val) {
// $val will first be "value1" then "value2"
}
function get_id(){
global $b_id, $f_id;
// stuff happens
return $b_id AND $f_id;
}
//later in the code:
get_id();
var_dump($b_id);
var_dump($f_id); // tested output by var_dump
不可能有两个return语句。然而,它不会抛出错误,但当函数被调用时,你只会收到第一个返回语句值。
我们可以使用return of array来获取多个返回值。例如:< / p >
function test($testvar)
{
// do something
//just assigning a string for example, we can assign any operation result
$var1 = "result1";
$var2 = "result2";
return array('value1' => $var1, 'value2' => $var2);
}
<?php
function foo(){
$you = 5;
$me = 10;
return $you;
return $me;
}
echo foo();
//output is just 5 alone so we cant get second one it only retuns first one so better go with array
function goo(){
$you = 5;
$me = 10;
return $you_and_me = array($you,$me);
}
var_dump(goo()); // var_dump result is array(2) { [0]=> int(5) [1]=> int(10) } i think thats fine enough
?>
public function selectAllUsersByRole($userRole, $selector) {
$this->userRole = $userLevel;
$this->selector = $selector;
$sql = "SELECT * FROM users WHERE role <= ? AND del_stat = 0";
$stm = $this->connect()->prepare($sql); // Connect function in Dbh connect to database file
$stm->execute([$this->userRole]); // This is PHP 7. Use array($this->userRole) for PHP 5
$usersIdArray = array();
$usersFNameArray = array();
$usersLNameArray = array();
if($stm->rowCount()) {
while($row = $stm->fetch()) {
array_push($usersIdArray, $row['id']);
array_push($usersFNameArray, $row['f_name']);
array_push($usersLNameArray, $row['l_name']);
// You can return only $row['id'] or f_name or ...
// I used the array because it's most used.
}
}
if($this->selector == 1) {
return $usersIdArray;
}elseif($this->selector == 2) {
return $usersFNameArray;
}elseif($this->selector == 3) {
return $usersLNameArray;
}
}