Creating Arrays in PHP


Masses in php are very similar to filling a large box with a large number of compartments. Section numbering starts from zero. Also in PHP, the numbering of array elements starts from zero.

Each box where we can put some value is an array index. The number of boxes is not limited and we can create such an array.

Let’s try to create an array in PHP in practice now and see how we can work with it.

Creating an array in PHP is a bit like creating a variable. We also put a $ sign, come up with a name for the array, and put square brackets. The square brackets indicate the index of the array element that we are going to fill.

<?php

$arr[1] = 3;

$arr[0] = ‘Hi’;

$arr[3] = ‘123’;

$arr[4] = 4;

$arr[7] = 5;

?>

Index numbers may not be listed in order.

If you try to output an array of data using the echo statement:

echo $arr;

We’ll get a warning that we’re trying to output an array as a string and the echo statement can’t do that.

The fact is that the echo operator is an operator that works with strings, and inside the $arr variable we have an array.

How can we see the contents of this array?

PHP has a special command specifically for this, called print_r().

print_r($arr)

When executing this command, we will display the contents of the array in a formatted form.

There is another way to see the contents of an array – this is to use the command:

var_dump($arr)

When executing this command, we will see the output of information about the array with the types of data that it contains for each cell.

Well, with the help of the echo operator, it is possible to see the contents of a particular element of the array.

To do this, we write the following:

echo $arr[1];

We just specify the index of the array element that we would like to look at.

When creating an array, it is allowed not to specify the number of a specific array cell in which the data will be located.

<?php

$arr[] = 3;

$arr[] = ‘Hi’;

$arr[] = ‘123’;

$arr[] = 4;

$arr[] = 5;

?>

In this case, the array cells will be filled sequentially and from top to bottom. First, the zero cell will be filled, then the first, and so on.

In addition, it is possible to give any name to the elements of an array in PHP.

<?php

$arr[‘test1’] = 3;

$arr[‘test2’] = ‘Hello’;

$arr[‘test3’] = ‘123’;

$arr[‘test4’] = 4;

$arr[‘test5’] = 5;

?>

Such an array, in which some string value is indicated as indices, is called associative (from the word association). Those. we give the cells some name by which it can then be found.

If you need to change the value of any element of the array, you can overwrite its value using the code below.

<?php

$arr[‘test1’] = 3;

$arr[‘test2’] = ‘Hello’;

$arr[‘test3’] = ‘123’;

$arr[‘test4’] = 4;

$arr[‘test5’] = 5;

$arr[‘test1’] = ‘rrr’;

?>

These are the basics of working with arrays in PHP.

Practice creating arrays now too and try changing their value and outputting them with output statements.

When writing various programs in programming languages, one often encounters the task of performing a certain action, a certain number of times.

In PHP, there are so-called loops for this purpose. These are special code constructs that allow you to perform certain actions, a certain number of times. How much we will indicate.

Let’s now look at the main types of loops that are available in the PHP language and we will start with the while loop.

In order to execute this loop, a special operator called while is used.

<?php

while(condition_under which_the_loop_is_executed) {

    action 1;

    action 2;

    …

}

?>

while – translated from English as “while”. While some condition is true, while it returns “True” (true), you need to perform some actions.

To start this cycle, let’s look at a small example:

<?php

$i = 1;

while($i <= 5) {

    echo “Action $i \n”;

    $i++;

}

?>

The $i variable is something like a counter that is incremented each “pass” of the loop.

Each step of the loop, we display the inscription “Action 1”, “Action 2”, …

To increment the loop counter by 1, we use the $i++ construct.

\n is a new line command.

With this code, we perform our actions 5 times. If you need to perform actions a different number of times, you need to set the appropriate value in the conditions.

Thus, using the while statement, you can loop through a certain set of actions.

Now let’s learn how to perform arithmetic operations in PHP.

Let’s say we have two variables in PHP:

$a = 5;

$b = 7;

$result = $a + $b;

echo $result;

Let’s create a result variable and use this construction as a result.

In this variable we will enter the sum of the values ​​of the variables a and b. And the value of what happens will be output by the echo operator.

If you run this program, the result will be the number 12. That is, there was an addition of two numbers from the variables a and b.

In order to perform the subtraction operation, we use the construction:

$result = $a – $b;

multiplications:

$result = $a * $b;

division:

$result = $a / $b;

integer division:

$result = $a % $b;

These are the basic arithmetic operations we can perform with you in the PHP programming language.

As in any programming language, when working with PHP it is very common to handle various conditional operations.

For example, if a certain condition is met, a set of actions must be performed. Otherwise, if some other condition is met, then some other set of actions must be performed.

To solve this problem: creating such conditional structures in PHP there is a so-called if conditional statement.

Let’s get to know him now.

In PHP, the conditional statement is written as follows.

if (condition) {

    action1;

    action2;

    …

}

else {

    actions otherwise;

}

“doing otherwise” is executed if the condition for the if statement is false.

The task of the “condition” is to return either true (true) or false (false).

Let’s write a simple program in which some conditions will be checked and, depending on them, some actions will be performed.

$a = 1;

$b = 5;

if ($a < $b) {

    echo ‘a is less than b’;

}

else {

    echo ‘a is greater than b’;

}

If we execute this code, we will get a message that “a is less than b”.

The else condition is an optional condition and may not exist at all.

There are also situations where you need to check several conditions at once.

$a = 5;

$b = 5;

if ($a < $b) {

    echo ‘a is less than b’;

}

else {

    echo ‘a is greater than b’;

}

If we now execute such a program, we will get a message that ‘a is greater than b’. But, in fact, we have a not greater than b, but equal to b.

In order to process such an intermediate condition, there is a special else if construct.

$a = 5;

$b = 5;

if ($a < $b) {

    echo ‘a is less than b’;

}

else if($a == $b) {

    echo ‘a equals b’;

}

else {

    echo ‘a is greater than b’;

}

When we run this code, we already get the message that ‘a is equal to b’, which is already true.

There can be any number of else if constructs.

These are the basics of working with the if statement in PHP. There is nothing complicated about this in PHP. It’s just checking some conditions and performing some actions that match that condition.

Leave a Reply

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