Print Value of an array

Asked

Viewed 57 times

1

Good afternoon,

I want to print the value of the array entered in the getTitleIdLocation function, which I am doing wrong?

Thanks in advance.

<?php

class Imovel {

private $title;
private $id;
private $location;


 function __construct($title, $id, $location){
     $this->title = $title;
     $this->id = $id;
     $this->location = $location;
 }

 function getTitleIdLocation(){
     return array($title, $id, $location);
     }
}



$casa = new Imovel ("Apartamento", "3", "Porto");

echo $casa -> getTitleIdLocation();


?>
  • Depends on what you will do with the result? needs to print in some way in specific?

  • Possible duplicate of I can’t access the function

  • This question should be marked as a duplicate of the question he himself has already asked and has even been answered?

  • I closed as not clear because of this comment: https://answall.com/questions/400473/n%C3%a3o-can-access-%C3%A0-fun%C3%A7%C3%a3o/400480#comment786145_400480 . Now, if you say that you tried something and it went wrong, why is that attempt not in the question? The way it is there’s no real doubt, it could have even closed as out of scope because it’s just asking to make the entire code of the array, which only the author knows how it should be. If you have a specific question, you should post this question completely, show what you tried.

  • Those marked as duplicate may or may not answer the question, because it has a context that indicates it is something else. In fact I analyzed the questions posted and it seems that do not answer (not sure why the question is not clear), one of them certainly does not answer because it shows how to print a associative array which is not the case. Two others even have a loop, I don’t know why they were indicated. The one he asked before does not say anything about the mistake he is having now, I myself indicated to open another question, but the question should state the problem. The Prob. is another only ñ is clear

  • Those marked as duplicates, I do not agree, my duplicate marking refers to the question that he himself has already made about 2 hours, this question here is a duplicate of the other of the same author and same text and classes ... just note ...

  • @Virgilionovic But different problems...

  • The problem is the same @Andersoncarloswoss note he wants to print the array and asked two questions because he was not satisfied with the answer of the first and certainly does not understand the philosophy of the site ... he wants to do the same thing with the array.

Show 5 more comments

2 answers

3

Use the method implode to convert into text.

<?php

class Imovel 
{

    private $title;
    private $id;
    private $location;
    function __construct($title, $id, $location)
    {
        $this->title = $title;
        $this->id = $id;
        $this->location = $location;
    }

    function getTitleIdLocation()
    {
        return array($this->title, $this->id, $this->location);
    }
}



$casa = new Imovel ("Apartamento", "3", "Porto");
echo implode(" ",$casa->getTitleIdLocation());
// Apartamento 3 Porto
?>

Sources:

  1. Array to String PHP?( In English)
  2. Implode function
  • NOTE: I am editing. It is not an answer.

  • My php ide was crashing. I was trying to paste and couldn’t. Now I got it.

  • There’s an error in the method getTitleIdLocation where to access private members should be used $this that is to say, $this->title etc.

  • @Virgilionovic Thank you.

0

The echo - Displays one or more strings

You must use print_r - Prints variable information in a readable form

$casa = new Imovel ("Apartamento", "3", "Porto");
echo $casa -> getTitleIdLocation();
//TROCA POR
print_r($casa -> getTitleIdLocation());

Browser other questions tagged

You are not signed in. Login or sign up in order to post.