Jump to content

explain these please


CyberShot

Recommended Posts

I am working on learning php. I want to work with wordpress more. I have seen two symbols. Not sure what they are called. maybe you can tell me. One looks like this -> the other looks like this =>

 

case in point. Here is a line from wordpress to get the post

 

<?php $recent = new WP_Query("cat=12&showposts=0"); while($recent->have_posts()) : $recent->the_post();?>

 

so is the line above saying that $recent is equal to having a post? what does the -> mean in the line above?

 

  if you could be as detailed as possible with an explanation, it would be greatly appreciated. thanks.

Link to comment
https://forums.phpfreaks.com/topic/140410-explain-these-please/
Share on other sites

-> is used to indicate, that have_posts() and the_post() are methods of $recent object.

 

This is related to object oriented programming, and if you want to learn more about it start here

http://www.php.net/manual/en/language.oop5.php

 

 

=> on the other hand is used in two cases (as far as I remember)

 

1. To define items of array

 

$arrayVariable = array(
  "foo" => "text",
  "bar" => 123,
);

 

2. In foreach loops

 

foreach ($arrayVariable AS $key => $value) {
  echo "$key: $value <br/>\n";
}

if you use $arrayVariable from point 1 above, it will display

foo: text
bar: 123

Link to comment
https://forums.phpfreaks.com/topic/140410-explain-these-please/#findComment-734863
Share on other sites

It is likely that the_post() method does just that. It's echoing posts.

 

Take a look at this example

 

class exampleClass {

  public function displayArray() {
    foreach($this->array AS $key => $value)
    echo "$key: $value <br/>\n";
  }

  private $array = array (
    "foo" => "text",
    "bar" => 123,
  );

}

$object = new exampleClass;
$object->displayArray();

 

Code not tested :P

 

[edit]

typo

Link to comment
https://forums.phpfreaks.com/topic/140410-explain-these-please/#findComment-734871
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.