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..
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.
Or use it like global as $var = 'some text'; function func() { global $var; echo 'something '.$var.' more things'; } PHP:
Nope not working i don't know the variable is not getting detected in the function only? It shows undefined variable?
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.
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.
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:
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):
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
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
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:
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.
i tried the same code out side i think it's something else anyways thanks all and yes it works through arguments only
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.
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.
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..