6

This is the code where we have used -> and => .

But I always get confused, while writing code at that time which one to use and where.

So seeking its logic to easily remember it.

   $quan= $request->all();
   ChaiExl::create(['date'=>date('Y-m-d',strtotime($quan['dt'])),'quantity'=>$quan['quan']]);

return view('edit',['row'=>$row]);
SUB0DH
  • 5,130
  • 4
  • 29
  • 46
Anika
  • 71
  • 1
  • 1
  • 5
  • 1
    http://stackoverflow.com/questions/3737139/reference-what-does-this-symbol-mean-in-php?rq=1 – nikos.svnk Mar 24 '17 at 09:17
  • Possible duplicate of [Reference - What does this symbol mean in PHP?](http://stackoverflow.com/questions/3737139/reference-what-does-this-symbol-mean-in-php) – Option Mar 24 '17 at 10:03

4 Answers4

17

-> and => are both operators.

The difference is that => is the assign operator that is used while creating an array.

For example: array(key => value, key2 => value2)

And -> is the access operator. It accesses an object's value

JimmyNJ
  • 1,134
  • 1
  • 8
  • 23
Gohel Dhaval
  • 820
  • 1
  • 8
  • 12
4

This is PHP syntax, not laravel specifics.

=> is for setting values in arrays:

$foobar = array(
    'bar' => 'something',
    'foo' => 222
);

or

$foobar = [
    'bar' => 'something',
    'foo' => 222
];

-> is used for calling class methods and properties:

class MyClass {

   public $bar = 'something';

   public function foo() {

   }

}
$foobar = new MyClass();
$foobar->foo();
echo $foobar->bar;
Jure Jager
  • 146
  • 1
  • 3
  • 11
2

=> is used in associative array key value assignment. look below:

array(
key  => value,
key2 => value2,
key3 => value3,
...

)

-> is used to access an object method or property. Example:

$object->method() or $object->var1
naib khan
  • 928
  • 9
  • 16
1

When you want to access a method from a class you will use -> that is

$class = new Class;
$class->mymethod();

but when you want to declare an array of object pairs you use => that is

$property = ('firstproperty', ['second'=>'secondPair','third'=>'thirdPair'])
Bosco
  • 1,536
  • 2
  • 13
  • 25