Permalink
Browse files

Reformatted arrays as per poll: #5

  • Loading branch information...
1 parent c9bb12a commit 7aeeef67382c710e713313fd78981674c16fc067 @PeeHaa committed Jul 2, 2012
Showing with 42 additions and 33 deletions.
  1. +22 −18 topics/07-data-types.md
  2. +20 −15 topics/08-conditions.md
View
@@ -25,20 +25,22 @@ Compound types
PHP has two compound types: [array][array] and [object][object]. The object data type will be introduced in a later topic. An array can be seen as a list of values. These values can consist of all the different data types PHP offers. Instead of creating separate variables as in the previous example we could have also created an array to hold these values:
<?php
- $someArray = array('This is a string',
- 42,
- 3.14159265,
- FALSE,
- );
+ $someArray = array(
+ 'This is a string',
+ 42,
+ 3.14159265,
+ FALSE,
+ );
Once we have created our array we can access the different values by using an index. The indexes of arrays in PHP start with `0`. This index gets incremented when a new item is added. If we want to echo the number `3.14159265` from our array we have to access it by it's index (2):
<?php
- $someArray = array('This is a string',
- 42,
- 3.14159265,
- FALSE,
- );
+ $someArray = array(
+ 'This is a string',
+ 42,
+ 3.14159265,
+ FALSE,
+ );
echo 'This is pi: ' . $someArray[2]; // this will output: This is pi: 3.14159265
@@ -58,10 +60,11 @@ It is also possible to initialize an empty array and add items to it later in th
PHP also supports named (or associative) arrays. This can be useful when storing specific data:
<?php
- $user = array('username' => 'PHP Dave',
- 'fullname' => 'Dave Doe',
- 'age' => 42,
- );
+ $user = array(
+ 'username' => 'PHP Dave',
+ 'fullname' => 'Dave Doe',
+ 'age' => 42,
+ );
echo 'Welcome back ' . $user['username']; // will output: Welcome back PHP Dave
@@ -77,10 +80,11 @@ Instead of just adding values to our array (which would have gotten a numeric in
It is not possible to simply display all the contents of an array by `echo`ing it:
<?php
- $myArray = array('First item',
- 'Second item',
- 'Third item',
- );
+ $myArray = array(
+ 'First item',
+ 'Second item',
+ 'Third item',
+ );
echo $myArray;
View
@@ -70,9 +70,10 @@ In the above example we have used the "or" operator `||`.
Another useful "trick" is grouping. With grouping we can group multiple conditions inside other conditions:
<?php
- $user = array('length' => '1.82',
- 'gender' => 'male',
- );
+ $user = array(
+ 'length' => '1.82',
+ 'gender' => 'male',
+ );
if (($user['length'] > 1.76 && $user['gender'] == 'female') || ($user['length'] > 1.8 && $user['gender'] == 'male')) {
echo 'You are taller than the average person.';
@@ -83,9 +84,10 @@ In this example we have grouped two conditions. Grouping conditions is done by w
Now that we have seen the simplest form of conditional statements we can take it a bit further:
<?php
- $movies = array('Pulp Fiction',
- 'Hackers',
- );
+ $movies = array(
+ 'Pulp Fiction',
+ 'Hackers',
+ );
if (count($movies) < 5) {
echo 'You have a small collection of movies.';
@@ -98,9 +100,10 @@ In this conditional statement we have introduced two new things. The first is th
We can even take this example one step further by adding an [`elseif`][elseif] statement:
<?php
- $movies = array('Pulp Fiction',
- 'Hackers',
- );
+ $movies = array(
+ 'Pulp Fiction',
+ 'Hackers',
+ );
if (count($movies) == 0) {
echo 'Don't you like movies?';
@@ -113,9 +116,10 @@ We can even take this example one step further by adding an [`elseif`][elseif] s
In this example we are first checking whether there are any movies in our array. When the number of movies is greater than 0 it goes further to the next statement which checks whether out movie collection contains less then 5 movies. And otherwise the text `You have a decent number of movies on your collection.` will be displayed. Not that when we would have 0 movies in our collection only the text `Don't you like movies?` would be displayed although 0 is also less than 5. This is because PHP stops checking for the other conditions once a condition is met. You can have as many `elseif` statement as you like / need.
<?php
- $movies = array('Pulp Fiction',
- 'Hackers',
- );
+ $movies = array(
+ 'Pulp Fiction',
+ 'Hackers',
+ );
if (count($movies) == 0) {
echo 'Don't you like movies?';
@@ -130,9 +134,10 @@ In this example we are first checking whether there are any movies in our array.
Another form of a conditional statement is a [`switch`][switch] statement:
<?php
- $animal = array('name' => 'Elephant',
- 'type' => 'land',
- );
+ $animal = array(
+ 'name' => 'Elephant',
+ 'type' => 'land',
+ );
switch($animal['type']) {
case 'land':

0 comments on commit 7aeeef6

Please sign in to comment.