954,580 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Semi-noob help

Hello All,

I've been dabbling with PHP for a couple years now, nothing serious, but something has really been bugging me. I see -> and => being used all the time, almost as assignment operators but can't find squat about them in any operator list, either on php.net, or in the two books I have. Nothing in the indices. It's really frustrating. What the heck do these two "things" do??

Please help before I go MORE insane!

Thanks,

Steel Rat

Steel Rat
Newbie Poster
5 posts since Feb 2006
Reputation Points: 10
Solved Threads: 0
 

-> is used for object oriented PHP. So if you have a class called user and the user class has a function called login and a variable called name. You would create an object from the class called John maybe. So to log John in you would call the login function like so $John->login(); or if you wanted John's name you would call $name = $John->name;

I did this really quick so sorry if it doesn't make sense.

DanceInstructor
Posting Whiz
368 posts since Feb 2005
Reputation Points: 17
Solved Threads: 14
 

Thanks, DI.

That sorta helps. Does the -> have a name I can search on? It's not an operator, is it a directive?

And any clues about the => ?

Thanks again,

SR

Steel Rat
Newbie Poster
5 posts since Feb 2006
Reputation Points: 10
Solved Threads: 0
 

Well => is used in the foreach() function, you can read about it here:

http://us2.php.net/foreach

and you can read more about object oriented PHP here:

http://us3.php.net/manual/en/language.oop.php

or just google object oriented PHP tutorial

DanceInstructor
Posting Whiz
368 posts since Feb 2005
Reputation Points: 17
Solved Threads: 14
 

Thanks again.

I guess my problem is they don't explain what => or -> actially is, they just show them in use. => seems to be an assignement operator, but it also seems to be totally useless, since the foreach already does an assignment if you do

foreach ($a as $b)

There's the assignment. Using

foreach ($a as $c => $b)

just seems totally useless to me. What am I missing?

Steel Rat
Newbie Poster
5 posts since Feb 2006
Reputation Points: 10
Solved Threads: 0
 

When you use foreach($a as $c => $b) then the key is copied to the variable $c. This gives you an easy way of changing the origional array if you want. Like so:

[PHP]foreach($a as $c => $b)
{
$b = '' . $b . '';
$a[$c] = $b;
}[/PHP]

DanceInstructor
Posting Whiz
368 posts since Feb 2005
Reputation Points: 17
Solved Threads: 14
 

Ok, so it's not a simple assignment. it's a key or index assignment. I guess that makes sense. I see the => used all the time when populating variables for templates, but just couldn't figure out what it was doing.

Thanks again for your help!

Steel Rat
Newbie Poster
5 posts since Feb 2006
Reputation Points: 10
Solved Threads: 0
 

None of that seemed easy to understand

Yes -> is used for objects such as

$apple = new Fruit();
$apple->color = 'red';
$apple->price = 1;

while => is used for arrays

$apple = array();
$apple['price'] = 1;
$apple['color'] = 'red';

$pear = array('price' => 2, 'color' => 'green');


and yes, it gets used in foreach because it's describing the relationship between the array key and the value.

sarahk
Junior Poster
144 posts since Apr 2005
Reputation Points: 10
Solved Threads: 1
 

:twisted:

DanceInstructor
Posting Whiz
368 posts since Feb 2005
Reputation Points: 17
Solved Threads: 14
 

Thanks to both of you again!

Steel Rat
Newbie Poster
5 posts since Feb 2006
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You