order of operations

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
franky
Forum Newbie
Posts: 2
Joined: Sun Apr 30, 2006 12:35 am

order of operations

Post 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]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

get_store_id() must return the value to be of use like you wish, not echo it.
franky
Forum Newbie
Posts: 2
Joined: Sun Apr 30, 2006 12:35 am

Post 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.
Post Reply