LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-15-2008, 02:16 PM   #1
jus71n742
Member
 
Registered: Apr 2008
Distribution: Ubuntu 8.04 64bit/ 32bit, FreeBSD 8
Posts: 185

Rep: Reputation: 30
Question PHP simple interest


I am playing with PHP trying to get any 3 inputs from the user and then compute the simple interest from what that person puts in. I have so far the following


php code:
Code:
<?php
$var_r=$_POST['r'];
echo $var_r . "\n";
$var_P=$_POST['P'];
echo $var_P . "\n";
$var_t=$_POST['t'];
echo $var_t . "\n";
$var_I=$_POST['I'];
echo $var_I

if (!($var_I=$_POST['I']))
{
	$var_m=$var_P * $var_t * $var_r;
}
elseif (!($var_P=$POST['P']))
{
	$var_m=$var_I/($var_r*$var_t);
}
elseif (!($var_t=$POST['t']))
{
	$var_m=$var_I/($var_p*$var_r);
}
else (!($var_r=$POST['r'}))
{
	$var_m=float=2(($var_I/($var_p*$var_t))*100);

?>

your simple interest will be:<?php echo $var_m ; ?>


basically I am trying to either set up a loop of an if statement stating that if (3 variables are input) do this formula and so on and so forth....any help getting going would be much appreciated.

Last edited by jus71n742; 04-15-2008 at 04:32 PM.
 
Old 04-15-2008, 04:36 PM   #2
BrianK
Senior Member
 
Registered: Mar 2002
Location: Los Angeles, CA
Distribution: Debian, Ubuntu
Posts: 1,334

Rep: Reputation: 51
so what's the question? are you asking for a better method? or are you asking for the form that would submit these values? something else? There are a few typos in there & you should probably check more than one variable at a time using the && operator.
 
Old 04-15-2008, 04:38 PM   #3
Maz_
LQ Newbie
 
Registered: May 2006
Location: Finland
Distribution: fedora
Posts: 15

Rep: Reputation: 0
Code:
<?php

if(isset($_POST['r'])&&isset($_POST['P'])&&isset($_POST['t'])&&isset($_POST['I']))
{
    .
    .
    .
    .
}
or use foreach
( http://fi.php.net/foreach )
 
Old 04-15-2008, 06:31 PM   #4
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
The function that you want is empty(). This will return true is the value is empty.

You will of course want to check that you have three valid values, you can do this by stepping through each variable and keeping a count of the number of number of non-empty once, so long as you have a total of three then you have valid input and so you can proceed.

Give it a go and if you have any problems get back.
 
Old 04-16-2008, 10:21 AM   #5
jus71n742
Member
 
Registered: Apr 2008
Distribution: Ubuntu 8.04 64bit/ 32bit, FreeBSD 8
Posts: 185

Original Poster
Rep: Reputation: 30
where is this function empty? a tutorial maybe somewhere?....I have heard of this function but I haven't found much on it. how would you set that up?
 
Old 04-16-2008, 11:59 AM   #6
BrianK
Senior Member
 
Registered: Mar 2002
Location: Los Angeles, CA
Distribution: Debian, Ubuntu
Posts: 1,334

Rep: Reputation: 51
Quote:
Originally Posted by jus71n742 View Post
where is this function empty? a tutorial maybe somewhere?....I have heard of this function but I haven't found much on it. how would you set that up?
http://www.php.net/

use the "search for" fucntion on that site. Every php function is there.
 
Old 04-16-2008, 01:02 PM   #7
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Ubuntu 12.04, Antix19.3
Posts: 3,794

Rep: Reputation: 282Reputation: 282Reputation: 282
@BrianK
If one knows the function name (as in this case), one does not need to search but can append it at the end of the URL (in this case http://www.php.net/empty).

Better even, if you do NOT know the exact function name, just add it like in http://www.php.net/mysql_affectedrows . It will tell you that it could not find it and give you a list of names that are close.

Hasn't failed me yet but might only be possible for english language.
 
Old 04-16-2008, 05:32 PM   #8
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
Quote:
Originally Posted by jus71n742 View Post
where is this function empty? a tutorial maybe somewhere?....I have heard of this function but I haven't found much on it. how would you set that up?
As others have mentioned you can look up the details of empty() in the php manual, either the online version or if you have a local copy you can find it there.

Technically empty() is not a function but a construct of the language, but because it follows the syntax of a function lazy people like me call it a function. However because of this it does have some limitations all explained in the manual.

Finally, like many modern scripting languages (Perl, Python, Ruby and PHP all come to mind) are supported by a large collection of functions that are considered part of the language and are bundled by default with the language and yet more can be added as you need them. So a large part of learning the language is in understanding the functions that are available. My advice, once you are comfortable with the core language browse the manual just looking at the wide collection of function families that are available and then spend a little time looking at the function families that look useful or intriguing. An early investment in the array and string functions would be time well spent.

Enjoy the journey
 
Old 04-17-2008, 01:59 PM   #9
jus71n742
Member
 
Registered: Apr 2008
Distribution: Ubuntu 8.04 64bit/ 32bit, FreeBSD 8
Posts: 185

Original Poster
Rep: Reputation: 30
Question

Code:
if empty($var_I==NULL])

{

	$var_I=$var_m;

	$var_m=$var_P * $var_t * $var_r;

}

elseif empty($var_P==NULL)

{

	$var_P=$var_m;

	$var_m=$var_I/($var_r*$var_t);

}

elseif empty($var_t==NULL)

{

	$var_t=$var_m;

	$var_m=$var_I/($var_p*$var_r);

}

else empty($var_r==NULL)

{

	$var_r=$var_m;

	$var_m=float=2(($var_I/($var_p*$var_t))*100);



?>

is this more along the lines of what you are talking about
 
Old 04-17-2008, 04:32 PM   #10
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
Try
Code:
if (!empty($var_I))
 
Old 04-17-2008, 04:37 PM   #11
BrianK
Senior Member
 
Registered: Mar 2002
Location: Los Angeles, CA
Distribution: Debian, Ubuntu
Posts: 1,334

Rep: Reputation: 51
Quote:
Originally Posted by jus71n742 View Post
Code:
if empty($var_I==NULL])
is this more along the lines of what you are talking about
the expression ($var_I==NULL) returns a true or false.

Therefore "empty($var_I==NULL)" expands to empty(true) or empty(false) which makes no sense.

as graemef said, the correct usage is empty(<variable>) or, in this case empty($var_I)
 
Old 04-18-2008, 03:23 PM   #12
marquardl
Member
 
Registered: Apr 2008
Posts: 100

Rep: Reputation: 15
$number === NULL

PHP also has the possibility to sort out invalid return values for parameters where NULL is a possible value.

$number == NULL // $number is a number and can be 0
$number === NULL // $number is not set or is FALSE

GWN

Last edited by marquardl; 05-01-2008 at 03:55 AM.
 
Old 04-20-2008, 10:35 PM   #13
jus71n742
Member
 
Registered: Apr 2008
Distribution: Ubuntu 8.04 64bit/ 32bit, FreeBSD 8
Posts: 185

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by graemef View Post
Try
Code:
if (!empty($var_I))
that was the first thing I tried...i kept getting a parse error on line 29 which is the start of the if statements on mine
 
Old 04-21-2008, 05:45 AM   #14
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
If you are getting a parse error on stmt 29, then it could be an error on line 28.

What exactly is the error?
 
Old 04-21-2008, 10:15 AM   #15
jus71n742
Member
 
Registered: Apr 2008
Distribution: Ubuntu 8.04 64bit/ 32bit, FreeBSD 8
Posts: 185

Original Poster
Rep: Reputation: 30
Ok I thought I was done...apparently not...I am still getting an error and I had some help with it. we wrote the code together then I copied it EXACTLY like it was on his when it worked....now that it is on my machine ....
Quote:
parse error, unexpected T_VARIABLE on line 28
I have no clue what is wrong. I am probably looking right at it and not seeing it..
here is line 29 up to the start of the actual PHP

Code:
<?php
echo "RATE:".$_POST['r']."<br>";
echo "PRINCIPLE: ".$_POST['P']."<br>";
echo "TIME: ".$_POST['t']."<br>";
echo "INTEREST:".$_POST['I'] ."<br>";

$r = $_POST['r'];
$p = $_POST['P'];
$t = $_POST['t'];
$i = $_POST['I'];

if (empty($i)) {
    $i = $p * $t * $r;
    print ("Interest will be: ").$i;

Last edited by jus71n742; 04-23-2008 at 01:51 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
A simple PHP problem Omni Programming 5 07-04-2007 11:24 AM
simple problem about php mohtasham1983 Programming 6 01-05-2006 09:09 PM
simple php, mysql help itz2000 Programming 4 12-10-2005 08:37 AM
Simple PHP Question newuser455 Programming 9 08-28-2005 11:58 PM
simple php question jfall Programming 1 05-03-2005 02:57 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:05 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration