I want get an array of errors using if condition
my code is below
$errors = array();
if(empty($name))
$errors[] = 'Name required';
elseif(empty($usernmae))
$errors[] ='Username required';
final output i expect
$errors = array([0]=>'Name Required',[1]=>'username required');
but it returns only one array element
$errors = array([0]=>'Name Required');
anyone know
elseif(empty($usernmae))
It will only do this as an else
, so you won't ever have both errors returned.
If you want both, you need to do it as two if's:
if(empty($name))
$errors[] = 'Name required';
if(empty($usernmae))
$errors[] ='Username required';
Use only if
instead of else if
$errors = array();
if(empty($name))
$errors[] = 'Name required';
if(empty($username))
$errors[] ='Username required';