2

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;
?>
1
  • Can you show the output of calling http://modmyi.com/cstats/index.php?package=Battery+Theme&output=text? Is it just the number as a string? Commented Aug 21, 2010 at 10:44

5 Answers 5

2

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).

Sign up to request clarification or add additional context in comments.

Comments

2

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));

2 Comments

Hmm, I think the OP was looking to add the numbers displayed by those scripts together. It might not be entirely possible, but this is as close as anyone is going to get with the information given.
@Sam152, Oh, hmm. I dunno; they need to give more information if they want their question better answered.
1

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

Comments

1

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;
?>

2 Comments

I think the questioner wants $z to hold all the numbers as a list, so the . concatenation operator might be more appropriate.
I think that he/she want to get the number from those url and calculate the sum.
0

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.)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.