Solved

String to Integer issue in php

Posted on 2011-02-23
5
331 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

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 i…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

726 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