Broad Network


PHP List

PHP Arrays with Security Considerations

Foreword: In this part of the series, I talk about lists in PHP.

By: Chrysanthus Date Published: 7 Nov 2018

Introduction

This is part 1 of my series, PHP Arrays with Security Considerations. In this part of the series, I talk about lists in PHP. You should have read the previous series before reaching here, as this is the continuation.

A List
In PHP, a list is a set of items separated by commas, in square brackets. The items are like elements of an array. Each element can be assigned to a separate variable outside the array. The following is a list of items, which were found on a reading table.

    'pen', 'book', 'ruler', 'computer'

If a number is a member of the list, it does not have to be typed in quotes. Any string member has to be typed in quotes. If you want to consider the list as a group, place the list in square brackets, as follows:

    ['pen', 'book', 'ruler', 'computer']

The following is the creation of an array:

    $arr = ['pen', 'book', 'ruler', 'computer'];

The items in a list can be assigned to variables outside the list, using the PHP list() function as follows:

    list($var0, $var1, $var2, $var3) = ['pen', 'book', 'ruler', 'computer'];

Try the following code:

<?php

    list($var0, $var1, $var2, $var3) = ['pen', 'book', 'ruler', 'computer'];

    echo $var0, '<br>';
    echo $var1, '<br>';
    echo $var2, '<br>';
    echo $var3, '<br>';

?>

The output is:

    pen
    book
    ruler
    computer

You can still assign the items as follows:

<?php

    $arr = ['pen', 'book', 'ruler', 'computer'];

    list($var0, $var1, $var2, $var3) = $arr;

    echo $var0, '<br>';
    echo $var1, '<br>';
    echo $var2, '<br>';
    echo $var3, '<br>';

?>

If you try this code, the result will be the same as above.

You can decide to copy just the first few items of an array. Try the following code:

<?php

    list($var0, $var1) = array('pen', 'book', 'ruler', 'computer');

    echo $var0, '<br>';
    echo $var1, '<br>';

?>

The output is:

    pen
    book

If you do not want the function, list(), you can use square brackets on the left hand side of the assignment operator as follows:

<?php

    [$var0, $var1] = array('pen', 'book', 'ruler', 'computer');

    echo $var0, '<br>';
    echo $var1, '<br>';

?>

The output is:

    pen
    book

Subscripting a List
The subscript for an array element is an integer inside square brackets.

You can subscript a list similar to the way you subscript an array. Try the following code:

<?php

    $item = ['pen', 'book', 'ruler', 'computer'][2];

    echo  $item;

?>

The output is:

    ruler

The square brackets with its subscript is just at the end of the list. The expression returns the value whose index is the subscript (index counting begins from zero).

List as Associative Array
A list can be an associative array. Try the following code:

<?php

    $item = ['Pear' => "green", 'Lemon' => "green", 'Apple' => "purple", 'Banana' => "yellow"]['Apple'];

    echo  $item;

?>

The output is:

    purple

, the value of the element whose key is Apple.

That is it for this part of the series. We stop here and continue in the next part.

Chrys


Related Links

Basics of PHP with Security Considerations
White Space in PHP
PHP Data Types with Security Considerations
PHP Variables with Security Considerations
PHP Operators with Security Considerations
PHP Control Structures with Security Considerations
PHP String with Security Considerations
PHP Arrays with Security Considerations
PHP Functions with Security Considerations
PHP Return Statement
Exception Handling in PHP
Variable Scope in PHP
Constant in PHP
PHP Classes and Objects
Reference in PHP
PHP Regular Expressions with Security Considerations
Date and Time in PHP with Security Considerations
Files and Directories with Security Considerations in PHP
Writing a PHP Command Line Tool
PHP Core Number Basics and Testing
Validating Input in PHP
PHP Eval Function and Security Risks
PHP Multi-Dimensional Array with Security Consideration
Mathematics Functions for Everybody in PHP
PHP Cheat Sheet and Prevention Explained
More Related Links

Cousins

NEXT

Comments