Assignment to variable in PHP

There is a construction that outputs the result of 2 digits:

if (intval($z21a) > 0) { printf("%02d", intval($z21a)); } else { echo ''; }

I want to make the $z21d variable equal to this code:

$z21d = if (intval($z21a) > 0) { printf("%02d", intval($z21a)); } else { echo ''; } ;

There is a syntax error. Tell me what I did wrong?


Answer 1, authority 100%

The if syntax does not return anything. Make an assignment from the body:

if (intval($z21a) > 0) { $z21d = sprintf("%02d", intval($z21a)); } else { $z21d = ''; }

Or use the ternary operator:

$z21d = intval($z21a) > 0 ? sprintf("%02d", intval($z21a)) : '';

PS: There is another option for working with a buffer, but this is closer to perversions. =)