Jump to content

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


Recommended Posts

It can be used either to declare elements of array

http://www.php.net/manual/en/language.types.array.php

 

Or to declare key and value variables when using foreach

http://www.php.net/manual/en/control-structures.foreach.php

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.