Solved

String to Integer issue in php

Posted on 2011-02-23
5
325 Views
Last Modified: 2012-05-11
Hi
I'm having trouble with retrieving a couple variables appended to a url and dividing them and assigning the result to a new variable. I suspect it has something to do with them possibly being strings as opposed to integers so the math is returning 0 for a value. But I could be absolutely wrong.

Below is the pertinent code.

When dividing expenses by income the value for $tdsr keeps appearing as '0'

Any ideas?
<?php
header("content-type: text/xml");
echo  "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";

$recording = $_REQUEST['recording'];
$numbertocall = $_REQUEST['numbertocall'];
$income = $_REQUEST['income'];
$expenses = $_REQUEST['expenses'];

$tdsr = $expenses/$income;


?>

Open in new window

0
Comment
Question by:tjyoung
  • 3
5 Comments
 
LVL 14

Accepted Solution

by:
brendanmeyer earned 500 total points
ID: 34966694
have a look at http://www.php.net/manual/en/function.intval.php


$income = intval ($_REQUEST['income']);
$expenses = intval ($_REQUEST['expenses']);
0
 
LVL 1

Author Comment

by:tjyoung
ID: 34966727
Hi, I did try that but I suspect my implementation must be wrong. Below is a more comprehensive view of the code I'm using with intval in.

Do you see what I'm doing wrong? It may be something very newbie...

Its mixed in with some Twilio code. All works except the value of $tdsr is '0'.
<?php
header("content-type: text/xml");
echo  "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";

$recording = $_REQUEST['recording'];
$numbertocall = $_REQUEST['numbertocall'];
$income = intval($_REQUEST['income']);
$expenses = intval($_REQUEST['expenses']);

$tdsr = $expenses/$income;


?>

<Response>

<?php
if ($_REQUEST['Digits']=="1"){

	$approvalID = mt_rand(301, 9950);
	$approvalSlow = wordwrap($approvalID,1,",",TRUE);


	if ($tdsr >= 70) { ?>
		<Say>This is hopeless</Say>
	
	<?php } elseif ($tdsr >= 60 && $tdsr < 70) { ?>
		<Say>You're application requires a few additional questions to be pre-approved</Say>
		<Say>Our Dealership will contact you so we can gather some additional information to get your application approved.</Say>
	
	<?php } elseif ($tdsr < 60) {?>
		<Say> Congratulations, you are Pre-Approved!</Say>
		<Say> Your Approval ID is <?php echo $approvalSlow ?></Say>
		<Say> Please make a note of your Pre-Approval ID </Say>
		<Say> That number again is <?php echo $approvalSlow ?></Say>
		<Say> Your total debt service ratio is <?php echo $tdsr ?></Say>
	<?php
	
	$email = "email@email.com";
	$subject = "New 5 Second Phone Credit App! Approval ID: ". $approvalID;
	$body .= "The Caller's recorded name is:";
	$body .= $_REQUEST['recording'].".mp3\n\n";
	$body .= "The Caller's reverse lookup name if available was: ${_REQUEST['CallerName']}\n\n";
	$body .= "The Caller's preferred phone number is: ${_REQUEST['numbertocall']}\n\n";
	$body .= "The Caller's display phone number is: ${_REQUEST['From']}\n\n";
	$body .= "The Caller is in: ${_REQUEST['CallerCity']}\n\n";
	$body .= "The Caller's TDSR: ". $tdsr."\n\n";

    
	$headers = 'From: me@me.com' . "\r\n";
	$headers .= 'Reply-To: me@me.com' . "\r\n";
	$headers .= 'X-Mailer: Twilio';

	mail ($email, $subject, $body, $headers);
	}
	?>

Open in new window

0
 
LVL 1

Author Closing Comment

by:tjyoung
ID: 34966745
Your answer was correct. I was implementing it incorrectly initially.
thanks!
0
 
LVL 6

Expert Comment

by:MatthewP
ID: 34966760
I can't see what's wrong with that and you shouldn't even need intval. Have you checked the raw values as they come into the script before you've touched them?

Just add

var_dump($_REQUEST);

to the top of your script for a quick way to check this out.
0
 
LVL 1

Author Comment

by:tjyoung
ID: 34966780
Thanks. That was my issue. I marked your answer as correct simply because had I actually had the 'correct problem' your response was right.
var_dump is good to know as well.

Thanks again.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now