Reading a two digit number, using substr, and creating images

So I have a snippet of code that will output a two digit number using saved images…the two digit number could be anything from 1 to 99.

The problem is that anytime it has a trailing zero (10, 20, 30…) - it does not display the “zero” image. I have tried various versions of code using isset, is_null, empty, etc…cannot get this to display properly.

[php]$number = “70”;

$num1 = substr($number,0,1);
$num2 = substr($number,1,2);

echo “Num1 = $num1

Num2 = $num2”; // It is parsing properly…this displays.

$num1 = “$num1”;
if ($num2) {$num2 = “$num2”;} else {$num2 = “”;}[/php]

The IF statement seems to be the problem, but I cannot figure out the solution. Appreciate any help!

Try this:

Note the if ($num2 != “”)

[php]<?php
$number
= “70”;

$num1 = substr($number,0,1);
$num2 = substr($number,1,1); // <----- Changed substr($number,1,2) to substr($number,1,1)

echo “Num1 = $num1

Num2 = $num2”; // It is parsing properly…this displays.

$num1 = “<img alt=”" . $num1 . “” height=“24” width=“24” src=“images/numbers/num” . $num1 . “.png”>";
if ($num2 != “”) {$num2 = “<img alt=”" . $num2 . “” height=“24” width=“24” src=“images/numbers/num” . $num2 . “.png”>";} else {$num2 = “”;}

echo “

” . $num1;
echo “

” . $num2;
?>php]

I’ll try that again. :smiley:

[php]<?php
$number
= “70”;

$num1 = substr($number,0,1);
$num2 = substr($number,1,1); // <----- Changed substr($number,1,2) to substr($number,1,1)

echo “Num1 = $num1

Num2 = $num2”; // It is parsing properly…this displays.

$num1 = “<img alt=”" . $num1 . “” height=“24” width=“24” src=“images/numbers/num” . $num1 . “.png”>";
if ($num2 != “”) {$num2 = “<img alt=”" . $num2 . “” height=“24” width=“24” src=“images/numbers/num” . $num2 . “.png”>";} else {$num2 = “”;}

echo “

” . $num1;
echo “

” . $num2;
?>[/php]

xerxes is right but should include explanation

using if ($num2) means that $num2 must be true. So that translates to if (0) which is false (because zero is considered “empty”)

Sponsor our Newsletter | Privacy Policy | Terms of Service