1

I have array, where i put data depend on the url. But there is the problem, i can not print this array like in the simple php:

$array = ["hi", "name"]; echo $array[1];

what is wrong in my code that i will show, and how i can print the array

Code:

<?php

class Translate {

    public $transl = [];

    public function getTransl($transl = []) {
        if (( isset($_GET['lang']))) {
    if ($_GET['lang'] == "en") {
        $this->transl = ['word1', 'word2'];
        }
        if ($_GET['lang'] == "ru") {
            $this->transl = ['word3', 'word4'];
            }
}
    }

}

$test = new Translate();
$test->getTransl([0]);

?>
6
  • You are actually not printing anything. Commented Oct 4, 2019 at 14:22
  • return $this->transl; from your method i think Commented Oct 4, 2019 at 14:22
  • I know, but how I can do this? Commented Oct 4, 2019 at 14:23
  • typing the keyword echo somewhere Commented Oct 4, 2019 at 14:23
  • no, it gives me error Commented Oct 4, 2019 at 14:24

4 Answers 4

1

No idea, why are you using $transl = [] in method parameter when you need specific index, here you can just pass key which you need.

Example:

<?
class Translate {

    public $transl = 0;

    public function getTransl($transl = '') {
      if (( isset($_GET['lang']))) {
        if ($_GET['lang'] == "en") {
          $this->transl = ['word1', 'word2'];
        }
        if ($_GET['lang'] == "ru") {
            $this->transl = ['word3', 'word4'];
        }
      }
      return $this->transl[$transl];
    }
}

$test = new Translate();
echo $test->getTransl(0); // this will print `word1` if $_GET['lang'] equal to `en`
?>

In your code, you are not using either echo or return in your method to get the result, and you are not matching $transl with $this->transl anywhere to get the specific index.

Sign up to request clarification or add additional context in comments.

Comments

0

First, you don't pass the index as a parameter. You use it as an index. Proper syntax would be:

$test->getTransl()[0];

That assumes that $test->getTransl() returns an array. But it doesn't. It doesn't return anything. It just sets the class attribute $transl. So, you have to do it in two lines:

$test->getTransl(); // This sets the attribute
$test->transl[0]; // This uses the attribute

But, that goes against that the method implies. The method implies that it returns the transl attribute. So, you SHOULD return it in the function with:

return this->transl;

Then, you can use:

$test->getTransl()[0];

Of course, this won't print anything. You need to precede with with echo or print:

echo $test->getTransl()[0];

Comments

0

I think you'll just need to return the output.

Let's say you have a file named test.php on your server

class Translate {

    public $transl = [];

    public function getTransl($transl = []) {
        if (( isset($_GET['lang']))) {
            if ($_GET['lang'] == "en") {
                $this->transl = ['word1', 'word2'];
            }
            if ($_GET['lang'] == "ru") {
                $this->transl = ['word3', 'word4'];
            }
        }
        return $this->transl;
    }

}

$test = new Translate();
$output=$test->getTransl([0]);
echo "<pre>";
    print_r($output);
echo "</pre>";

Running http://server/{{enterfolderhere}}/test.php?lang=en in your browser will give

Array
(
    [0] => word1
    [1] => word2
)

Running http://server/{{enterfolderhere}}/test.php?lang=ru in your browser will give

Array
(
    [0] => word3
    [1] => word4
)

Comments

0

There are a few issues with your code as others have pointed out. But from a code structure point of view, there are some more fundamental issues (IMHO).

If you are going to create a translator, basing it on if $_GET variables means it can be difficult to test. In this example, you send in a language you want in the constructor and then the class will just set the private translator variables to the table of translations.

Secondly - using numeric values for the word your after can be prone to errors (so can this method, but less so). In this case, the key of the translation is the word you want to start with and the value is the new word, so rather than

echo $test->getTransl(0);

you use

echo $russianTrans->getTransl("word2");

This is the code, hope it helps...

class Translate {
    // Use private variables whenever possible
    private $transl = [];

    public function __construct( string $lang ) {
        if ($lang == "en") {
            $this->transl = ['word1' => 'word1', 'word2' => 'word2'];
        }
        if ($lang == "ru") {
            $this->transl = ['word1' => 'word3', 'word2' => 'word4'];
        }
    }

    public function getTransl($word) {
        return $this->transl[$word];
    }

}

$russianTrans = new Translate($_GET['lang']); // Or hardcode it to 'ru' for example
echo $russianTrans->getTransl("word2");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.