Jump to content

[SOLVED] => and -> operators


itazev

Recommended Posts

Hey guys!

 

I often have seem these ( => , -> ) operators through php samples on the net but I do not know what they really do and i tried a search but they are so 'small' that it never give the results for them.

particulary i note the use of => on foreachs.

 

what does these operators do actually?

 

cheers!

Link to comment
Share on other sites

They are essentially an association.

 

Usually used in array (IE the foreach).

 

When creating an associative array you use those to define a value at key.

 

IE:

 

$array = array("key1" => "Value1", "Key2" => "Value2");

print $array["key1"]; // will print Value1

foreach ($array as $key => $value) {
     print "Key: " . $key . " is Value: " . $value;
}

 

Hope that helps.

Link to comment
Share on other sites

let's cover one at a time.

'=>' can be used in a couple of different ways. for instance in an associative array like this:

$assoc = array('something' => 'something_else', 'dirka' => 'nation');

 

it can also be used in a foreach loop:

foreach($assoc as $key => $val){
        echo $key ." | ". $val ."<br />\n";
}

 

it can also be used to compare two values:

if($one >= $the_other || $the_other <= $one){ /*do something*/ }

 

 

the '->' operator is generally used in object oriented programming. for instance:

class Foo
{
   var $foo;
   var $bar;
   
   function setBar ($bar)
   {
      $this->bar = $bar;  
   }
   function getBar ()
   {
      return $this->bar;  
   }
}

$obj = new Foo;

$obj->setBar('string');

 

look in my signature under 'oop' for more information on this.

Link to comment
Share on other sites

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.