php variable and function problem its URGENT

Discussion in 'PHP' started by Kload, Feb 7, 2011.

  1. #1
    Ok now i have a small problem which is kinda weird
    now i have variable named $var
    no if i am calling it in the next line it works fine gets displayed and all but i want to add it in a function like
    function func(){
    echo'some text'.$var.'some text';
    }
    now i cannot use the "" codes for some reason i can only use '' for this and i use dreamweaver it pops up the variable even when i try to use it just in the next line of declaration but not within the function what is happening?
    please help..
     
    Kload, Feb 7, 2011 IP
  2. mastermunj

    mastermunj Well-Known Member

    Messages:
    687
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    110
    #2
    Variables defined outside functions are not directly available within function.
    1. Either you pass the variable in function as parameter.
    2. OR access the variable with global identifier.

    Best is to pass as parameter.
     
    mastermunj, Feb 7, 2011 IP
  3. ipr22

    ipr22 Peon

    Messages:
    113
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Yep, just pass it in like this:

    function func($var){
    echo 'some text'.$var.'some text';
    }
     
    ipr22, Feb 7, 2011 IP
  4. swashata

    swashata Member

    Messages:
    86
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    43
    #4
    Or use it like global as
    $var = 'some text';
    function func() {
    global $var;
    echo 'something '.$var.' more things';
    }
    PHP:
     
    swashata, Feb 7, 2011 IP
  5. Kload

    Kload Peon

    Messages:
    17
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Nope not working i don't know the variable is not getting detected in the function only?
    It shows undefined variable?
     
    Last edited: Feb 7, 2011
    Kload, Feb 7, 2011 IP
  6. swashata

    swashata Member

    Messages:
    86
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    43
    #6
    Can you show the snippet of the function where it is not working? Because, it should work for declaring a global $var or passing it as argument.
     
    swashata, Feb 7, 2011 IP
  7. Kload

    Kload Peon

    Messages:
    17
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Ok i am fetching data from my database
    
    // Recovering exact data.
    $keywords=$query[keywords];
    $description=$query[description];
    
    
    // Displaying meta keywords and description.
    function func(){
    	echo'<meta name="keywords" content="">
    <meta name="description" content="">';
    }
    PHP:
    no i am trying to call the keyword like content="'.$keyword.'" and description="'.$description.'" but non of them works they don't even get detected in the function only but the are detected out side.
     
    Kload, Feb 7, 2011 IP
  8. mastermunj

    mastermunj Well-Known Member

    Messages:
    687
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    110
    #8
    Try following..

    
    // Recovering exact data.
    $keywords=$query[keywords];
    $description=$query[description];
    
    
    // Displaying meta keywords and description.
    function func()
    {
        global $keywords, $description;
        //Below you can directly use both variables now.
        echo'<meta name="keywords" content="">
        <meta name="description" content="">';
    }
    
    PHP:
     
    mastermunj, Feb 7, 2011 IP
  9. gerulis

    gerulis Active Member

    Messages:
    30
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    63
    #9
    Or you can try this:

    // Recovering exact data.
    $keywords=$query[keywords];
    $description=$query[description];
    
    
    // Displaying meta keywords and description.
    function func($keywords, $description)
    {
        //Below you can directly use both variables now.
        echo'<meta name="keywords" content="">
        <meta name="description" content="">';
    }
    Code (markup):
     
    gerulis, Feb 8, 2011 IP
  10. Kload

    Kload Peon

    Messages:
    17
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Guys i had already tried passing it through the arguments it won't work i just don't know somehow its not getting detected in the function only
     
    Kload, Feb 8, 2011 IP
  11. Kload

    Kload Peon

    Messages:
    17
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    not only this but all the variables are not detected in the function
     
    Kload, Feb 8, 2011 IP
  12. Grit.

    Grit. Well-Known Member

    Messages:
    1,424
    Likes Received:
    22
    Best Answers:
    1
    Trophy Points:
    110
    #12
    Have you ensured that your variables have data within them? Try the above, but instead of using array data, try it with some static data, and if it works, there is an issue with your array
     
    Grit., Feb 8, 2011 IP
  13. Christian Little

    Christian Little Peon

    Messages:
    1,753
    Likes Received:
    80
    Best Answers:
    0
    Trophy Points:
    0
    #13
    Have you actually called the function somewhere in your code? I.e. you have this line somewhere outside of the function:


    func($keywords, $description);
    PHP:
     
    Christian Little, Feb 8, 2011 IP
  14. Kload

    Kload Peon

    Messages:
    17
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #14
    Ok i'll try using static variables and no i have used the normal function way...
     
    Kload, Feb 8, 2011 IP
  15. buddyborg

    buddyborg Guest

    Messages:
    36
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #15
    Swashata already answered this question. the way to bring external variables into PHP is by calling them in as global within the function. i look at the code posted and it looks like his is the most accurate.
     
    buddyborg, Feb 9, 2011 IP
  16. Kload

    Kload Peon

    Messages:
    17
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #16
    i tried the same code out side i think it's something else anyways thanks all and yes it works through arguments only
     
    Kload, Feb 9, 2011 IP
  17. Kload

    Kload Peon

    Messages:
    17
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #17
    Ok my bad still no luck not working through arguments nor through global variables its just not getting detected in the function only not in the arguments in the () nor in the {} as global.
     
    Kload, Feb 9, 2011 IP
  18. ThomasTwen

    ThomasTwen Peon

    Messages:
    113
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #18
    What do you mean by "not getting detected"? Does it give you an error message or does it just not output your variables?


    Also, please show us the exact code you are using. The thing you copied above can not possibly work, because $keywords and $description are never used inside the function you posted above.
     
    ThomasTwen, Feb 9, 2011 IP
  19. Kload

    Kload Peon

    Messages:
    17
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #19
    aa really sorry guys let me apologize well it worked actually it was a problem from my wamp only anyways yes its works through passing the variable as global like.

    function foo(){
    global $var;
    }

    well thanks for your time and sorry for wasting it..
     
    Kload, Mar 26, 2011 IP