Jump to content

Recommended Posts

I'm still a relative noob at all this, but making some progress, considering I'm self-taught and your help is much appreciated! :D

 

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

Link to comment
https://forums.phpfreaks.com/topic/124943-passing-variables-between-functions/
Share on other sites

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>

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

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.

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.