Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Indexes | Arrays
Introduction to PHP
course content

Conteúdo do Curso

Introduction to PHP

Introduction to PHP

1. First Acquaintance
2. Variables and Data Types
3. Conditional Statements
4. Arrays
5. Loops

bookIndexes

Let's consider how to work with arrays. First, let's understand how to access specific elements in an array.

To access a specific element in an array, use square brackets with the index. Remember that the index is not the actual position of the element, as indexing in PHP starts from 0. Let's look at an example for clarity.

The index is always one less than the actual position. For example, we can retrieve several elements from an array in the example below:

php

main

copy
1234567
<?php // Initial array $site = ["c", "o", "d", "e", "f", "i", "n", "i", "t", "y"]; // Getting letters 'o' and 'y' echo $site[1] . " " . $site[9]; // outputs 'o y' ?>

In this example, the array $site contains the letters of the word "codefinity". We use numerical indices to access specific letters.

String Indexes


In PHP, just like arrays, strings also have indexes starting from zero. This means you can access each individual character of a string by its index, similar to how you work with elements in an array. For example, in the string $string = "Hello";, the character 'H' has index 0, 'e' has index 1, and so on. You can access characters using square brackets like $string[0] will give you 'H', or curly braces like $string{0} will also give you 'H'. This allows you to manipulate individual characters in a string as easily as elements in an array.

php

main

copy
1234567
<?php // Initial array $string = "Hello"; // Getting letter 'H' echo $string[0]; // outputs 'H' ?>

Tarefa

Fill in the blanks in the code. Access the second element of the array ($array) and print it to the screen.

Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 4. Capítulo 2
toggle bottom row

bookIndexes

Let's consider how to work with arrays. First, let's understand how to access specific elements in an array.

To access a specific element in an array, use square brackets with the index. Remember that the index is not the actual position of the element, as indexing in PHP starts from 0. Let's look at an example for clarity.

The index is always one less than the actual position. For example, we can retrieve several elements from an array in the example below:

php

main

copy
1234567
<?php // Initial array $site = ["c", "o", "d", "e", "f", "i", "n", "i", "t", "y"]; // Getting letters 'o' and 'y' echo $site[1] . " " . $site[9]; // outputs 'o y' ?>

In this example, the array $site contains the letters of the word "codefinity". We use numerical indices to access specific letters.

String Indexes


In PHP, just like arrays, strings also have indexes starting from zero. This means you can access each individual character of a string by its index, similar to how you work with elements in an array. For example, in the string $string = "Hello";, the character 'H' has index 0, 'e' has index 1, and so on. You can access characters using square brackets like $string[0] will give you 'H', or curly braces like $string{0} will also give you 'H'. This allows you to manipulate individual characters in a string as easily as elements in an array.

php

main

copy
1234567
<?php // Initial array $string = "Hello"; // Getting letter 'H' echo $string[0]; // outputs 'H' ?>

Tarefa

Fill in the blanks in the code. Access the second element of the array ($array) and print it to the screen.

Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 4. Capítulo 2
toggle bottom row

bookIndexes

Let's consider how to work with arrays. First, let's understand how to access specific elements in an array.

To access a specific element in an array, use square brackets with the index. Remember that the index is not the actual position of the element, as indexing in PHP starts from 0. Let's look at an example for clarity.

The index is always one less than the actual position. For example, we can retrieve several elements from an array in the example below:

php

main

copy
1234567
<?php // Initial array $site = ["c", "o", "d", "e", "f", "i", "n", "i", "t", "y"]; // Getting letters 'o' and 'y' echo $site[1] . " " . $site[9]; // outputs 'o y' ?>

In this example, the array $site contains the letters of the word "codefinity". We use numerical indices to access specific letters.

String Indexes


In PHP, just like arrays, strings also have indexes starting from zero. This means you can access each individual character of a string by its index, similar to how you work with elements in an array. For example, in the string $string = "Hello";, the character 'H' has index 0, 'e' has index 1, and so on. You can access characters using square brackets like $string[0] will give you 'H', or curly braces like $string{0} will also give you 'H'. This allows you to manipulate individual characters in a string as easily as elements in an array.

php

main

copy
1234567
<?php // Initial array $string = "Hello"; // Getting letter 'H' echo $string[0]; // outputs 'H' ?>

Tarefa

Fill in the blanks in the code. Access the second element of the array ($array) and print it to the screen.

Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Let's consider how to work with arrays. First, let's understand how to access specific elements in an array.

To access a specific element in an array, use square brackets with the index. Remember that the index is not the actual position of the element, as indexing in PHP starts from 0. Let's look at an example for clarity.

The index is always one less than the actual position. For example, we can retrieve several elements from an array in the example below:

php

main

copy
1234567
<?php // Initial array $site = ["c", "o", "d", "e", "f", "i", "n", "i", "t", "y"]; // Getting letters 'o' and 'y' echo $site[1] . " " . $site[9]; // outputs 'o y' ?>

In this example, the array $site contains the letters of the word "codefinity". We use numerical indices to access specific letters.

String Indexes


In PHP, just like arrays, strings also have indexes starting from zero. This means you can access each individual character of a string by its index, similar to how you work with elements in an array. For example, in the string $string = "Hello";, the character 'H' has index 0, 'e' has index 1, and so on. You can access characters using square brackets like $string[0] will give you 'H', or curly braces like $string{0} will also give you 'H'. This allows you to manipulate individual characters in a string as easily as elements in an array.

php

main

copy
1234567
<?php // Initial array $string = "Hello"; // Getting letter 'H' echo $string[0]; // outputs 'H' ?>

Tarefa

Fill in the blanks in the code. Access the second element of the array ($array) and print it to the screen.

Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
Seção 4. Capítulo 2
Switch to desktopMude para o desktop para praticar no mundo realContinue de onde você está usando uma das opções abaixo
some-alt