hello everyone

Can someone explain to me about this two syntax of php the => and ->, i dont have any idea what this is. I already googled it but didnt find anythig. Actually I tried to look the source code of joomla and found this two syntax.

I will appreciate for any help. thanks.

=>

is used for arrays, denoting key/value pairs eg.

$array = array('item1'=>'apple','item2'=>'pear');
foreach($array as $key=>$value){
    echo $key.' = '.$value;
}

->

is used for object orientated processing (OOP)

eg.

$shoppingcart = new basket();

$shoppingcart->addItem('apple');
$shoppingcart->displayBasket();
Member Avatar for diafol

The first is found in arrays, assigning a value to a key

$myarray = array('name'=>'Ardav','country'=>'Wales');

The second is used in Object Orientated Programming for referencing variables and functions (or properties and methods as they are called in OOP-speak):

echo $myObj->name;
$country = $myObj->getCountry();

This last bit shouldn't be confused with the first bit of code (not related).

Thank you very much, as now i think i will research more about that.

BTW do you have a good totutrial or link for the OOP "->" w/o the quotes syntax and what is the name of this syntax, how do we call this syntax?

Member Avatar for diafol

AFAIK, it's called the 'arrow operator'.

Thank you very much for all of your help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.