New function proposal urlappendvar()

php.internals

Juan Alonso

22 years ago
Hello list, I find often myself wanting to append a variable to an unknown url. The problem is that you don't know if you should append the var with ? or with &. Do you think it would be useful to have a builtin function that would do this automatically? example code as I do it actually: <?php $foo = mt_rand(); $separator = (ereg('?', $_SERVER['HTTP_REFERER'])) ? '&' : '?'; $url = $_SERVER['HTTP_REFERER'].$separator.'foo='.$foo; ?> example code with the new version: <?php $foo = mt_rand(); $url = urlappendvar($_SERVER['HTTP_REFERER'], 'foo', $foo); ?> I use it quite frequently so I thought it could be useful to have such functionality builtin. What do you think?
-- Juan Alonso Hernández Desarrollo, Seguridad y Sistemas www.art3mis.com | (34) 91 530 16 33

Rasmus Lerdorf

22 years ago
Generally we don't add functions to replace simple one-line PHP expressions. $url .= strstr($url,'?')?"?foo=$foo":"&foo=$foo"; -Rasmus On Wed, 21 Jan 2004, Juan Alonso wrote:

James Devenish

22 years ago
In message <Pine.LNX.4.58.0401210328110.1105@thinkpad.lerdorf.com> on Wed, Jan 21, 2004 at 03:31:08AM -0800, Rasmus Lerdorf wrote:
> > $url = urlappendvar($_SERVER['HTTP_REFERER'], 'foo', $foo); > > Generally we don't add functions to replace simple one-line PHP > expressions. > > $url .= strstr($url,'?')?"?foo=$foo":"&foo=$foo";
Without wanting to make any comment about the merits of the proposed function, I would have thought there would be merit in having a set of generalised URL-manipulation functions (if someone wanted to write them, that is). For example, the urlappendvar function would clearly handle the syntax of URLs transparently, yet the one-liner requires an author to do The Right Thing in a piecemeal fashion.

Petras Kudaras

22 years ago
James Devenish wrote:
>> $url .= strstr($url,'?')?"?foo=$foo":"&foo=$foo"; >> >> >Without wanting to make any comment about the merits of the proposed >function, I would have thought there would be merit in having a set of >generalised URL-manipulation functions (if someone wanted to write them, >that is). >
Shouldn't that go into separate module (available from PEAR) or something? Sticking as many things into the core as possible seems to be the reason a lot of people don't like PHP ;)
-- Petras Kudaras aka moxliukas

James Devenish

22 years ago
In message <400E821D.8000103@delfi.lt> on Wed, Jan 21, 2004 at 02:43:57PM +0100, Petras Kudaras wrote:
> Shouldn't that go into separate module (available from PEAR) or something? > Sticking as many things into the core as possible seems to be the reason > a lot of people don't like PHP ;)
I don't know what your preferred style is, nor what the "best" API would be, so I don't attempt to make direct stylistic recommendations. However, personally, I don't see why most users would appreciate such features being "hidden" in PEAR (no offence intended!) when other frequently-used string-handling functions are part of the compiled-in modules (e.g. parse_url, urlencode, addslashes, htmlentities, mysql_escape_string, etc.). You may conceive of URLs as objects that should have a class structure in PEAR, but I suspect that manipulating query strings and anchors is a vital procedural task in a vast number of PHP scripts.

Andi Gutmans

22 years ago
At 07:53 PM 1/21/2004 +0800, James Devenish wrote:
>In message <Pine.LNX.4.58.0401210328110.1105@thinkpad.lerdorf.com> >on Wed, Jan 21, 2004 at 03:31:08AM -0800, Rasmus Lerdorf wrote: > > > $url = urlappendvar($_SERVER['HTTP_REFERER'], 'foo', $foo); > > > > Generally we don't add functions to replace simple one-line PHP > > expressions. > > > > $url .= strstr($url,'?')?"?foo=$foo":"&foo=$foo"; > >Without wanting to make any comment about the merits of the proposed >function, I would have thought there would be merit in having a set of >generalised URL-manipulation functions (if someone wanted to write them, >that is). For example, the urlappendvar function would clearly handle >the syntax of URLs transparently, yet the one-liner requires an author >to do The Right Thing in a piecemeal fashion.
I agree that it might be a good idea. There are only two problems: a) The name of the function. b) The timing (we are in a feature freeze right now). I suggest you bring it up again once we release 5.0.0. I'm sure that it has the potential to spawn an intellectual discussion on what the function should look like and other features which are often needed (i.e. it might become something more interesting). Andi

epplestun

22 years ago
I agree with Rasmus, I don´t believe that it is necessary to add a new function that can be solved with a simple line in PHP, another thing would be to be able to make the function your same one and to add it to your PHP. You can add into ext/standard/string.c your function. Iván Rodriguez Espada _________________________ ALAPLAYA.COM http://www.alaplaya.com epplestun@alaplaya.com ----- Original Message ----- From: "Rasmus Lerdorf" <rasmus@php.net> To: "Juan Alonso" <jalonso@art3mis.com> Cc: <internals@lists.php.net> Sent: Wednesday, January 21, 2004 12:31 PM Subject: Re: [PHP-DEV] New function proposal urlappendvar()