cnotethegr8
cnotethegr8

Reputation: 7510

php adding numbers

each of the urls displays a number, but when i echo out $z it displays 0 instead of the large group of all the numbers combined...

what am i doing wrong?

 <?php 
 $a = "<script type=\"text/javascript\" src=\"http://modmyi.com/cstats/index.php?package=Battery+Theme&output=text\"></script>";
 $b = "<script type=\"text/javascript\" src=\"http://modmyi.com/cstats/index.php?package=Connection+Theme&output=text\"></script>";
 $c = "<script type=\"text/javascript\" src=\"http://modmyi.com/cstats/index.php?package=Icon+Theme&output=text\"></script>";
 $d = "<script type=\"text/javascript\" src=\"http://modmyi.com/cstats/index.php?package=Percent+Theme&output=text\"></script>";
 $e = "<script type=\"text/javascript\" src=\"http://modmyi.com/cstats/index.php?package=StatusNotifier+Theme&output=text\"></script>";
 $f = "<script type=\"text/javascript\" src=\"http://modmyi.com/cstats/index.php?package=c-note&output=text\"></script>";
 $g = "<script type=\"text/javascript\" src=\"http://modmyi.com/cstats/index.php?package=iAcces+c-note+KB&output=text\"></script>";
 $h = "<script type=\"text/javascript\" src=\"http://modmyi.com/cstats/index.php?package=c-note+Lite&output=text\"></script>";

 $z = $a+$b+$c+$d+$e+$f+$g+$h;
 echo $z;
?>

Upvotes: 2

Views: 1047

Answers (5)

Bang Dao
Bang Dao

Reputation: 5102

I think this is the solution

 <?php 
 $a = file_get_contents("http://modmyi.com/cstats/index.php?package=Battery+Theme&output=text");
 $b = file_get_contents("http://modmyi.com/cstats/index.php?package=Connection+Theme&output=text");
 ......
 $z = $a+$b+$c+$d+$e+$f+$g+$h;
 echo $z;
?>

Upvotes: 1

nico
nico

Reputation: 51640

You are mixing server-side scripting (PHP) with client-side scripting (JS).

When you request a PHP page the page is interpreted by the server that then serves it to the client as, for instance, an HTML document. That's why if you look at the source of a PHP page in a browser you will not see any PHP code.

Those <script>s will be executed on the client, AFTER the PHP has been processed. While the server is processing your PHP those are just strings, so effectively you're just adding some strings (which results in 0).

So, if you need the sum in the PHP you should not use JS to call those pages.

There are several ways to do it, but I would use cURL to fetch the pages results (see for instance this example) and then sum the results (which you'll have to convert to int before summing).

Upvotes: 2

bobince
bobince

Reputation: 536369

In addition to what strager said, IMO you're going a bit wrong by templating HTML together using strings. PHP is a templating language, you might as well use it:

<?php
    $packages= array(
        'Battery Theme', 'Connection Theme', 'Icon Theme', 'Percent Theme',
        'StatusNotifier Theme', 'c-note', 'iAcces c-note KB', 'c-note Lite'
    );
?>
<?php foreach ($packages as $package) { ?>
    <?php $src= 'http://modmyi.com/cstats/index.php?package='.urlencode($package).'&output=text'; ?>
    <script type="text/javascript" src="<?php echo htmlspecialchars($src); "></script>
<?php } ?>

(Note the HTML-encoding, to avoid the invalid unescaped ampersands in src.)

Upvotes: 0

L&#232;se majest&#233;
L&#232;se majest&#233;

Reputation: 8045

First off, PHP is processed before the page is passed to the web browser. Javascript is processed afterwards. So your PHP variables are just the string literals you wrote in your script.

And to understand why the result is 0, read how PHP typecasts strings to integers: http://www.php.net/manual/en/language.types.string.php#language.types.string.conversion

Upvotes: 1

strager
strager

Reputation: 90012

You want to concat strings, but you're adding strings. String addition first casts each side to a number and returns a number.

Use the string concatenation operator (.) instead:

$z = $a.$b.$c.$d.$e.$f.$g.$h;

Or interpolate:

$z = "$a$b$c$d$e$f$g$h";

Or use an array and join:

$z = implode('', array($a, $b, $c, $d, $e, $f, $g, $h));

Upvotes: 2

Related Questions