Jump to content

Making variable avaliable outside function- ?global


meltingpoint

Recommended Posts

I run these functions to find the key of a given element inside an array.  Like so;

$ban_ip_file = file("ip_file.txt");
foreach($ban_ip_file as $key => $value)
{
$value = trim($value);
if($value == $ip_from_form)// passed to page by a form
$set = $key;
}

How do I make $set available for testing and file writing further down the script?

 

Tried assigning global $set; just above the $set=$key; ----but it did not work.

 

 

you can use the return keyword inside the function to return a value!

 

$myVariable = myFunction('Bye');

function myFunction($var){
  switch ($var){
    case 'Hi';
      return 'Hiya!';
      break;
   case 'Bye':
      return 'See you later!';
      break;
   default:
      return 'Sorry, I don\'t understand';
  }
}

My apologies for not being more clear.  It is not in a user defined function.  it is simply run as you see it.  The code above gives the following;

 

Fatal error: Can't use function return value in write context in /home/XXXX/public_html/ban_ip.php on line 31

 

Line 31  follows the above code and is:

if(strlen($set) = "")// line 31
{
echo " There was not a match to the IP address to delete";
exit;
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.