What does => mean?

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
nitediver
Forum Contributor
Posts: 109
Joined: Tue Feb 24, 2009 9:05 am

What does => mean?

Post by nitediver »

What does => sign mean in these code, is it mean greater than?
And why it used twice, after else statement?

Code: Select all

$cart[] = array('product' => $product, 'quantity' => 1);

Code: Select all

<?php
    if (!$in_cart) {
        $cart[] = array('product' => $product, 'quantity' => 1);
    }
} else {
    $cart[] = array('product' => $product, 'quantity' => 1);
}
$_SESSION['cart'] = $cart;
?>
User avatar
PHPHorizons
Forum Contributor
Posts: 175
Joined: Mon Sep 14, 2009 11:38 pm

Re: What does => mean?

Post by PHPHorizons »

Hello nitediver,

That symbol is used to indicate key/value pairs.

On the left hand side is the key, and on the right hand side is the value.


array(key => value, key => value)

Hope that helps.
User avatar
hypedupdawg
Forum Commoner
Posts: 74
Joined: Sat Apr 10, 2010 5:21 am

Re: What does => mean?

Post by hypedupdawg »

Yes; it is used to assign your specific keys to an array. YWithout them, they are numbered automatically from 0 upwards like so:

Code: Select all

<?php
$myArray = array("number one", "number two", "number three");
?>

//outcome

Key: 0 | Value: number one
Key: 1 | Value: number two
Key: 2 | Value: number three
However, this one has specific string keys:

Code: Select all

<?php
$myArray = array("name" => "hypedupdawg", "forum" => "devNetwork");
?>

//outcome

Key: name | Value: hypedupdawg
Key: forum | Value: devNetwork
NOTE: It is very important that your string keys should always have quotes around them.
nitediver
Forum Contributor
Posts: 109
Joined: Tue Feb 24, 2009 9:05 am

Re: What does => mean?

Post by nitediver »

Thank you, that's really helpful.
Post Reply