Q: Output values in foreach loop only once I'm using a foreach loop to save error messages resulting from invalid user inputs in an array,change it into a a string and output them on another page.However, while i am able to output the string succesfully, the string is being repeated multiple times.
Extract of my code:
foreach($_POST as $field=>$value)
{
if(!empty($value))
{
    if($_POST['Email']==$_POST['Verify_Email'])
    {
        $email=$_POST['Email'];
        $email=mysqli_real_escape_string($cxn,$email);//Saves the input  data & preps it for insertion into database.
    }
    else
    {
        $error[]="Mismatched Email Addresses";
    }

    if(preg_match('/^[a-z\d_]{2,20}$/i',$_POST['Username']))
    {
        $user=$_POST['Username'];
        $user=mysqli_real_escape_string($cxn,$user);
    }
    else
    {
        $error[]="Invalid Username entered.Please enter an alphanumerical username between 2 to 20 characters";
    }

    if($_POST['Password']==$_POST['Verify_Password'])
    {
        $pass=$_POST['Password'];
        $pass=mysqli_real_escape_string($cxn,$pass);
    }
    else
    {
        $error[]="Your passwords do not match";
    }

    if(preg_match('/^[a-z\d_]{2,20}$/i',$_POST['BizName']))
    {
        $biz=$_POST['BizName'];
        $biz=mysqli_real_escape_string($cxn,$biz);
    }
    else
    {
        $error[]="Invalid BizName entered.Please enter an alphanumerical     username between 2 to 20 characters";

    }
}

if(!empty($_POST['Desc']))
{
    $desc=$_POST['Desc'];
    $desc=mysqli_real_escape_string($cxn,$desc);
}
else
{
    $desc="NULL";
    $desc=mysqli_real_escape_string($cxn,$desc);
}
}

foreach($_POST as $field => $value)
{
$$field = strip_tags(trim($value));
}
if(!empty($error))
{
$errormsg="";
foreach($error as $value)
{   
    $errormsg.= "$value";
}
include("RegForm.php");
exit();
}
include("RegForm.php");

I suspect that the cause of $errormsg being repeated multiple times is due to the values in $error[ ]
(E.g $error[]="Invalid BizName entered.Please enter an alphanumerical username between 2 to 20 characters";)

This is because the number of times $error is being repeated coincides with the number of user input fields, and the number of $error [ ] i have.
Question
If i am correct in $error being the cause of $errormsg being repeated multiple times, could someone kindly point out why is it happening as the foreach loop should only run as many times as there are values in the $error array( How many erros there are).If my assumption is wrong, could someone point me in the right direction?
Thanks

A: You are using a foreach loop wrong. Check below
<?php
foreach($_POST as $field=>$value)
{
  if(!empty($value))
  {
    /*
    this code block is executed for every POSTed var with a value.
    So once for Email
    So once for Verify_Email
    So once for Username
    So once for Password
    So once for Verify_Password
    So once for BizName
    So once for Desc
    etc..
    */

  }
}
?>

I guess you just want to use the code block without the foreach.
You are also doing this.
<?
foreach($_POST as $field => $value)
{
  $$field = strip_tags(trim($value));
}
?>

This way I can set any variable in your script to any value. If you would use a variable like $login = true to keep track of who is logged on, I can simply POST login=1 and I would have access.

A: The problem is that for each key-value pair in $_POST you run all the checks and add error messages in $error. So, essentially, you run the checks as many times as there are keys in $_POST and for each loop you append the same error messages to $error.
Remove these lines: 
foreach($_POST as $field=>$value)
{
if(!empty($value))
{

and the matching closing brackets and everything should work fine.

A: That happens because of the first foreach($_POST as $field=>$value) loop.
You tell the script to extract a key and his respective value in $_POST, set them to $key and $value and then exec the code inside the loop. The loop is executed for each variable in $_POST.
If you are accesing $_POST variables directly, then you don't need to loop all variables in $_POST.
Remove
foreach($_POST as $field=>$value)
{
if(!empty($value))
{

and everything will work fine.

A: Yo could develop your error array a little bit more then ! (following your answer to my comment):
Here would be the $error structure:
Array Structure
$error = array(
    'email'=>array(
        'hasError'=>'0',
        'message'=>'My error Message'
    ),
    'username'=>array(
        'hasError'=>'0',
        'message'=>'My error Message'
    ),
    ...=>array(
        'hasError'=>'0',
        'message'=>'My error Message'
    )
);

Creating the array
$errorMessages= array(
    'email'=>'Wrong email!',
    'username'=>'Wrong username!',
    'password=>'Wrong password!',
    'desc'=>'Wrong description!');

$elementsToBeValidated = array('email','username','password','desc');
$error = '';    

foreach($elementsToBeValidated as $inputElem):
    $error[$inputElem]['hasError'] = false;
    $error[$inputElem]['message'] = $errorMessages[$inputElem];


so instead concatenating all your error messages in an array when validating, just change the flags to true:
Validation example
if(/*conditions for a wrong username */) $error['username']['hasError'] = true;


To check all the errors you got at the end, (out of the loop), do something along those lines:
PHP
$errorMessage = '';
foreach($error as $e):
    if($e['hasError']) #if the hasError flag is true
        $errorMessage.=$e['message'];
endforeach;

if(strlen($errorMessage)>0) return $errorMessage; #or echo, whatever you want !


A: By doing 
$error[]="Invalid BizName entered.Please enter an alphanumerical username between 2 to 20 characters";

You are declaring the $error array each time you encounter it, the correct way is to define this array outside the foreach part with $error = array(); then inside your loop you want to append a value inside your array, you need to do array_push($error, "error message");
Your array will be filled fine then.
