I'm having a problem with this function. It's supposed to echo out some stuff, but for some reason it won't do so when I call it.
Here's the function:
$count = count($info['level']);
function displayInfo()
{
for ($i=0; $i<$count; $i++)
{
echo "[b]".$info['name'][$i]."[/b]\n\n"
."Level: ".$info['level'][$i]
."\nPrice: ".$info['price'][$i]
."\nSellback: ".$info['sell'][$i]
."\nLocation: ".$location
."\n\nType: ".$info['type'][$i]
."\nElement: ".$info['element'][$i]
."\nDamage: ".$info['damage'][$i]
."\nBTH: ".$info['bth'][$i]
."\n\n开发者_高级运维SPECIAL"
."\nHits: ".$info['hits'][$i]
."\nType: ".$info['stype'][$i]
."\nElement: ".$info['selement'][$i]
."\nDamage: ".$info['sdamage'][$i]
."\nBTH: ".$info['sbth'][$i]
."\nRate: ".$info['rate'][$i]
."\n\nDescription\n".$description
."\n".$img
."\n\n\n";
}
}
And here's the code I use to call the function:
<?PHP
displayInfo();
?>
I can't find out what's wrong--it's not a sintax error, page loads without interuption.
Thanks in advance.
You are declaring the $count
and $info
variable outside of your function :
// $info already exists
$count = count($info['level']); // and $count is initialized here
function displayInfo()
{
for ($i=0; $i<$count; $i++)
...
In PHP, a variable declared outside of a function is not visible from inside the function.
If you want your "external" variables to be visible from inside the function, you have to declare them as global
in the function :
$count = count($info['level']);
function displayInfo()
{
global $count, $info;
// $count is now visible ; same for $info
for ($i=0; $i<$count; $i++)
...
But it's generally considered better to pass the variables as parameters to the function : you have to declare them as a parameter :
function displayInfo($count, $info)
{
for ($i=0; $i<$count; $i++)
...
And pass them to the function when calling it :
$count = count(...);
displayInfo($count, $info);
Passing parameters instead of using global variables ensures you know what your functions have access to -- and modify.
Edit : thanks for the note, X-Istence ! Didn't read enough of the given code :-(
$count and $info are declared outside the function and therefore they are not visible within it. You could pass $info into the function and then calculate $count within, like this:
//get info from db, or somewhere
$info = array();
displayInfo($info);
function displayInfo($info)
{
$count = count($info['level']);
//now $count and $info are visible.
}
See http://php.net/manual/en/language.variables.scope.php
Add global
for variables which should be accessed from function, but are not parameters:
function displayInfo()
{
## bad design
global $count, $info;
...
Or pass your array as parameter:
function displayInfo($info)
{
## this is local variable accessable only inside function
$count = count($info['level']);
...
}
The call it
<?php
## don't forget to pass array as parameter
displayInfo($info);
?>
You can either move your $count inside the function, or pass it to the function. Also, your $info array has to be passed into the function as well, or made a global. Here is the function prototype with count and info being passed into the function.
function displayInfo($count, $info)
{
for ($i=0; $i<$count; $i++)
{
// Do stuff
}
}
<?php
$count = count($info['level']);
displayInfo($count, $info);
?>
Indeed, there were no syntax errors, but there were other errors, as was pointed out in other reactions. I highly recommend configuring PHP to show all errors when writing new code. This way you would've seen a notice about $info and $count not being defined inside your function.
You can turn errors on in a couple of ways.
- Configure your server to do it.
- Turn them on by using an .htaccess file
- Use the following PHP code in the very beginning of your script.
Example:
error_reporting(E_ALL | E_NOTICE); //turn on all errors
ini_set("display_errors","On"); //activate display_error
精彩评论