digitalgravy Posted February 5, 2008 Share Posted February 5, 2008 I wrote a function that will return the search query from the referring url. I will use Google in my example. The function is in a required include file full of functions, it is not behaving like I expect. I need a little help. I am able to get the value I want, but I am not able to return the value. The function if as follows: <?php function searchQuery($urlPart) { // Parse search query from url $fullQuery = $urlPart['query']; parse_str($fullQuery, $queryPart); // Decode query portion of url // q for google and msn - p for yahoo if (isset($queryPart['q'])) { $query = $queryPart['q']; echo $query; // Added to see if $query is what I expect, will remove when I am able to return on next line return $query; } elseif (isset($queryPart['p'])) { $query = $queryPart['p']; echo $query; // Added to see if $query is what I expect, will remove when I am able to return on next line return $query; } } ?> I do a function call as follows: <?php require($_SERVER["DOCUMENT_ROOT"] . '/functions.php'); $referrer = "http://www.google.com/search?q=Test+Query%2C+Test&sourceid=navclient-ff&ie=UTF-8&rlz=1B3GGGL_enUS221US221"; $urlPart = parse_url($referrer); searchQuery($urlPart); echo $query; ?> The above code gives me: The echoed query from the function "Test Query, Test" and Notice: Undefined variable: query in /home/path/to/test.php on line 8 I am not sure what I am doing wrong. I require the functions include I define the variable I call the function and pass the variable as an argument I try and use the returned value. Am I missing something? Am I not understanding variable scope? Thanks in advance. Link to comment https://forums.phpfreaks.com/topic/89503-solved-functions-and-variable-scope/ Share on other sites More sharing options...
Aureole Posted February 5, 2008 Share Posted February 5, 2008 You could try something like... [code <?php function searchQuery($urlPart) { global $query; // etc... } ?> ...but apparently, "globals are bad". Try it though. Link to comment https://forums.phpfreaks.com/topic/89503-solved-functions-and-variable-scope/#findComment-458459 Share on other sites More sharing options...
Barand Posted February 5, 2008 Share Posted February 5, 2008 you need to assign the value returned by the query $query = searchQuery($urlPart); echo $query; Link to comment https://forums.phpfreaks.com/topic/89503-solved-functions-and-variable-scope/#findComment-458475 Share on other sites More sharing options...
digitalgravy Posted February 5, 2008 Author Share Posted February 5, 2008 ...but apparently, "globals are bad". Try it though. Yes, I am staying away from using global. you need to assign the value returned by the query $query = searchQuery($urlPart); echo $query; Thanks Barand, that solved it! Link to comment https://forums.phpfreaks.com/topic/89503-solved-functions-and-variable-scope/#findComment-458752 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.