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/javascri开发者_开发百科pt\" 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;
?>
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).
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));
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
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;
?>
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
.)
精彩评论