Page 1 of 1

order of operations

Posted: Sun Apr 30, 2006 12:46 am
by franky
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Is there a place were we can view the 'order of operations' for different php commands?

Here's an example of a problem that I have noticed recently.

Let's say you have a function that pulls a store ID from a database, like below.  

NOTE: Please overlook the ineffeciency of the code and accept the concept I'm presenting.

Code: Select all

// Gets Store ID
///////////////////////////////////////////////////////////////////////////
function get_store_id($my_store_name) {
$query = "SELECT * FROM mystores WHERE my_store_name = '".$my_store_name."'";
$result = mysql_query($query);
if (mysql_num_rows($result) > 0 ) {
while ($record = mysql_fetch_assoc($result)) {
	while (list($fieldname,$fieldvalue) = each ($record)) {
	$$fieldname = $fieldvalue;
	}
}
echo $my_store_id;
}
}
When calling this function in a situation where it will print to screen, it works great.

Code: Select all

echo "My Store ID: ";
get_store_id($my_store_name);
echo "<br>";
However, if you would like to use it in one of the following examples, it doesn't return the same information.

Code: Select all

$query = "INSERT INTO my_main_category (my_main_cat,my_store_id) VALUES ('".$my_main_category."','".get_store_id($my_store_name)."')";
.. or ...

Code: Select all

$my_store_id = get_store_id($my_store_name);
Being rather new to PHP, any education on this matter would be most enlightening.
Thanks.


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Sun Apr 30, 2006 12:53 am
by feyd
get_store_id() must return the value to be of use like you wish, not echo it.

Posted: Sun Apr 30, 2006 12:59 am
by franky
thanks for the quick reply, and format intro. Honestly, I forgot all about the 'return' command. Although I haven't seen an example of it in use with PHP persay, I probably should have guessed it.