• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Simple PHP problem, please help

Vicken

Senior member
I am new to PHP and I have a problem which I am trying to figure out.

Say a user inputted 5 strings in a form. Some of these strings can be null.

What I want to do is find out which ones are NOT null and print out the string name and value.

like this

5 strings = A B C D E

User inputs 5 for A, and 4 for E

program will output this

A 5

E 4

I want to use arrays instead of a million if then statements.

How is this done?
 
Vicken

Try something like a for loop:

create two arrays to represent the letters and the numbers input by the users

$a[] = A
$a[] = B etc

and another for the values to be associated with A, B etc.

$b[] = user value etc

let the user input the values (probably construct a loop for this too)

then print them out if they have a value:

for ($i = 0;$i <=5;$i++)
{
if (!empty($b[$i]) print ("You assigned %s to value %s", $a[$i], $b[$i]);
}


 
Back
Top