Loops in PHP

Posted on 2003-02-27
Medium Priority
Last Modified: 2006-11-17

I have a bit of PHP at the top of my checking page, I need it to run through ten variables (var[1] thru var[10]) and make sure they all are more than "" they contain something, if they all do I need to it to then convert all those var[1] etc to their real values like
$username=var[1] etc and then jump to another URL to process, if one of these variables is still empty I need it to continue loading the page, how do I go about this ?
Question by:MirageSF
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 15

Accepted Solution

VGR earned 400 total points
ID: 8032390
ok, first you need an array holding the "real names" of your variables, like this :

then you code is as follows :
$nbv=0; // counter of empty values
for ($i=0;$i<10;$i++) { // explore variables set to validate
  if ($var[$i]=="") $nbv++; else ${$realnames[$i]}=$var[$i];
} // for explore
if ($nbv==0) { // set true names and relocate somewhereelse
} // else nothing, continue to "load the page"

Expert Comment

ID: 8196480

// first, check to see that all variables exist

for ($x = 0; $x < 10; $x++)
    if ($var[$x] =="")      // or !isset($var[$x])
           exit;  // if any of the values are empty, page stops processing.

// if you made it here, you have all your values...

$username = $var[0];
$lastname = $var[1];
$email    = $var[2];
$age      = $var[9];

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question