开发者

php functions variable scopes

开发者 https://www.devze.com 2023-03-22 20:01 出处:网络
I want to get the variable $place from function getaddress and pass开发者_如何学编程 the value to function breakintostrings.It seems i cant get the variable $place.

I want to get the variable $place from function getaddress and pass开发者_如何学编程 the value to function breakintostrings. It seems i cant get the variable $place.

<?php
   $username ="1224hammer";
  getaddress ($username);
  function getaddress($username)
    {
    $place = row['place'];

     function format_address($record)
      {
      function breakintostrings($x,$y,$z)   

      }  
      breakintostrings($place,"$y","$z") 
    }
  ?>


Create the variable $place outside of your getaddress function and import it into your breakintostringsfunction using the global keyword:

$place = '';

function getaddress($username)
{
   $place = row['place'];
}

function breakintostrings($x,$y,$z)   
  global $place;
  // more code
}  


You can get the value by returning the variable $place with the return statement, but please, dont nest your functions. Please


Don't nest your functions like that. Instead, try something like this:

function getaddress($username){
    return row['place']; // What is row?
}
function breakintostrings($address,$y,$z){
    // do whatever
}

$address = getaddress($username);
breakintostrings($address,"$y","$z");
0

精彩评论

暂无评论...
验证码 换一张
取 消