开发者

How to get a variable from another function

开发者 https://www.devze.com 2022-12-21 03:05 出处:网络
I am new to PHP and I am trying to create a web mashup with amazon and ebay. My problem is that I have a function called \"printCategoryItems()\" which sets a variable called $keyword. I want to use t

I am new to PHP and I am trying to create a web mashup with amazon and ebay. My problem is that I have a function called "printCategoryItems()" which sets a variable called $keyword. I want to use this variable elsewhere in the code but I can't get it to work. For Example,


<?php
function printCategoryItems(){
    if(isset($_GET['keyword'])){
        $keyword = $_GET['keyword'];
        ...
    }
}
...

$query = $keyword;

...

This is the sort of thing I am trying to do but I end up getting an Undefined variable error for keyword. Is there a way for me to do what I'm trying to do?

Thanks for your help in advance.

(Only have Java Programming Exp开发者_运维知识库erience)


You could use the global keyword in the function, so $keywords inside the function refers to $keywords outside the function :

function printCategoryItems() {
    global $keyword;
    if(isset($_GET['keyword'])){
        $keyword = $_GET['keyword'];
    }
}

printCategoryItems();
var_dump($keyword);

This is because variables inside a function belong to the local-scope of the function, and not the global scope (I haven't done any JAVA for a long time, but I think it's the same in JAVA : a variable declared inside a function is not visible from outside of that function).


But using global variables is generally not a great idea... a better solution would be to have your function return the data ; for instance :

function printCategoryItems() {
    if(isset($_GET['keyword'])){
        return $_GET['keyword'];
    }
}

$keyword = printCategoryItems();
var_dump($keyword);


As a semi-side-note : another solution, still with global variables (not a good idea, again) would be to use the $GLOBALS superglobal array :

function printCategoryItems() {
    if(isset($_GET['keyword'])){
        $GLOBALS['keywords'] = $_GET['keyword'];
    }
}

printCategoryItems();
var_dump($GLOBALS['keywords']);

Here, no need for the global keyword anymore.


And, to finish, you should read the PHP documentation -- especially the part about Functions.


Return the variable from the function

return $keyword;

and assign it when you call the function

$query = printCategoryItems();

In addition, you could declare $query as empty string and pass it to the function by reference, e.g. printCategoryItems(&$query). Or you could wrap your code into a class and make $query an instance variable, so you can set it with $this->query = $keyword.

However, from a function named printCategoryItems(), I wouldn't expect it to set something, but to print something on the screen. You might want to consider the responsibility of the function.


Totally agree with the opposition to global. Whereever you can avoid global, just avoid it. Don't think, but avoid. And in PHP, I cannot think of any scenario, where you could not avoid global.


If you want to access a variable which is defined somewhere else but you want to access it inside and outside of the function, precede it with global keyword:

function printCategoryItems(){
    if(isset($_GET['keyword'])){

        global $keyword = $_GET['keyword'];
        ...
    }
}
0

精彩评论

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