learning the syntax ->

Hello guys. I’m dipping my toe in php as part of my wordpress learning experience. I had akimmed through some tutorials here and there and the basic is kind of OK as I have some basic knowledge of other programming language. There is one thing though that I’m not too sure about, and I thought you guys can enlighten me: what’s the difference (and meaning) of [php]->[/php] and [php]=>[/php]?
Let’s look at some examples:
[php]< ?php

$my_divs = get_post_meta($post->ID, ‘my_divs’); // Get the field data

foreach ($my_divs as $my_div => $name) // loop through each add_more
{
get_template_part(‘custom-div’, $name); // display the template
}

?>[/php]
Here we have both. what’s the meaning of [php]$post->ID[/php]?
And what’s the meaning of [php]$my_div => $name[/php]? what’s the difference?
thanks

[php]$post->id; // Is an object[/php]
[php]foreach($myArray as $key => $value) {
/* => used for arrays */
}[/php]

Here’s something that might make it clearer:

[php]/* Lets say you have an array */
$myArray = [ ‘player’ => ‘Miguel Cabrera’ ];

/* You can convert it to an object */
$myObject = (object) $myArray;

/* Here’s how to output both of them */
echo $myArray[‘player’] . “
\n”;
echo $myObject->player . “
\n”;[/php]

I personally like working with objects better for in my opinion it’s clearer and besides I really don’t working with arrays unless I absolutely have to. :smiley: Though there are times arrays are necessary and unavoidable.

Just to let you know sometimes you hear people loosely refer to $myObject->player as an argument (variable) or an instance of an object. It all basically boils to being the same thing, which is an object.

and if you see something like this
[php]$myObject->player(); // the braces () is a good giveaway that it is a call to a method:[/php]
that is a call to a method (function) and is NOT an object; which can get confusing unless you just remember to break it down to its simplest parts.

and if you are ever unsure if something is an object or not, just do a var_dump():
for example:
[php]var_dump($myObject);[/php]

Strider actually summed it up, here is some more information if you are interested.

PHP Operators

Sponsor our Newsletter | Privacy Policy | Terms of Service