Undefined value error in PHP (example for beginners) example script

I hope someone can help me determine how to initialize this variable ($ value) from this example script. I copied a beginner book from Vikram Vaswani from PHP.

I have included the display_erors value in php.ini and it returns this error in the browser.

Note: Undefined variable: cities in C: \ BitNami \ wordpress-3.6-0 \ apache2 \ htdocs \ associative-array.php on line 23

Warning: invalid argument provided by foreach () in C: \ BitNami \ wordpress-3.6-0 \ apache2 \ htdocs \ associative-array.php on line 23

Here is my code copied from page 94 of this book

<?php // define array $citites = array( "United Kingdom" => "London", "United States" => "Washington DC", "France" => "Paris", "India" => "Delhi" ); // Iterate over the associative array // and print each value // this example as supplied in the book // returns uninitialized error for either $key or $value on line 20 foreach ($cities as $key => $value) { echo "$value is in $key. \r\n"; } ?> 

Also in the same chapter, this other example, as copied, for the "array iterator" simply hangs endlessly in the browser. UP until all the examples from the book seemed to work perfectly.

This is the code copied from a book for an example of an array iterator. Does anyone know why this hangs endlessly, and the performers do not output output to the browser. Many thanks for the help.

 <?php // define associative array (hash) $cities = array( "United States" => "Washington", "United Kingdom" => "London", "France" => "Paris", "Spain" => "Madrid", "Italy" => "Rome" ); // Create an array itterator object $iterator = new ArrayIterator($cities); // rewind to beginning of array $iterator->rewind(); // iterate over the array // print each value while ($iterator->valid()) { print $iterator->current() . " is in " . $iterator->key() . ". \r\n"; $iterator->next; } ?> 
+4
source share
4 answers

Change $iterator->next to $iterator->next() next to the last line.

$iterator->next does not work, because PHP assumes that you are accessing a field and not calling a method. A method call requires brackets () .

+2
source

I believe that in $iterator->next you want to call a function, rather than trying to access a variable of the ArrayIterator ( $iterator ) class called next .

To call a function, you need to add a set of brackets after the function name ie function_name() with or without a parameter, if necessary.

Thus, instead of $iterator->next you should use $iterator->next() .

+1
source

In the first example, the error is on this line:

 $citites = array( 

You mistakenly wrote "cities" here and broke the foreach loop.

In the second example, just change $iterator->next to $iterator->next() . This will tell PHP that it is a method, not a field, allowing it to be called correctly.

+1
source

You are trying to access the method, so you must close it with the first brackets. You should use $ iterator-> next () instead of $ iterator-> next.

0
source

Source: https://habr.com/ru/post/1498858/


All Articles