Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>By default, when you are inside a function, you do not have access to the outer variables.</p> <p><br> If you want your function to have access to an outer variable, you have to declare it as <code>global</code>, inside the function :</p> <pre><code>function someFuntion(){ global $myArr; $myVal = //some processing here to determine value of $myVal $myArr[] = $myVal; } </code></pre> <p>For more informations, see <a href="http://www.php.net/manual/en/language.variables.scope.php" rel="noreferrer"><strong>Variable scope</strong></a>.</p> <p>But note that <strong>using global variables is not a good practice</strong> : with this, your function is not independant anymore.</p> <p><br> A better idea would be to make your function <strong>return the result</strong> :</p> <pre><code>function someFuntion(){ $myArr = array(); // At first, you have an empty array $myVal = //some processing here to determine value of $myVal $myArr[] = $myVal; // Put that $myVal into the array return $myArr; } </code></pre> <p>And call the function like this :</p> <pre><code>$result = someFunction(); </code></pre> <p><br> Your function could also take parameters, and even <strong>work on a parameter passed by reference</strong> :</p> <pre><code>function someFuntion(array &amp; $myArr){ $myVal = //some processing here to determine value of $myVal $myArr[] = $myVal; // Put that $myVal into the array } </code></pre> <p>Then, call the function like this :</p> <pre><code>$myArr = array( ... ); someFunction($myArr); // The function will receive $myArr, and modify it </code></pre> <p>With this :</p> <ul> <li>Your function received the external array as a parameter</li> <li>And can modify it, as it's passed by reference.</li> <li>And it's better practice than using a global variable : your function is a unit, independant of any external code.</li> </ul> <p><br> For more informations about that, you should read the <a href="http://www.php.net/manual/en/language.functions.php" rel="noreferrer"><strong>Functions</strong></a> section of the PHP manual, and,, especially, the following sub-sections :</p> <ul> <li><a href="http://www.php.net/manual/en/functions.arguments.php" rel="noreferrer">Functions arguments</a></li> <li><a href="http://www.php.net/manual/en/functions.returning-values.php" rel="noreferrer">Returning values</a></li> </ul>
    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. 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.
 

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