the => operator

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jmilane
Forum Commoner
Posts: 89
Joined: Mon Aug 07, 2006 6:05 pm

the => operator

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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']
jmilane
Forum Commoner
Posts: 89
Joined: Mon Aug 07, 2006 6:05 pm

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Post 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.
Last edited by jayshields on Mon Aug 14, 2006 11:50 am, edited 1 time in total.
jmilane
Forum Commoner
Posts: 89
Joined: Mon Aug 07, 2006 6:05 pm

Post 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 '=='
jmilane
Forum Commoner
Posts: 89
Joined: Mon Aug 07, 2006 6:05 pm

Post 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.
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Post by jayshields »

It's all on php.net.

=== is the identical operator, whereas == is the equal to operator.
Last edited by jayshields on Mon Aug 14, 2006 11:52 am, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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)
jmilane
Forum Commoner
Posts: 89
Joined: Mon Aug 07, 2006 6:05 pm

Post by jmilane »

jayshields wrote:It's all on php.net.
Indeed it is. Thanks for the direction.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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
}
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
Post Reply