B
B
Boris Yakushev2015-12-17 21:48:12
PHP
Boris Yakushev, 2015-12-17 21:48:12

Where is the error in this php script?

Good afternoon, I'm learning php from a book, the following script was given in it:

<?php 
  // Эти строки форматируют выводимую информацию в виде
  // HTML комментариев и последовательно вызывают функцию dump_array()
  echo "\n <!-- начало дампа переменных --> \n\n";

  echo "\n <!-- начало переменных GET --> \n\n";
  echo '<!--'.dump_array($HTTP_GET_VARS)." -->\n";

  echo "\n <!-- начало переменных POST--> \n\n";
  echo '<!--'.dump_array($HTTP_POST_VARS)." -->\n";

  echo "\n <!-- начало переменных СЕАНСА --> \n\n";
  echo '<!--'.dump_array($HTTP_SESSION_VARS)." -->\n";

  echo "\n <!-- начало переменных COOKIE-набора --> \n\n";
  echo '<!--'.dump_array($HTTP_COOKIE_VARS)." -->\n";

  echo "\n <!-- Конец дампа переменных --> \n";

  // Функция dump_array() получает один массив в качестве параметра.
  // Она проходит в цикле по этому массиву  и создает единственную
  // строку, представляющую собой массив как набор.

  function dump_array($array) {
    if (is_array($array)) {
      $size = count($array);
      $string = "";
      if ($size) {
        $count = 0;
        $string .= "{ ";

        // Добавить ключи и значения всех элементов к стоке
        foreach ($array as $key => $value) {
          $string .= $var." = ".$value;
          if ($count++ < ($size-1)) {
            $string .= ", ";
          }
        }
        $string .= "} ";
      }
      return $string;
    } else {
      // Если это не массив просто вернуть его
      return $array;
    }
  }
 ?>

Either the whole day my eyes are washed out, or I don’t understand something. But the script does not work, I tried to copy from the source of the book, it does not work either.

Answer the question

In order to leave comments, you need to log in

6 answer(s)
I
Ivanq, 2015-12-17
@za4me

<?php 
  // Эти строки форматируют выводимую информацию в виде
  // HTML комментариев и последовательно вызывают функцию dump_array()
  echo "\n <!-- начало дампа переменных --> \n\n";

  echo "\n <!-- начало переменных GET --> \n\n";
  echo "<!--".dump_array($_GET)." -->\n";

  echo "\n <!-- начало переменных POST--> \n\n";
  echo "<!--".dump_array($_POST)." -->\n";

  echo "\n <!-- начало переменных СЕАНСА --> \n\n";
  echo "<!--".dump_array($_SESSION)." -->\n";

  echo "\n <!-- начало переменных COOKIE-набора --> \n\n";
  echo "<!--".dump_array($_COOKIE)." -->\n";

  echo "\n <!-- Конец дампа переменных --> \n";

  // Функция dump_array() получает один массив в качестве параметра.
  // Она проходит в цикле по этому массиву  и создает единственную
  // строку, представляющую собой массив как набор.

  function dump_array($array) {
    if (is_array($array)) {
      $size = count($array);
      $string = "";
      if ($size) {
        $count = 0;
        $string .= "{ ";

        // Добавить ключи и значения всех элементов к стоке
        foreach ($array as $key => $value) {
          $string .= $key." = ".$value;
          if ($count++ < ($size-1)) {
            $string .= ", ";
          }
        }
        $string .= "} ";
      }
      return $string;
    } else {
      // Если это не массив просто вернуть его
      return $array;
    }
  }
 ?>

code error + $_POST, $_GET, $_REQUEST, $_COOKIE etc

A
Andrey Burov, 2015-12-17
@BuriK666

your book is very old... HTTP_GET_VARS , HTTP_POST_VARS , HTTP_SESSION_VARS , HTTP_COOKIE_VARS - deprecated

K
Kirill Arutyunov, 2015-12-17
@arutyunov

Use $_POST, $_GET, $_SESSION, $_COOKIEinstead of $HTTP_GET_VARS etc.

O
OnYourLips, 2015-12-17
@OnYourLips

Your PHP book is about 15 years old.
The language has changed a lot since then, and such a book will not help, but only hurt.

A
Artem Spiridonov, 2015-12-17
@customtema

www.php.su/var_dump

B
Boris Yakushev, 2015-12-18
@za4me

In general, I took note of all the comments about the fact that my book was outdated, but the broken code haunted me and it was very interesting why it didn’t work and how to fix it.
In the morning, with a fresh mind, I once again went over his logic, and realized the mistake. The loop outputs $var, but it should be $key.
Thank you all for your help.
ps Just noticed that Ivanq corrected this error.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question