RE: [PHP] Function -> Sending URL's

From: Date: Thu, 11 Jan 2001 22:07:39 +0000
Subject: RE: [PHP] Function -> Sending URL's
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-34110@lists.php.net to get a copy of this message
Ok, what I would do is either pass in an array of key/value pairs OR pass in 2 variables, the key name and the value. $keyValuePairs["name"]="Company1"; $keyValuePairs["address"]="123 Main Street"; $keyValuePairs["city"]="Anytown"; $newURL = testPassVar("http://www.calevans.com",$keyValuePairs) function testPassVar($url, $keyValuePairs) { /* * There here in the function you unwind the array and add each to the URL in the form of * $thisKey=$keyValuePairs[$thiskey] * */ return $url } // function testPassVar($url, $keyValuePairs) A simpler method is to pass in a single key and value and let the function add them and return you the newly munged URL. function testPassVar($url, $key, $value) { //won't work if it's the first pair. need to test for that! return $url."&".$key."=".$value; } $newURL = testPassVar("http://www.calevans.com?test=one","name","Cal"); Cal http://www.calevans.com -----Original Message----- From: Abe [mailto:abe@fish.tm] Sent: Thursday, January 11, 2001 3:51 PM To: Cal Evans; php-general@lists.php.net Subject: Re: [PHP] Function -> Sending URL's Hey Cal - this makes sense but the thing is that different parts of the site will call such a function and they will all want different variables attached to the end - I could do it some other way - but I wanted to know if there was some way of doing it by sending the whole URL(incl. the variables) and letting them be dealt with by the function. Thanks, Abe ----- Original Message ----- From: "Cal Evans" <cal@calevans.com> To: <php-general@lists.php.net> Sent: Thursday, January 11, 2001 9:40 PM Subject: RE: [PHP] Function -> Sending URL's > There's a couple points here: > > 1: echo isn't what you want to do. You want to RETURN the value from you > function. Try this: > <? > function testPassVar($url) { > $name = "TEST"; > echo "<a href=\"$url\">Click</a>"; > return "<a href=\"$url?name=$name\">Click</a>"; > } > > $url = 'asdfasdf.php3'; > $newURL = testPassVar($url); > > echo($newURL); > > echo "This is a $name"; > ?> > > 2: in the last line, you try to echo $name. $name is defined within the > function testPassVar() and goes out of scope when that function is done At > this point in your script, $name does not exist. > > Hope this helps, > Cal > > http://www.calevans.com > > -----Original Message----- > From: Abe [mailto:abe@fish.tm] > Sent: Thursday, January 11, 2001 3:35 PM > To: php-general@lists.php.net > Subject: [PHP] Function -> Sending URL's > > > Hey there, > this is a strange one - I want to send a URL to a function that includes > varibles. Those variables should be taken from within the function - as in > the example below the link I want is: > > asdfasdf.php3?name=TEST , but that is not what I get as you can see. > > Does anybody know a way around this - The example is simpler than what I am > actually doing and the value of $company must come from the variable. > > Thanks, > Abe > > > > <? > function testPassVar($url) { > $name = "TEST"; > echo "<a href=\"$url\">Click</a>"; > } > > $url = 'asdfasdf.php3?name=$company'; > testPassVar($url); > echo "This is a $name"; > ?> > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net > For additional commands, e-mail: php-general-help@lists.php.net > To contact the list administrators, e-mail: php-list-admin@lists.php.net > > > > -- > PHP General Mailing List (http://www.php.net/) > To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net > For additional commands, e-mail: php-general-help@lists.php.net > To contact the list administrators, e-mail: php-list-admin@lists.php.net >

« previous php.general (#34110) next »