richrock Posted September 19, 2008 Share Posted September 19, 2008 I'm still a relative noob at all this, but making some progress, considering I'm self-taught and your help is much appreciated! Now I'm onto creating functions, which is pretty cool. What I'd like to do is have two functions with interchangable values - function getItem () { This function will enter the information and store it. This one needs to send current id number to the prevItem func... } function prevItem () { This function shows all the items in a category that have been changed. This one needs the getItem id number, and list it. Then send it back so that I can get the next id number. } Unless there's another way of doing this? R Quote Link to comment https://forums.phpfreaks.com/topic/124943-passing-variables-between-functions/ Share on other sites More sharing options...
.josh Posted September 19, 2008 Share Posted September 19, 2008 getItem($id) { // some code here $x = prevItem($id); // do something with $x } prevItem($id) { // use $id here return $something; } getItem($id); You mean like that? Quote Link to comment https://forums.phpfreaks.com/topic/124943-passing-variables-between-functions/#findComment-645580 Share on other sites More sharing options...
discomatt Posted September 19, 2008 Share Posted September 19, 2008 Your best bet in this case is to get into classes. <pre><?php class inventory { private $id = FALSE; public function getItem( $id ) { if ( ctype_digit((string)$id) === FALSE ) return FALSE; $this->id = $id; echo "\$this->id has been set to $id\n"; return TRUE; } public function prevItem() { if ( $this->id === FALSE ) { echo "No ID to pass quite yet\n"; return FALSE; } echo "The ID passed from getItem() is {$this->id}\n"; return TRUE; } } $obj = new inventory; $obj->prevItem(); $obj->getItem( 55 ); $obj->prevItem(); ?></pre> Quote Link to comment https://forums.phpfreaks.com/topic/124943-passing-variables-between-functions/#findComment-645586 Share on other sites More sharing options...
Garethp Posted September 19, 2008 Share Posted September 19, 2008 There is another way. Have session_start(); at the top of the page, and use this func a() { $var = something; $_SESSION['var'] = $var; } func b() { $varb = $_SESSION['var']; echo $varb; } Quote Link to comment https://forums.phpfreaks.com/topic/124943-passing-variables-between-functions/#findComment-645587 Share on other sites More sharing options...
richrock Posted September 19, 2008 Author Share Posted September 19, 2008 Thanks for the quick responses!!! I'm not at all sure how classes work, liek I mentioned, I'm teaching myself as I go along. I'll look into that. The basics of what I'm doing is to have a form at the top of a page with id num ($id), some info to fill in. Click save, the form is saved to the DB and then listed underneath the form. So all the items are listed underneath, and the next item ($id) is in the top form. I'll give these examples you posted a try and see how it goes. Gotta love this php stuff! R Quote Link to comment https://forums.phpfreaks.com/topic/124943-passing-variables-between-functions/#findComment-645597 Share on other sites More sharing options...
.josh Posted September 19, 2008 Share Posted September 19, 2008 There is another way. Have session_start(); at the top of the page, and use this func a() { $var = something; $_SESSION['var'] = $var; } func b() { $varb = $_SESSION['var']; echo $varb; } If you really want to go that route then you might as well just declare a global var, instead. But using global vars (including session vars, since they are global) in different scopes like that is bad programming practice. Not to mention that the whole point of session vars is to make data persist across pages. There's no reason to use a session var for one page like that. It's like creating an array when you're only going to use 1 element of it. It's overkill. The better route would indeed be to use a class. Quote Link to comment https://forums.phpfreaks.com/topic/124943-passing-variables-between-functions/#findComment-645601 Share on other sites More sharing options...
discomatt Posted September 19, 2008 Share Posted September 19, 2008 As Voilent said, you should avoid using this method, but if you're careful, this will work -> <pre><?php function setValue( $id ) { $GLOBALS['id'] = $id; } function getValue() { echo "\$id is {$GLOBALS['id']}"; } setValue( 14 ); getValue(); echo "\nor alternately, you can just echo \$id: $id"; ?></pre> A better way to do with though, would be to pass by reference. <pre><?php function add15( &$id ) { $id += 15; } function echoValue( $id ) { echo "\$id is equal to $id"; } $int = 10; echo "\$int starts off at $int\n"; add15( $int ); echo "\$int is now $int\n"; # We can now pass it to other functions echoValue( $int ); ?></pre> Or use returns, as shown by Violent. Quote Link to comment https://forums.phpfreaks.com/topic/124943-passing-variables-between-functions/#findComment-645607 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.