Solved

String to Integer issue in php

Posted on 2011-02-23
5
320 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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
This article discusses four methods for overlaying images in a container on a web page
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

760 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