wildteen88 Posted June 14, 2006 Share Posted June 14, 2006 [b]Q:[/b] What do the following operators do: -> and => ?>[b]A:[/b][size=3][b]The -> Operator[/b][/size]The -> opertator is used in Object Oriented Programming (OOP for short) to call methods/variables from within an object for example you have this class:[code]<?phpclass foobar { function foo() { echo "Foo initiated"; } function bar() { echo "Bar initiated"; }}[/code]To call the foo function you first need to create the object by doing this:[code]$obj = new foobar;[/code]Now you need use the -> operator to call the method within the object:[code]$obj->foo();// the same for calling bar() too:$obj->bar();[/code][size=3][b]The => Operator[/b][/size]But the => operator is used when dealing with arrays. It is used to define the key for an item within an array. For example[code]$person = array("name" => "John", "age" => 24);[/code]So with that code you are you are creating two keys, the first one is 'name' which stores 'John' and a secound key called 'age' which stores the number '24'Now that you have defined the key for each item in the array you can use this code:[code]echo $person['name']; //yields "John"echo $person['age']; //yeilds 24[/code]If you didn't define a key in your array you'll have to get the value from the array like so[code]echo $person[0]; //yields "John"echo $person[1]; //yields 24[/code]=> can also be used in foreach loops, but it used to get the value of the key rather than assing the key, you may use this when debugging POST'd data for example:[code]foreach($_POST as $k => $v) { echo "'" . $k . "' = " . $v;}[/code] Link to comment https://forums.phpfreaks.com/topic/11987-what-does-do/ Share on other sites More sharing options...
Recommended Posts