easy question on variables

Links for php scripts

Moderators: macek, egami, gesf

icu90ucme
New php-forum User
New php-forum User
Posts: 23
Joined: Mon Jul 07, 2003 2:29 pm

easy question on variables

Postby icu90ucme » Wed Aug 06, 2003 9:23 am

im trying to pass this variable through this link

Code: Select all

<td>Suspended <a href="take_off_suspension.php?username=$usr[username]">Take off</a></td>


on the take_off_suspension.php page i have

Code: Select all

  $username = $HTTP_GET_VARS['username'];
  echo $username;


the result that is echo'd is "$usr[username" instead of the actual value... does anybody know a fix

User avatar
swirlee
Moderator
Moderator
Posts: 2272
Joined: Sat Jul 05, 2003 1:18 pm
Location: A bunk in the back
Contact:

Postby swirlee » Wed Aug 06, 2003 10:11 am

First, of all, you're calling your array element wrong. When specifying an associative index, the index must be specified as a string, e.g. between quotation marks:

Code: Select all

<?
   $arr = array('a' => 1, 'b' => 2);

   // this is very wrong:
   echo $arr[a];

   // this is correct:
   echo $arr['a'];   
?>


Doing it without the quotes still works, but it's deprecated functionality and should never be done. You can read about it in the "Array do's and don'ts" section on this page.

Secondly, I think your problem is that you're trying to access a variable from outside PHP. PHP ignores everything that isn't between <? and ?> tags. So, instead of this:

Code: Select all

<a href="take_off_suspension.php?username=$usr[username]">Take off</a>


You want this:

Code: Select all

<a href="take_off_suspension.php?username=<? echo $usr['username']; ?>">Take off</a>


Or you can use the short syntax:

Code: Select all

<a href="take_off_suspension.php?username=<?= $usr['username'] ?>">Take off</a>


Return to “PHP Scripts”

Who is online

Users browsing this forum: No registered users and 0 guests

cron