SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Evangelist
    Join Date
    Apr 2004
    Location
    Boston
    Posts
    482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    passing variable values inside and outside of functions

    Hello,

    I having trouble passing data which comes from outside a functions through said function.

    For example I have an array:

    PHP Code:
    $s[1] = 15;
    $s[2] = 30;
    $s[3] = 50
    and I have a function:

    PHP Code:
    function size($s){
    $i=0;
    foreach (
    $s as $size){
    $i++;
    if(
    $size == 15){
    $val 'small';
    }elseif(
    $size == 30){
    $val 'medium';
    }else{
    $val 'large';
    }
    }
    return array(
    $val);

    It doesn't seem to pass the value of $s into the function. (the values of $s actually come from a query)

    Thanks

  2. #2
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2006
    Location
    Augusta, Georgia, United States
    Posts
    4,194
    Mentioned
    17 Post(s)
    Tagged
    5 Thread(s)
    PHP Code:
    function size($s){

        
    $val = array();
        
        foreach (
    $s as $key=>$size){

            if(
    $size == 15){

                
    $val[$key] = 'small';

            } else if(
    $size == 30){

                
    $val[$key] = 'medium';

            } else {

                
    $val[$key] = 'large';

            }

        }

        return 
    $val;



  3. #3
    SitePoint Evangelist
    Join Date
    Apr 2004
    Location
    Boston
    Posts
    482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by oddz View Post
    $result = size($s);

    That doesn't yield the expected result?

    You can't call size() and expect the variable be passed. You need to actually pass it when calling the function. Is that what your doing?
    No, that is what I am asking, how can i call a variable from outside the function into the function, in this case it's an array created by a database query.

  4. #4
    SitePoint Enthusiast
    Join Date
    Apr 2009
    Posts
    63
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You use the global reference to call a variable inside a function that exists out site of it.

    PHP Code:
    function size(){
    global 
    $s;
    $i=0;
    foreach (
    $s as $size){
    $i++;
    if(
    $size == 15){
    $val 'small';
    }elseif(
    $size == 30){
    $val 'medium';
    }else{
    $val 'large';
    }
    }
    return array(
    $val);


  5. #5
    SitePoint Evangelist
    Join Date
    Apr 2004
    Location
    Boston
    Posts
    482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by atlantadental View Post
    You use the global reference to call a variable inside a function that exists out site of it.

    PHP Code:
    function size($s){
    global 
    $s;
    $i=0;
    foreach (
    $s as $size){
    $i++;
    if(
    $size == 15){
    $val 'small';
    }elseif(
    $size == 30){
    $val 'medium';
    }else{
    $val 'large';
    }
    }
    return array(
    $val);

    Thank you.

  6. #6
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by atlantadental View Post
    You use the global reference to call a variable inside a function that exists out site of it.
    The OP shouldn't need to do that, they are passing it as a function parameter already...hopefully...
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  7. #7
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Can you show us your full code?

    Well, the whole function and the code from the query until past where you call the function.
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona

  8. #8
    SitePoint Evangelist
    Join Date
    Apr 2004
    Location
    Boston
    Posts
    482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by arkinstall View Post
    Can you show us your full code?

    Well, the whole function and the code from the query until past where you call the function.
    that basically is all the code the only difference is the database query which produces the original array $size. If i print_r($size) outside the function it displays the correct values for the array otherwise it's blank. The error is telling me Invalid argument supplied for foreach()

  9. #9
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Quick example...

    PHP Code:
    <?php
    $aSizes 
    = array(153050);

    function 
    getSizeDescription($iSize)
    {
        if(
    $iSize <= 15)
        {
            return 
    'Small';
        }
        elseif(
    $iSize <= 30)
        {
            return 
    'Medium';
        }
        elseif(
    $iSize >= 50)
        {
            return 
    'Large';
        }
        else
        {
            return 
    'Unknown';
        }
    }

    echo 
    getSizeDescription($aSizes[0]) #Small
    ?>
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  10. #10
    SitePoint Evangelist
    Join Date
    Apr 2004
    Location
    Boston
    Posts
    482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by SilverBulletUK View Post
    Quick example...

    PHP Code:
    <?php
    $aSizes 
    = array(153050);

    function 
    getSizeDescription($iSize)
    {
        if(
    $iSize <= 15)
        {
            return 
    'Small';
        }
        elseif(
    $iSize <= 30)
        {
            return 
    'Medium';
        }
        elseif(
    $iSize >= 50)
        {
            return 
    'Large';
        }
        else
        {
            return 
    'Unknown';
        }
    }

    echo 
    getSizeDescription($aSizes[0]) #Small
    ?>
    The problem with this is that there are not always going to be a set amount of keys. It could loop over and over depending on data because it's dynamic.

    The array could actually be (small, large, medium, medium, large, small, small, large, large, small, medium, small, small). I'm sure you get the point.

  11. #11
    SitePoint Evangelist
    Join Date
    Apr 2004
    Location
    Boston
    Posts
    482
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've actually tweaked it a little with some progress but now i am getting a totally different error. I've also changed some of the variable and array names and added a second part to the function and some math.

    Here is the actual code outside of the queries:

    PHP Code:
    //array data (this is true)
    //sizes
    $cirsz[1] =  'small';
    $cirsz[2] =  'medium';
    $cirsz[3] =  'large';

    //colors
    $circlr[1] = 23;
    $circlr[2] = 20;
    $circlr[3] = 18;

    //function

    function Settings($cirsz$circlr) {

    global 
    $cirsz
    global 
    $circlr
        
    // This callback will adjust the fill color and size of
        // the datapoint according to the data value according to
    $z 0;
    //get size
    foreach($cirsz as $s){
    $z++;
    if(
    $s == 'small'){
    $sz 15;
    }elseif(
    $s == 'medium'){
    $sz 30;
    }else{
    $sz 50;
    }

    }
    //get colors

    foreach ($circlr as $c){
    $z++;
    if(
    $c <= 25 && ($c >= 18.75)){
    $col "red";
    }elseif(
    $c 18.75 && ($c >= 12.5)){
    $col ="orange";
    }elseif(
    $c 12.5 && ($c >= 6.25)){
    $col "yellow";
    }else{
    $col "green";
    }

    }
        return array(
    $sz""$col);

    If I echo $col and $sz I get the proper values however it's returning this error:

    Warning: Missing argument 2 for Settings()

    It's also looping three times over, once for each datapoint in the array.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •