Forum Moderators: coopster

Message Too Old, No Replies

Math on includes?

         

Spanger

11:49 am on Aug 25, 2007 (gmt 0)

10+ Year Member



Through a script I'm writing, I am ending with some .php files that are just plain numbers - Is it possible to use the math functions and add the numbers from the includes together?

<?php
$dothemath = include('ronviews.php') / include('ronvids.php');

echo 'The answer is $dothemath';

?>

Something like that?

Btw, I do get an error from that, which is why I ask. =)

Thanks!

Spanger

jatar_k

12:02 pm on Aug 25, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



as you can see you can't do that since you are getting an error

you could assign those numbers to vars and then do the math with the vars.

Spanger

1:40 pm on Aug 25, 2007 (gmt 0)

10+ Year Member



Something like this?

<?php
$ronviews = include'ronviews.php';
$ronvids = include'ronvids.php';
$dothemath = $ronviews / $ronvids;

echo 'The answer is $dothemath';
?>

Doesn't work though. I get this:

3,330,03044The answer is $dothemath

=/

What am I doing wrong? (it's probably something really simple. =¦)

Thanks!

Spanger

jatar_k

1:46 pm on Aug 25, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



you would have to play with the included files themselves. You could have them set a var that can be used in the main script. You could also have a return in those files.

maybe this explains it
[php.net...]

Handling Returns: It is possible to execute a return() statement inside an included file in order to terminate processing in that file and return to the script which called it. Also, it's possible to return values from included files. You can take the value of the include call as you would a normal function. This is not, however, possible when including remote files unless the output of the remote file has valid PHP start and end tags (as with any local file). You can declare the needed variables within those tags and they will be introduced at whichever point the file was included.

eelixduppy

1:58 pm on Aug 25, 2007 (gmt 0)



Also, just so you don't go crazy in the future trying to find your error in the echo statement, you are using single quotes around the variable which isn't correct. It would be something like this:

echo 'The answer is '.$dothemath;

or


echo "The answer is $dothemath"; #use double quotes

pixeltierra

4:23 am on Aug 26, 2007 (gmt 0)

10+ Year Member



If all you have as text in your file is a number, you can read the file into a variable, (instead of including it):

// get contents of a file into a string
$filename = "/usr/local/something.txt";
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));
fclose($handle);

In order to actually do math on your vars, though, you'll have to do some type switching to make sure you operating on numbers, not strings.

I'm not sure how you "ended up" with php files of just numbers, though. Probably a better way to do this from the start...