Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You could use the <a href="http://fr2.php.net/manual/en/language.variables.scope.php" rel="noreferrer"><code>global</code></a> keyword in the function, so <code>$keywords</code> inside the function refers to <code>$keywords</code> outside the function :</p> <pre><code>function printCategoryItems() { global $keyword; if(isset($_GET['keyword'])){ $keyword = $_GET['keyword']; } } printCategoryItems(); var_dump($keyword); </code></pre> <p>This is because variables inside a function belong to the local-scope of the function, and not the global scope <em>(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)</em>.</p> <p><br> But <strong>using global variables is generally not a great idea</strong>... a better solution would be to have your function <a href="http://php.net/return" rel="noreferrer"><code>return</code></a> the data ; for instance :</p> <pre><code>function printCategoryItems() { if(isset($_GET['keyword'])){ return $_GET['keyword']; } } $keyword = printCategoryItems(); var_dump($keyword); </code></pre> <p><br> As a semi-side-note : another solution, still with global variables <em>(not a good idea, again)</em> would be to use the <a href="http://fr2.php.net/manual/en/reserved.variables.globals.php" rel="noreferrer"><code>$GLOBALS</code> superglobal array</a> :</p> <pre><code>function printCategoryItems() { if(isset($_GET['keyword'])){ $GLOBALS['keywords'] = $_GET['keyword']; } } printCategoryItems(); var_dump($GLOBALS['keywords']); </code></pre> <p>Here, no need for the <code>global</code> keyword anymore.</p> <p><br> And, to finish, you should read the PHP documentation -- especially <a href="http://php.net/manual/en/language.functions.php" rel="noreferrer">the part about Functions</a>.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload