PHP Tutorial: Arrays mapping your mind in PHP

PHP Tutorial: Arrays mapping your mind in PHP

According to the PHP manual:

An array in PHP is actually an ordered map. A map is a type that associates values to keys. This type is optimized for several different uses; it can be treated as an array, list (vector), hash table (an implementation of a map), dictionary, collection, stack, queue, and probably more. As array values can be other arrays, trees and multidimensional arrays are also possible.

Uh…. ok, so what does that all mean? Well I’ll start with the part where it said “associates values to keys.” As you should already know all variables in PHP must have a value, whether it is NULL, empty, an array, an integer, whatever. So in the case of an array inside the array there are keys, these keys are like identifiers for the values contained in the array. Here is an example:

<?php
$variable = array('key'=>'value');
?>

So to explain the above code: the variable named $variable is an array, it has one key=>value pair, the key is key and the value is value. So, to use this new value we would simply make a call like this:

echo $variable['key'];

The names of keys can be integers, or strings. If you do not set a key, PHP will set one for you. For example:

<?php
/* PHP will automatically set the key for the value Apples to 0 */
$fruit = array('Apples');
 
/* another way to assign values into an array is like this: */
$veggies[] = 'Lettuce';
$veggies[1] = 'Broccoli';
 
/* so $veggies contains two key=>value pairs, one is 0=>'Lettuce', 
                                    and the other is 1=>'Broccoli' 
To use these values, we can simply do an echo, like this: */
echo "I like to eat {$veggies[0]} and {$veggies[1]} with every meal.";
?>

Now, keys don’t always have to be one or the other, you and mix and match all you’d like:

<?php
$breads[1] = 'Whole Wheat';
$breads['one'] = 'Light Rye';
?>

So there you have it, a simple and straight-forward look at arrays in PHP. I hope you found it useful!

By Mike on March 26, 2011 | PHP, Tutorials
Tags: , , , ,

PHP Tutorial: Strings and Variables

In PHP there are two things you will often use, variables and strings. A variable is an item in your program that can or will change often, hence it is variable. A string is text, typically used when outputting to the browser. When out putting to the browser, you would typically use the echo command. Let’s try that:

<?php
/* let's try echoing a string
and see what it does */
echo "Hello, this is a string, and I am using PHP.<br />";
 
/* let's try another one, just for good measure
what do you say? */
echo 'Hello, this is my second attempt at a string, using PHP.';
?>

Great job! “But Mike, why did you use double quotes in your first string, and single quotes in your second string?” Good question. I did this simply to show you that PHP allows both types of quotes to contain strings. However, they do have some differences.

  • Double quotes: allow you to put variables directly inside, without having to concatenate your string.
  • Single quotes: do not allow variables to be placed directly inside, they will echo the text as literally as possible.

Note: If you string contains quotes that are the same as the ones you use to encase your string, you must escape these characters, using the escape character \ (backslash). For example: echo ‘You can\’t do this.’;

Let’s try escaping a few things.

<?php
echo "The name of this website is \"PHP and MySQLi tutorials\", isn't it?";
echo 'You can\'t use \' inside \', without escaping them';
?>

Let’s move on to variables. All variables in PHP start with a dollar sign, $, and are CaSE-SeNSitiVE. They can contain letters (capital or small), numbers (0-9) and underscores ( _ ). They must, however, start with a letter or an underscore, they can’t start with a number. The next part of variables in PHP is the assignment operator, which is a single equals sign ( = ). Now, don’t start thinking of this as equals, think of it more as the left operand (typically a variable) “gets set to” the expression(s) on the right. Let’s try that:

<?php
/* so remember, all variables start with a $
then a letter or an underscore, then any other combination */
$variable = 'This is my first variable, it contains a string.';
$Math = 1+1;
$_FALSE = TRUE;
$9badname = 'This will fail';
?>

The last variable in that example will not work, because it’s name begins with a number.

Variables in PHP can contain 8 data types, they are as follows:

  • Strings (“This is a string”)
  • Integers (whole numbers)
  • Floating Point Numbers (numbers with a decimal place, 124.356)
  • Booleans (TRUE/FALSE)
  • Arrays (an ordered map)
  • Resources (holds a reference to an external resource)
  • NULL (a value that represents nothing)
  • Objects (a particular instance of a class)

We’ll start with the simple ones, Strings, Integers, and Floating Point numbers.

<?php
/* Let's set a variable to have a value of a string */
$string = 'This is a string value.';
 
/* Let's set a variable to have a value of an integer */
$integer = 2011;
 
/* Let's set a variable to have a value of a floating point number */
$float = 2.011;
?>

Note: Notice that when setting the integer and the floating point variables, there are no quotes required.

Now let’s try putting all that we’ve learned together in one big finale, shall we? OK!

<?php
/* we'll begin by setting a string variable */
$message = 'We have learned a lot in this tutorial.<br />';
 
/* now an integer */
$types = 3;
 
/* and a float */
$understood = 0.1;
 
/* now we can echo all those variables, in any order we want
let's try and make a nifty sentence. */
 
echo $message;
echo "We've learned about $types data types, and we've retained $understood percent of it.";
/* this should output:
We have learned a lot in this tutorial.
We've learned about 3 data types, and we've retained 0.1 percent of it.
*/
?>

Thanks for reading! Hope to see you in the next tutorial.

By Mike on March 22, 2011 | PHP, Tutorials
Tags: , , ,