Page 1 of 1

the => operator

Posted: Mon Aug 14, 2006 11:36 am
by jmilane
I have a background in OOP, though not PHP.
I am getting back into this after a long time away.

Can someone point me towards an explanation of '=>' ?

It is proving difficult to search for. It may be something you need to understand in the larger sense.

Thanks.

J

Posted: Mon Aug 14, 2006 11:43 am
by feyd
It isn't an operator. It's a part of the inline array construct.

Code: Select all

$foo = array('bar' => 'hi');
"bar" is the key that maps to "hi." It would be accessed through $foo['bar']

Posted: Mon Aug 14, 2006 11:46 am
by jmilane
feyd wrote:It isn't an operator. It's a part of the inline array construct.

Code: Select all

$foo = array('bar' => 'hi');
"bar" is the key that maps to "hi." It would be accessed through $foo['bar']
That makes sense.

And does '::' refer to a parent's functions?

Posted: Mon Aug 14, 2006 11:48 am
by feyd
:: is the scope resolution operator, just like in C++. The left operand is either the name of a class or one of two special names: self and parent. The right operand is either a static property or static method of that class.

Posted: Mon Aug 14, 2006 11:49 am
by jayshields
Well, I'm probably not the best person to ask (what a way to start a reply!), but the only time I've seen/used that is when using a foreach loop.

Example:

Code: Select all

foreach (array_expression as $key => $value)
    statement

Since you mentioned OOP in your post, I assume you mean '->' instead. This is used when handling a class.

Example:

Code: Select all

class Something {
  var $items;

  function whatever() {
    return $this->items; //that's how you would handle/access a variable set inside the class
  }
}

$test = new Something;
$test->whatever(); //that's how you would call the whatever() function.
Correct me if I'm wrong...

edit: got beaten to it.

Posted: Mon Aug 14, 2006 11:49 am
by jmilane
feyd wrote::: is the scope resolution operator, just like in C++. The left operand is either the name of a class or one of two special names: self and parent. The right operand is either a static property or static method of that class.
Awesome.

One more, please

'==='

Its not an equivalency check... thats '=='

Posted: Mon Aug 14, 2006 11:50 am
by jmilane
jayshields wrote:Well, I'm probably not the best person to ask (what a way to start a reply!), but the only time I've seen/used that is when using a foreach loop.

Example:

Code: Select all

foreach (array_expression as $key => $value)
    statement

Since you mentioned OOP in your post, I assume you mean '->' instead. This is used when handling a class.

Example:

Code: Select all

class Something {
  var $items;

  function whatever() {
    return $this->items; //that's how you would handle/access a variable set inside the class
  }
}

$test = new Something;
$test->whatever(); //that's how you would call the whatever() function.
Correct me if I'm wrong...
You are not wrong. This is actually what I was asking. I confused myself.

Thanks, J.

Posted: Mon Aug 14, 2006 11:51 am
by jayshields
It's all on php.net.

=== is the identical operator, whereas == is the equal to operator.

Posted: Mon Aug 14, 2006 11:51 am
by feyd
triple equal (===) is an identity check. It not only checks the value to see that they are equal, but the types must match too.

Code: Select all

var_dump(0 == '0', 0 === '0');
outputs

Code: Select all

boolean(true)
boolean(false)

Posted: Mon Aug 14, 2006 11:52 am
by jmilane
jayshields wrote:It's all on php.net.
Indeed it is. Thanks for the direction.

Posted: Mon Aug 14, 2006 1:15 pm
by s.dot
One of my chick friends likes to refer to => as a "little rocket" :)
how's that for a technical explaination? :-P

=> is array assignment

Code: Select all

$array = (
   'key1' => 'value1',
   'key2' => 'value2'
);
-> is used in OOP

Code: Select all

$var = $object->doFunc($data);
=== is used when you need to check types as well as value

Code: Select all

$a = 0;
$b = '0';

if($a == $b)
{
   //this will be true
}

if($a === $b)
{
   // this will be false
}