0

I'm trying to make a profile completion progress, which shows the percentage of how much a user has completed his profile settings. If a user has filled in a field, he receives +15 or +5, however, if the field is not filled in he receives +0.

the code I did is really bad, with variable repetitions, I wanted to know if you knew a cleaner way to do this.

if (!empty($user->avatar)) {
  $avatar = 15;
} else { $avatar = 0; }
if (!empty($user->copertina)) {
  $copertina = 15;
} else { $copertina = 0; }
// dati personali
if (!empty($user->name)) {
  $name= 5;
} else { $name = 0; }
if (!empty($user->last_name)) {
  $last_name = 5;
} else { $last_name = 0; }

[...]

if (!empty($user->biografia)) {
  $biografia = 5;
} else { $biografia = 0; }

$personal = $avatar+$copertina+$name+$last_name+$sesso+$nascita;
$gaming = $steam+$battlenet+$xbox+$playstation+$switch+$uplay+$origin+$ds;
$social = $twitter+$facebook+$biografia;

$punti = $personal+$gaming+$social;

how do I remove all the others {$ variable = 0}?

LegoLiam
  • 97
  • 3
  • 11
  • 1
    You can't really, since you want the value to be a number, and not "undefined". You could initialize your variables to 0 like in this answer https://stackoverflow.com/questions/9651793/initializing-multiple-php-variables-simultaneously. If you want to get into type comparisons for null variables, check http://php.net/types.comparisons. I would just initialize the variables to 0 and remove all the else. – Nic3500 Jul 15 '18 at 11:23
  • 1
    OR... modify your $user object to have all these variables in an array ($key:$value). You can then initialize the array to 0 all over, and modify it. Adding a new profile value would be easy, and adding array values is quick. – Nic3500 Jul 15 '18 at 11:26
  • Thank you very much for your help @Nic3500, I have used the method array destructuring. :) – LegoLiam Jul 15 '18 at 11:52
  • Pleasure, I transformed my comments into an answer, if you could accept it I would be grateful. – Nic3500 Jul 15 '18 at 15:31

2 Answers2

0

You can't really, since you want the value to be a number, and not "undefined". You could initialize your variables to 0 like in this answer stackoverflow.com/questions/9651793/….

If you want to get into type comparisons for null variables, check php.net/types.comparisons. I would just initialize the variables to 0 and remove all the else.

OR...

modify your $user object to have all these variables in an array ($key:$value). You can then initialize the array to 0 all over, and modify it. Adding a new profile value would be easy, and adding array values is quick.

Nic3500
  • 8,144
  • 10
  • 29
  • 40
0

This snippet :

if (!empty($user->avatar)) {
 $avatar = 15;
} 
else {
 $avatar = 0;
}

is semantically equivalent to :

$avatar = (bool)$user->avatar * 15;

Explanation:

  1. Non-empty field gets converted to true and empty string or null gets converted to false

  2. Because we do multiplication php true/false values gets converted to 1/0

  3. So after multiplication you get 15 * 1 or 15 * 0 - depending if your field was used or not.
Agnius Vasiliauskas
  • 10,935
  • 5
  • 50
  • 70