Jump to content

[SOLVED] Newby question: what does "=>" do?


physaux

Recommended Posts

Hmm interesting thanks for the replies.

It wasnt the "greater than or equal" mistake lol, but thanks for considering all the possibilities :P

 

Ok, so I am trying to understand it. Here is a sample code where it is used. Could someone clear up for me what exactly is going on?

 

<?php
$employees [] = array(
  'name' => 'Albert',
  'age' => '34',
  'salary' => "$10000"
  );
  $employees [] = array(
  'name' => 'Claud',
  'age' => '20',
  'salary' => "$2000"
  );
  
  $doc = new DOMDocument();
  $doc->formatOutput = true; 
?>

 

I can see that the first "=>" assigns an array, ok thats cool.

But what is the second, "->" part do??

 

Thank you!

Hopefully this will help you physaux: by doing this:

 

$employees [] = array(
  'name' => 'Albert',
  'age' => '34',
  'salary' => "$10000"
);

 

You are creating an associative array. What that means is instead of having an array with 0,1,2 as the index's the index's are what you set here. For example using the array above you could access the fields like this:

 

$employees['name'] //would equal Albert
$employees['age'] //would equal 34
$employees['salary'] //would equal $10000

//without named indexes it would be
$employees[0] //would equal Albert
$employees[1] //would equal 34
$employees[2] //would equal $10000

 

Now for this part:

 

  $doc = new DOMDocument();
  $doc->formatOutput = true; 

 

What you are using there is a class. When you do $doc = new DOMDocument(); you are creating an instance of that class. Then $doc->formatOutput is setting a variable in that class. so for instance the class may look like this:

 

class DOMDocument{
   public formatOutput = false;

   public function helloWorld(){
        echo 'hello world';
   }
}

 

So by doing $doc->formatOutput it is then setting that to true instead. You can also access functions like that as well. So using the example above if you were to do this $doc->helloWorld(); it would output "hello world". All that is doing is making a reference to an object of that class. Hopefully that will clear some stuff up.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.