Arrays & Objects

The series is one of the indispensable uses for programming languages. The most important features of arrays are that they can store more than one value in one variable. With these aspects, we will use them frequently in the following sections. Objets are like arrays, the only difference between them is the way they access the values ​​in the array.



# Section Headings

# How are sequences defined?

Accessing Array Values
Adding a Sequence Key

# Directing to the Objey
# Multidimensional Arrays
# The Super Kurses



# How are sequences defined?

The syntax of array definitions in the PHP programming language is as follows.

Rule1: $ arrayName = [ 'value1' , 'value2' , ... 'valueN' ];

Rule2: $ arrayAdd = [ 'key1' => 'value1' , ... 'keyN' => 'valueN' ]

Let's give an example of using the sample array definition and usage below.

$sehirler = ['istanbul', 'izmir', 'ankara', 'bursa'];

[0] => "Istanbul" [1] => "Izmir" [2] => "ankara" [3] => "bursa" 

Above we made a 4- element array definition of the $ cities variable .

Accessing Array Values

You might want to access one of the values in the $ city ​​array in the above definition . Array values ​​are numbered starting from 0 .

echo $sehirler[0]; # istanbul
echo $sehirler[1]; # izmir
echo $sehirler[2]; # ankara
echo $sehirler[3]; # bursa

If you try to reach a non-existent set of values, you will get an error similar to the following.

echo $sehirler[4];
Notice : Undefined offset: 4 in  C: \ xampp \ htdocs \ test.php  on line  5
Adding Sequence Key

Notice that the values ​​in the array are accessed with the sequence number where the values ​​are. If you wish, you can use your own keys instead of this number.

$sehirler = [34 => 'istanbul', 35 => 'izmir'];

echo $sehirler[35]; # izmir

Array keys can contain any of the output types (string | bool | int).

$kullanici = 
    'kullaniciAdi' => 'Ozan', 
    'sifre'        => '1234', 
    'eposta'       => ''

echo $kullanici['kullaniciAdi'];
echo $kullanici['eposta'];



# Dialing to the Object

You can also use arrays by transforming objects into data objects. It would be more useful to use an array of keys to convert the object to a key => value pair. That is, it is unnecessary to translate an index without a key into the object. This is to apply a defined lattice type conversion that needs to be done all at once.

$kullanici = (object)
    'kullaniciAdi' => 'Ozan', 
    'sifre'        => '1234', 
    'eposta'       => ''

echo $kullanici->kullaniciAdi;
echo $kullanici->eposta;

If an invalid key is used, it throws an error similar to the following.

echo $kullanici->telefon;
Notice : Undefined property: stdClass :: $ phone in  C: \ xampp \ htdocs \ test.php  on line  5 



# Multidimensional Series

The concept of multidimensional arrays is used to express the use of arrays in an array. If there are several array definitions inside an array, it is called a size array.

$sehirler = 
    ['isim' => 'istanbul', 'plaka' => '34'],
    ['isim' => 'izmir'   , 'plaka' => '35']

echo $sehirler[0]['isim'] . ':' . $sehirler[0]['plaka'];
echo $sehirler[1]['isim'] . ':' . $sehirler[1]['plaka'];
istanbul: 34
izmir: 35

Above, a 2 -dimensional array usage example is shown. Because two series are used inside . Already [] juxtaposed operator 2 times used his 2 shows that size.



# Super Bros.

Supercursors are predefined array variables that are always valid in all domains. PHP has some predefined variables of its own. The reason for the superglobal is that these variables can provide access to any kind of file or function. Under normal conditions, you can use the super-globals to pass an external variable directly in a function. Below is a list of superglobal series variables.

$ GLOBALS An integrated string containing all the variables defined in the global field of the current script. Variable names are the keys to the index.
$ _SERVER $ _SERVER  is a string containing information such as titles, paths, and locations of scripts. Entries in this directory are created by the HTTP server. These are usually supported on every HTTP server.
$ _GET An associative array of variables passed to the current script via URL modifiers.
$ _POST The array of built-in variables passed to the current script using the HTTP POST method.
$ _FILES An integrated array of elements loaded into the current script using HTTP POST management.
$ _COOKIE An integrated variable string passed through HTTP Cookies to the current script
$ _SESSION A relational string containing session variables that can be used in the current script.  See the Session Functions documentation for more information on how to use it  .
$ _REQUEST By default,  this  is a relational string containing $ _GET$ _POST,  and  $ _COOKIE variables.
$ _ENV The array of built-in environment variables passed to the current script using environment management. These variables are imported into the global namespace of PHP from the environment where the PHP spellchecker works. Most are provided by the shell that runs under PHP. Different systems run different kinds of shells, so a complete list of defined environment variables can not be given. 

An example is shown below.

$merhaba = 'Merhaba';

echo $GLOBALS['merhaba'];
Hello there