User Tools

Site Tools


en:web_development:named_arrays

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
en:web_development:named_arrays [2021/12/23 16:46]
mag created
en:web_development:named_arrays [2023/08/16 09:33] (current)
Line 45: Line 45:
 ==== Foreach ==== ==== Foreach ====
   * We've seen how to do a 'for loop' in an earlier class, but there's another loop that is extremely useful for processing arrays. It's the 'foreach' loop.   * We've seen how to do a 'for loop' in an earlier class, but there's another loop that is extremely useful for processing arrays. It's the 'foreach' loop.
-  * Add the following PHP code to the <html><main></html> tag. +  * Add the following PHP code to the <html><main></html> tag after creating your array
-<code>    $my_array = array('item1' => 'dog', 'item2' => 'elephant', 'item3' => 'rabbit'); +<code>    foreach($animals as $name => $item) {
-    foreach($my_array as $name => $item) {+
         echo "MURRAY: $name is a $item";         echo "MURRAY: $name is a $item";
     }</code>     }</code>
-  * The first line creates an array, but unlike the arrays we've seen, each item in the array is named. +  * The first line sets up the loop. It says 'run through this code for each item in the array <html>$animals</html>, where the reference for each item is <html>$name</html> and the item itself is <html>$item</html>'.
-  * Instead of referring to an item by number (<html>$my_array[0]</html>) we can refer to each item by name (<html>$my_array['item2']</html>). +
-  * The second line sets up the loop. It says 'run through this code for each item in the array <html>$my_array</html>, where the reference for each item is <html>$name</html> and the item itself is <html>$item</html>'.+
   * We can now refer to the name and the actual item by using the variables <html>$name</html> and <html>$item</html>.   * We can now refer to the name and the actual item by using the variables <html>$name</html> and <html>$item</html>.
-  * <html>error_log</html> then reproduces these values so that we can read them in the error log.+  * <html>echo</html> then display these values so that we can read them in the HTML code.
   * Try it now to be sure you understand it.   * Try it now to be sure you understand it.
  
Line 75: Line 72:
  
 ==== Objects ==== ==== Objects ====
 +  * Objects are an advanced data structure and we won't go into it too much here, but you'll need to understand a little bit to use the PHP database connection.
 +  * An object is like a named array where you can access each element by name.
 +  * But an object also has functions built into. Recall that in Javascript <html>audio</html> has built-in functions like <html>.play()</html>, <html>.paused()</html> and built-in information like <html>.paused</html>. That's because <html>audio</html> is an object.
 +  * Similarly, in PHP, an object can have both data and functions. For example, a database connection object can have information about the connection and also have functions to extract data.
 +  * Whereas you access the information in an array using <html> [] </html>, we access information and functions using <html>-></html>.
 +  * To use an abstract example, we could create an object called $table.
 +  * We could set the number of legs on the table as follows.
 +<code>$table->legs = 4;</code>
 +  * We could also move the table to another location as follows.
 +<code>$table->move($location);</code>
 +  * There is a whole school of programming based on objects (Object-oriented programming) and objects could be the subject of many classes, but this is all you need to know to use the database connection.
  
 +[[en:web_development:databases:tables|Next: Tables]]
en/web_development/named_arrays.1640306799.txt.gz · Last modified: 2023/08/16 09:33 (external edit)