4 Replies - 653 Views - Last Post: 11 October 2009 - 01:07 PM Rate Topic: -----

#1 1cookie  Icon User is offline

  • D.I.C Regular

Reputation: -5
  • View blog
  • Posts: 338
  • Joined: 19-October 06

associative arrays

Posted 11 October 2009 - 12:15 PM

hi
The code below is based on a user login application. The 'fields_login.inc' file provides the form (see screenshot) with the field content by way of associative arrays.
<?php
//fields_login.inc
$fields_1 =   array("fusername" => "User Name",
					"fpassword" => "Password"
				   );
$length_1 =   array("fusername" => "10",
					"fpassword" => "10"
				   );
?>



and 'double_form.inc' is the html providing the look of the form.
//double_form.inc
  foreach($fields_1 as $field => $value)				  
  {
	if(preg_match("/pass/i",$field))							   
	   $type = "password";
	else
	   $type = "text";
	echo "<tr>
			<td id='menu_text'>$value</td><td><input type='$type' name='$field' value='".@$$field."' size='{$length_1[$field]}' maxsize='{$length_1[$field]}'></td>
		</tr>\n";
  }													   
?>



I'm pretty comfortable with most of it, the 'foreach' loops through the array, the preg_match determines which type of field we are dealing with, and then the loop prints out the contents of the arrays basically. One thing that i'm not too sure about though is:
value='".@$$field."'

I mean i know that a '.' is concatination in php and the '@' generally supresses Notices and a '$' immediately in front of a string indicates a variable, but why the extra $? The only thing i can think of is that the '@$' is somehow suppressing output to the screen? But why use a value in the first place then?

Can someone confirm please... :)

Attached image(s)

  • Attached Image


Is This A Good Question/Topic? 0
  • +

Replies To: associative arrays

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4359
  • View blog
  • Posts: 12,176
  • Joined: 18-April 07

Re: associative arrays

Posted 11 October 2009 - 12:25 PM

It is known as a variable variable. That is, $field is resolved to its value and that is used as the name of another variable.

$field = "hello";

$$field will then be the variable $hello.

$newvar = $$field; <--- Results in $newvar being assigned the value in $hello.



I never found it very useful except in select cases but that is how it works. :)
Was This Post Helpful? 1
  • +
  • -

#3 1cookie  Icon User is offline

  • D.I.C Regular

Reputation: -5
  • View blog
  • Posts: 338
  • Joined: 19-October 06

Re: associative arrays

Posted 11 October 2009 - 12:49 PM

View PostMartyr2, on 11 Oct, 2009 - 11:25 AM, said:

It is known as a variable variable. That is, $field is resolved to its value and that is used as the name of another variable.

$field = "hello";

$$field will then be the variable $hello.

$newvar = $$field; <--- Results in $newvar being assigned the value in $hello.



I never found it very useful except in select cases but that is how it works. :)


thanks. :) And in the context of my example, the dots '.' are somehow concatenating the string? And the '@' ??

:)
Was This Post Helpful? 0
  • +
  • -

#4 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4359
  • View blog
  • Posts: 12,176
  • Joined: 18-April 07

Re: associative arrays

Posted 11 October 2009 - 12:59 PM

The @ suppresses the error in case the new formed variable doesn't exist. So back to my example where $$field is the same as $hello, if the variable $hello then didn't exist, the @ would suppress the notice that $hello is undefined. As for the periods, it looks like they are attempting to concatenate single quotes on each side but I don't think it will work that way and instead will print the actual characters like ".@$$field." . Now if they wanted to add single quotes on both side, using our example, they would do $value = "'".@$$field."'"; which would then yield 'test' (assuming the value in $hello was "test").

:)

This post has been edited by Martyr2: 11 October 2009 - 01:00 PM

Was This Post Helpful? 0
  • +
  • -

#5 1cookie  Icon User is offline

  • D.I.C Regular

Reputation: -5
  • View blog
  • Posts: 338
  • Joined: 19-October 06

Re: associative arrays

Posted 11 October 2009 - 01:07 PM

View PostMartyr2, on 11 Oct, 2009 - 11:59 AM, said:

The @ suppresses the error in case the new formed variable doesn't exist. So back to my example where $$field is the same as $hello, if the variable $hello then didn't exist, the @ would suppress the notice that $hello is undefined. As for the periods, it looks like they are attempting to concatenate single quotes on each side but I don't think it will work that way and instead will print the actual characters like ".@$$field." . Now if they wanted to add single quotes on both side, using our example, they would do $value = "'".@$$field."'"; which would then yield 'test' (assuming the value in $hello was "test").

:)


excellent explanation, thankyou Martyr

:)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1