Page 1 of 1

Why can functions be executed using variables?

Posted: Tue Sep 28, 2010 9:58 pm
by iliekphp
I'm aware that this works, but I don't quite understand how. I'm kind of new to programming in general so forgive me if this is some basic concept.

So for example:


<?php

$dbc = mysqli_connect('localhost', 'root', '', ''databasename')
or die("Can't connect");

$query = "INSERT INTO tablename (value, value2, value3)
VALUES ('value', 'valuee', 'valueee');";

$result = mysqli_query($dbc, $query)
or die("Cannot query database");

mysqli_close($dbc);

?>
why does simply declaring the mysqli_query function in a variable make it work? Why does this function get parsed just for being in a variable? Also does this work on all functions? Thanks.

Re: Why can functions be executed using variables?

Posted: Tue Sep 28, 2010 9:59 pm
by John Cartwright
You do not need to capture the return value of the function.

Code: Select all

mysqli_query($dbc, $query)or die("Cannot query database");
Would have worked just the same.

In your case, you it is usually a good practice to check the return values of query operations, to 1) make sure they ran correctly 2) furthur processes using that query, such as with mysqli_num_rows requires the resource returned from the query.

Re: Why can functions be executed using variables?

Posted: Tue Sep 28, 2010 10:34 pm
by requinix
Preeetty sure you got your terminology wrong. There are no variable functions in that code. No "declaring the mysqli_query function in a variable" or "function get parsed" in there. Just regular function calls.

Re: Why can functions be executed using variables?

Posted: Tue Sep 28, 2010 11:07 pm
by califdon
Saying the same thing in different words, this line of code:

Code: Select all

$result = mysqli_query($dbc, $query) or die("Cannot query database");
is simply assigning the value that is returned by the mysqli_query() function to the variable $result, or if the function returns the value False, it calls the die() function.