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.

 

 

Link to comment
Share on other sites

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';
  }
}

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.