Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>In reference to</p> <pre><code>$connect2db = mysqli_connect('values here'); </code></pre> <p>you mention:</p> <blockquote> <p>$connect2db equals to "those commands" but nothing is calling it. all $connect2db equals to literally is the instruction to take once its called into play.</p> </blockquote> <p>That is <strong>not true.</strong> <code>mysqli_connect</code> is a function, and using that syntax (<code>functionname(argument)</code>) actually <em>calls that function</em>. What <code>$connect2db</code> is used for is to store the return value from that function.</p> <p>To sum up what goes on on that line:</p> <ul> <li>a call to an existing function called <code>mysqli_connect</code> is made. This <strong>executes</strong> the function.</li> <li>the return value from that function gets stored in a variable named <code>$connect2db</code></li> </ul> <p>The rules governing this assignment are defined by <a href="http://www.php.net/manual/en/language.operators.precedence.php" rel="nofollow">operator precedence</a>, which in this case specifies that the function call will be evaluated prior to the assignment.</p> <p>To know what the type of return that is, one must look at the API documentation for this function, and in this case it is an object (which represents the connection to the server).</p> <hr> <p>Now it is possible to have a variable contain a function, like what you thought your example meant. The syntax of this - called an anonymous function, would be like so:</p> <pre><code>$greet = function($name) { printf("Hello %s\r\n", $name); }; </code></pre> <p>You can see that instead of the format:</p> <blockquote> <p>functionname + parenthesis + argument(s) + parenthesis + semicolon</p> </blockquote> <p>the format is:</p> <blockquote> <p>function + parenthesis + argument(s) + parenthesis + open bracket + function definition + close bracket + semicolon</p> </blockquote> <p>In this case, the variable holds what the function does, the function has not beem executed, and to execute it you would use the first format:</p> <pre><code>$greet('World'); </code></pre> <p>Note that this format is available in PHP 5.3 only, prior to that one needed to use <a href="http://php.net/manual/en/function.create-function.php" rel="nofollow"><code>create_function</code></a>.</p> <hr> <p>Relevant links:</p> <ul> <li><a href="http://www.php.net/manual/en/language.variables.php" rel="nofollow">Variables</a></li> <li><a href="http://php.net/manual/en/language.functions.php" rel="nofollow">Functions</a></li> <li><a href="http://php.net/manual/en/functions.anonymous.php" rel="nofollow">Anonymous functions</a></li> <li><a href="http://php.net/manual/en/function.create-function.php" rel="nofollow"><code>create_function</code></a></li> <li><a href="http://www.php.net/manual/en/language.operators.precedence.php" rel="nofollow">Operator precedence</a></li> </ul>
 

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