0

I have found this command written in PHP, but I cannot understand what it does... I have seen an introduction to arrays for PHP, but still, I can't figure this out.

$from=1;
$A['from'] = $A[$from];

What is the programmer here doing? I mean, how he can assign $A[$from] somewhere, since $A[$from] has not get any values (array A is not used before). And what this command does?

Jim Blum
  • 2,656
  • 5
  • 28
  • 37
  • If the $A is not yet initialized, then programmers are not doing anything at all. I suspect this is only to help you get in touch with assoc vs vars names when they're same. –  May 25 '14 at 03:31
  • If `$A` hasn't been defined before, it looks like bad code. There isn't any fancy trickery going on here. The value of `$A[1]` is being inserted into `$a['from']`. If `$A[1]` isn't set, this will result in an error (Errr...notice). – Fluffeh May 25 '14 at 03:32
  • It won't result in an error, it will result in a notice: undefined index. – Popnoodles May 25 '14 at 03:33
  • @Popnoodles Picky picky :) I would consider anything that I didn't intend the user to see as an error - notices, warnings, errors. – Fluffeh May 25 '14 at 03:34
  • @Fluffeh It's not picky. In a dev vs production environment an error may prevent an end user from doing something, but a notice will only show to developers and not stop the code dead. There's a rather large difference between the two. – Popnoodles May 25 '14 at 03:36
  • Many thanks for your comments.! He also has this error_reporting(0); ini_set('display_errors',0); above of his code... is this just to avoid this kind of errors? – Jim Blum May 25 '14 at 03:37
  • Avoiding errors produces bad code. Jim can you show us the code from the point where `$A` is defined, something like `$A = array();` – Popnoodles May 25 '14 at 03:38
  • it is not defined at all. It is used first time there... – Jim Blum May 25 '14 at 03:40
  • And that file isn't included by another file that DOES define it? If not, and you're sure it's not it should read `$A = array('from' => 1);` You were right to question it. – Popnoodles May 25 '14 at 04:52

1 Answers1

2

This code mean

You have variable form have value = 1 and you have array with key that's mean

$array['key']=1

that was example

with your code

$form = 1 // this is variable have value = 1 $A['form']=$a[$form] // give key form value of variable form it's mean

echo $A['form']; // it will show you 1 witch is value of variable $form .

Mr.Geeker
  • 113
  • 1
  • 1
  • 8