Board index   FAQ   Search  
Register  Login
Board index PHP PHP General

URL variables

General discussions related to php

Moderators: macek, egami, gesf

URL variables

Postby Virtuoso » Mon Jul 21, 2003 7:11 am

how do I get a url to...i dont know how to explain this

i want something like this:

echo '<a href="profile.php?profile=' .$profileuser. '">';

except when i do it, it wont work.
User avatar
Virtuoso
php-forum Active User
php-forum Active User
 
Posts: 268
Joined: Mon Jul 21, 2003 7:05 am

Postby swirlee » Mon Jul 21, 2003 7:19 am

That's exactly how to do it, Virtuoso (and trembolos.. and arpeggios.. sorry).

So if your code looks like this:

Code: Select all
echo '<a href="profile.php?profile=' . $profileuser . '">';


On your profile.php page, you can access the variable using $_GET['profile'].
User avatar
swirlee
Moderator
Moderator
 
Posts: 2272
Joined: Sat Jul 05, 2003 1:18 pm
Location: A bunk in the back

Postby pootergeist » Mon Jul 21, 2003 8:27 am

note though - if $profileuser contains any non-standard characters (such as a space, slash, ampersand etc) you would need to urlencode and decode the var

echo '<a href="profile.php?profile=' . urlencode($profileuser) . '">';

$profile = urldecode($_GET['profile']);
pootergeist
New php-forum User
New php-forum User
 
Posts: 191
Joined: Wed Jan 29, 2003 7:11 am
Location: UK

Postby Virtuoso » Mon Jul 21, 2003 8:55 am

do i HAVE to use $GET_[profile]

is it possible to just use $profile
User avatar
Virtuoso
php-forum Active User
php-forum Active User
 
Posts: 268
Joined: Mon Jul 21, 2003 7:05 am

Postby swirlee » Mon Jul 21, 2003 9:59 am

If you have register_globals in php.ini turned on, then you can just use $profile. In PHP 4.2 and after, register_globals is turned off by default, for very good reason. Using register_globals creates the potential for serious security problems. I'll let you read the article documentation at Zend.com.

I really can't imagine why you'd want to use $profile instead. Aside from saving you a half-dozen keystrokes, it doesn't offer any benefit, and using $_GET['profile'] instead affords you the benefit of increasing code readability, as you can tell at a glance that it's not a regular variable but a variable passed from the browser.
User avatar
swirlee
Moderator
Moderator
 
Posts: 2272
Joined: Sat Jul 05, 2003 1:18 pm
Location: A bunk in the back

Postby WiZARD » Mon Jul 21, 2003 11:45 pm

actually you may use like
Code: Select all
echo '<a href="profile.php?profile=' . $profileuser . '">';
but it posibly if you define $profileuser in that script, where you use
Code: Select all
echo '<a href="profile.php?profile=' . $profileuser . '">';
.....
by the way i recommend use like that(simply):
Code: Select all
echo ("<a href=\"profile.php?profile=" . $profileuser . \">");
User avatar
WiZARD
Moderator
Moderator
 
Posts: 1257
Joined: Thu Jun 20, 2002 10:14 pm
Location: Ukraine, Crimea, Simferopol

Postby swirlee » Tue Jul 22, 2003 7:31 am

WiZ, I'm a little confused by your advice here:

WiZARD wrote:by the way i recommend use like that(simply):
Code: Select all
echo ("<a href="profile.php?profile=" . $profileuser . ">");


First of all, if you're using dot-concatenation, why are you using double quotes? Double-quotes force PHP to parse everything in the string, but if you don't have any variables or escape-characters (e.g. "\n" or "\t") in it, then by using double quotes you're just forcing it to do unnecessary processing. In other words, using single-quotes when you can tends to be marginally faster.

Secondly, why are you recommending that the poster add parentheses around the argument for echo? To quote the documentation:

echo() is not actually a function (it is a language construct) so you are not required to use parentheses with it. In fact, if you want to pass more than one parameter to echo, you must not enclose the parameters within parentheses.


So parentheses is not the recommended usage and, in fact, it probably causes PHP to do an extra bit of unnecessary processing because it has to add another parenthese to the stack (the same is true for print, I believe). And using parentheses also keeps you from using the syntax echo $a, $b;, which I'm told is marginally faster than echo $a . $b;.

Anyway, sorry for all the questions. I was just curious about your recommendations.
User avatar
swirlee
Moderator
Moderator
 
Posts: 2272
Joined: Sat Jul 05, 2003 1:18 pm
Location: A bunk in the back

Postby WiZARD » Thu Jul 24, 2003 4:29 am

Hi swirlee!
I'm don't know how about speed, but as i remember some advanced programmer use exactly double quotas :!:
examples?
Phorum Development Team http://www.phorum.org
Francisco Burzi http://phpnuke.org
User avatar
WiZARD
Moderator
Moderator
 
Posts: 1257
Joined: Thu Jun 20, 2002 10:14 pm
Location: Ukraine, Crimea, Simferopol

Postby swirlee » Thu Jul 24, 2003 5:37 am

WiZARD wrote:Hi swirlee!
I'm don't know how about speed, but as i remember some advanced programmer use exactly double quotas :!:
examples?
Phorum Development Team http://www.phorum.org
Francisco Burzi http://phpnuke.org


Phorum and PHPNuke are indeed good pieces of software, but that doesn't mean that their code is optimal. Never assume that just because someone with a big or popular product does something one way that it's the best way to do it.
User avatar
swirlee
Moderator
Moderator
 
Posts: 2272
Joined: Sat Jul 05, 2003 1:18 pm
Location: A bunk in the back


Return to PHP General

Who is online

Users browsing this forum: No registered users and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.