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
https://forums.phpfreaks.com/topic/46612-solved-and-operators/
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
https://forums.phpfreaks.com/topic/46612-solved-and-operators/#findComment-226931
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
https://forums.phpfreaks.com/topic/46612-solved-and-operators/#findComment-226935
Share on other sites

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.