Arrays in PHP


An array is another type of data, like a number or a string. The main difference between an array and other data types is its ability to store more than one value in a variable. In the previous examples, a variable name has always been associated with only one value:

$name = “Innocent”

$age = 42

And if we want to know not only the gender, name and age of the user, but also, say, favorite TV shows? It is very difficult to name one favorite series, but remembering a few is much easier. Saving several values ​​into an array variable looks like this: $fav_shows = [“game of thrones”, “american horror story”, “walking dead”]

In this example, we have stored three values ​​at once in the $fav_shows variable. But storing this data is only half the battle. How to work with them then? The already familiar method of displaying a variable on the screen will not work with arrays:

<?php

print(“My favorite shows: ” . $fav_shows);

So you won’t be able to see a list of your favorite TV shows. The fact is that an array is not an ordinary variable. An array does not store simple types like text or numbers (they are also called “scalar types”), but a more complex data structure, so a special approach is needed here.

Within an array, each value has an address where it can be accessed. Such an address is called an index. The index is simply the ordinal number of the value within the array. Indexing starts from zero, so the first element gets index “0”, the second one gets index “1”, and so on.

To get a specific array element, you need to know its index (key). Let’s print the names of all series from the array separated by commas:

<?php

print(“My favorite shows are: ” . $fav_shows[0] . “, ” . $fav_shows[1] . “, ” . $fav_shows[2]);?>

Now we can define an array:

An array is a collection of many elements of the form “key: value”.

Arrays allow you to overwrite existing values ​​and add new ones. You can add a new value to the array like this: $fav_shows[] = “the big bang theory”

The new element will automatically get an index equal to the maximum index of the existing ones + 1. The Big Bang Theory will be stored in the array at index 3.

If we no longer like one of the series, because the new season turned out to be very bad or a new favorite appeared, the values ​​​​in the array can be replaced. To cross out the old value and replace it with the new one, assign the new value to any of the existing indices in the array: $fav_shows[4] = “fargo”

To completely remove (without replacing with another) a value by its index, there is an unset function: unset($fav_shows[4])

Associative arrays

In the previous section, we got acquainted with the so-called simple arrays. But in PHP there is a slightly more complex type of arrays – associative. Associative arrays differ from simple arrays in that they have keys instead of indices. And if the index is always an integer, ordinal number, then the key can be any arbitrary string. That’s what it’s for. We already know a lot about our user: his name, age, favorite color and series. There is only one inconvenience: all this data is now in different variables. It would be convenient to store all this data in one place, and it is in such situations that associative arrays help.

Recording all user information using an associative array:

<?php

$user = [‘age’ => 42, ‘name’ => ‘Innocent’, ‘fav_shows’ => [“game of thrones”, “american horror story”, “walking dead”] ];

Note that an array can contain another array as one of its values. In our example, we have placed a simple array inside an associative array under the key “fav_shows”.

The output of information from associative arrays is similar to simple arrays.

Display all information about the user from the $user variable:

<?php

print(“Name: ” . $user[‘name’] . ” Age: ” . $user[‘age’] . “

Favorite shows: ” . $user[‘fav_shows’][0] . “, ” . $user[‘fav_shows’][1] . “, ” . $user[‘fav_shows’][2]);

The concept of an algorithm in PHP

Many of us have heard something about algorithms in computer science classes at school. Unfortunately, not all school knowledge remains with us after graduation. Nevertheless, understanding what an algorithm is and the ability to build these same algorithms are very important skills, without which it will not be possible to solve even relatively simple tasks.

In simple terms, an algorithm is just a very detailed work plan. We all plan something during our life: a vacation, some event, our own independent study, and the like. What distinguishes an algorithm from a simple list of steps is the existence of conditions and repetitive actions. If you are able to create a good, detailed algorithm for implementing, say, some feature on the site, then you can consider that half the work is already done!

For example, let’s consider one algorithm of medium complexity. Add a contact form to the site. The user can fill out this form, indicate their contact details there and write a message. Information from the completed form is sent to the e-mail of the site owner. Here’s what the algorithm for this task would look like:

Show form.

Check that the form has been submitted.

Make a list of form fields that must be completed.

For each field from the list, check that it is filled.

If the field is empty, show an error and do not submit the form.

If all fields are filled, generate an email message.

Send a message to the site administrator’s address.

PHP Configuration

Most programs always have a separate settings window where you can specify all the main parameters of this application. PHP also has its own settings, only they change not through the interface, but by editing a special file – php.ini. The php.ini file specifies all the settings for PHP operation. From what we will be interested in the first place – this is the error management mode, connecting additional features, session settings and cookies.

In different operating systems, this file is located in different ways. The easiest way is for users of the OpenServer assembly – there php.ini can be opened from the main menu.

In what cases might you need to know the result of an expression?

Expressions are especially useful in conditions, that is, when we want to perform or not perform some action, depending on the result of the expression. Expressions can also be combined with each other in such a way that several separate expressions are eventually evaluated as one.

Let’s say that on our site we want to show a certain picture only to male visitors and over 18 years old. Earlier, in the script code, we already got and stored the visitor’s year of birth and gender in the $age and $gender variables.

Let’s write an expression and a condition to implement this behavior:

<?php

if ($gender == “male” and $age > 17) {

    print(“<img src=’/xxx.jpg’>”);

}

Notice the and keyword, which is an operator that combines two separate expressions into one new one. This new expression will evaluate to true only if both separate expressions are true.

In other words, our condition will only be met for male visitors AND over the age of eighteen. That is, underage boys, as well as adult girls, will not see any picture.

Leave a Reply

Your email address will not be published. Required fields are marked *