PHP code displaying character instead of number

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
steveWillett
Forum Newbie
Posts: 1
Joined: Wed May 28, 2014 1:39 pm

PHP code displaying character instead of number

Post by steveWillett »

I have been bedeviled by this problem periodically. I have PHP code that generates a number and stores it in a variable, but when I display the variable on a web page, SOMETIMES it is displayed as an odd character instead instead of the number. The current example is that I have generated the value of 10, but it displays as a colon on the web page. The behavior is browser specific - the current example happens with Chrome & IE, but not with Firefox.

The application is displaying hours worked and leave accrual information based on a database of submitted time reports. There is a method that calculates how much vacation was earned based on employee hire date and hours worked and another that calculates what the maximum vacation accrual for the employee is. Here is the relevant code:

Code: Select all

	
	// $rates - the current monthly accrual rates, based on years of service
	// $allocRatio - the ratio of hours worked to the monthly number of hours, for vacation allocation purposes
	// $yos - years of service
	function vacEarned($rates, $allocRatio, $yos) {
		$rate = $rates['vacation5'];
		if ($yos>=15)
			$rate = $rates['vacation20'];
		else if ($yos>=10)
			$rate = $rates['vacation15'];
		else if ($yos>=5)
			$rate = $rates['vacation10'];
		return round($allocRatio * $rate, 2);
	}
...

       $maxedVac = false;
...
	$maxVac = maxVacation($currentRates, $hireDate, $tlrDates[$dateID], $currentPctTime);
	$vacEarned = vacEarned($currentRates, $allocRatio, $yos);
	$vacUsed = $usage[$idx]['vacUsed'];
	$currentVac += $vacEarned - $vacUsed;
	if ($currentVac>$maxVac) {
		$maxedVac = true;
		$vacEarned = $vacEarned + $maxVac - $currentVac;
		$currentVac = $maxVac;
	}

I can usually find a kludge to fix this by echoing the variable to the page once first and then doing it for real. The lines that display the value look like this:

[text]
<!-- the following line is my kludge to get around the problem -->
<!-- <?php echo $vacEarned; ?> -->

<td class="formField" align="center"><?php echo $vacEarned; ?><?php echo ($maxedVac ? " *" : ""); ?></td>
[/text]

(Note that the code prints an asterisk next to the value if the accrual has maxed out - not the case in this example) Any suggestions?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: PHP code displaying character instead of number

Post by requinix »

That is highly unusual. Any chance you can pare down the code to something we can try ourselves? I understand how you could get a colon but (a) it would be in all browsers and (b) I don't see how that code could cause it.

Otherwise the exact values of all those variables, including the type (eg, string, int), would be great. var_dump can help with that.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: PHP code displaying character instead of number

Post by Christopher »

I agree. Carefully check the type and value of each variable through the process. It may be a character encoding issue. Perhaps at some point it is being interpreted as a UTF character rather than a number.
(#10850)
Post Reply