Closed Thread Icon

Topic awaiting preservation: What does -> and => in PHP mean? Pages that link to <a href="https://ozoneasylum.com/backlink?for=31549" title="Pages that link to Topic awaiting preservation: What does -&amp;gt; and =&amp;gt; in PHP mean?" rel="nofollow" >Topic awaiting preservation: What does -&gt; and =&gt; in PHP mean?\

 
Author Thread
jstuartj
Paranoid (IV) Inmate

From: Mpls, MN
Insane since: Dec 2000

posted posted 12-22-2009 10:25

I've got a stupid PHP question, one I can't google because I have no idea what it's even called.

I'm learning PHP and WordPress and I keep running across a symbols/operators I don't understand?

What is the meaning of the -> in the following line code or what is it called?

code:
while ($my_query->have_posts()) : $my_query->the_post();



I gather from it's context, that it passes the contents of $my_query to the WordPress function have_posts() which return true or false completing the while loop.

The problem is, I don't like using things I don't fully understand so if someone could point me in the proper direction I would appreciate it.

I did find some info on => which appears to be used with associative arrays, but I'm not positive of its usage or meaning either or if it has any relation to ->

J. Stuart J.

CPrompt
Maniac (V) Inmate

From: there...no..there.....
Insane since: May 2001

posted posted 12-22-2009 16:00

the "->" is a pointer to an object or an object reference.

You can read some here : http://php.net/manual/en/language.references.php

Later,

C:\

CPrompt
Maniac (V) Inmate

From: there...no..there.....
Insane since: May 2001

posted posted 12-22-2009 16:03

also, there is a function "mysql_fetch_object" that is probably being used somewhere in there :

http://us2.php.net/manual/en/function.mysql-fetch-object.php

Later,

C:\

twItch^
Maniac (V) Mad Scientist

From: Denver, CO, USA
Insane since: Aug 2000

posted posted 12-22-2009 17:27

Also, "=>" is definitely for arrays.

Consider the following array:

code:
$fruits = array("apple", "pear", "orange");



That's an array of three fruits. You can refer to "orange" in this case as:

code:
echo $fuits[2];



(Point of reference: arrays start counting from 0, so 0 is "apple", 1 is "pear".) That's fine and dandy, but the "=>" operand comes into play if you want to step through, or assign keys that are other than the standard array counting scheme. Let's say you wanted to have an array that started counting at 5 instead of 0:

code:
$fruits = array(5=>"apple", "pear", "orange'");



Great, now 5 is "apple", 6 is "pear", and 7 is "orange". But what if you wanted to step through each piece of an array with keys that are set to the color of the fruit, and do something with it? That's where foreach() comes into play:

code:
$fruits = array("red"=>"apple", "green"=>"pear", "orange"=>"orange");
foreach($fruits as $color=>$fruit)
{
  echo "$fruit is $color\n";
}



I hope this helps you understand what the => operand means :)

-S

jstuartj
Paranoid (IV) Inmate

From: Mpls, MN
Insane since: Dec 2000

posted posted 12-22-2009 18:08

Thanks so much. Those were the last stumbling block to fully understanding what I was doing.


J. Stuart J.

reilz81
Obsessive-Compulsive (I) Inmate

From: ballarat, victoria, australia
Insane since: May 2010

posted posted 05-09-2010 16:21

i have wondered what the => operator does for some time but never really had a need to use it makes sense now so thanks

chrisadam11
Obsessive-Compulsive (I) Inmate

From:
Insane since: Sep 2010

posted posted 09-20-2010 13:55

edit tp: spam removed

(Edited by Tyberius Prime on 09-20-2010 15:17)

« BackwardsOnwards »

Show Forum Drop Down Menu