Class 7 – PHP Variables, Functions, and Arrays
When we learned how to create and use Javascript variables, and Javascript functions, we were learning techniques that apply across many programming languages. Fortunately, in this regard, Javascript and PHP are very similar, and we will not have to re-learn the theory of variables and functions. We will just focus on the slight differences between the syntax of the two languages.
PHP variables
In PHP, variables are declared and defined like this:
$someVariable1 = 10; $someVariable2 = "something";
Notice the dollar sign before the name of each variable – in PHP all variable names begin with the dollar sign. Also notice that, like Javascript, all PHP instructions must end in a semi-colon.
Variables do not need to be explicitly declared in PHP. Whereas in Javascript, we declared variables, as in “var someVariable”, before using them, in PHP, we usually just define them. PHP automatically handles the declaration. There are some circumstances in which we may want to declare our intention to use a variable in PHP without actually giving it a defined value. Doing so is as simple as:
$someVariable1;
Like Javascript, PHP is a mostly untyped language, meaning a single type of variable can hold the any type of data: numeric values, Strings (i.e., text), arrays (i.e. lists), and other more complicated types of data (i.e. objects.)
PHP functions
Functions in PHP look almost identical to their Javascript counterparts. A simple function definition might look like:
function doSomething() {
echo "Hello World";
}
To call this function, one would use the following code:
doSomething();
This function would simply output the text “Hello World”. “echo” is a special PHP command to output some text.
Parameters in functions must use the PHP syntax for variable names, meaning they must begin with a dollar sign. For example, here is a function that accepts a parameter:
function sayHello($personName) {
echo "Hello, " . $personName;
}
This function takes one parameter, which it calls $personName. It then concatenates the text, “Hello, ” with the value of the variable $personName. Unlike Javascript, PHP does not use the “+” sign to do string concatenation. In PHP, “+” is used only for mathematical addition. PHP uses the concatenation operator to concatenate strings. So, for example, calling the function like this…
sayHello("Andy");
…would put the word, “Andy” in the variable called $personName. The function then concatenates the text, “Hello, ” with the word “Andy”. And ultimately, the function would output the following text:
Hello, Andy
PHP arrays
The built-in array() function of PHP is used to create arrays. Recall that arrays are lists of things, not single values like regular variables.
To create an empty array which can be populated with data later in the code, call the array() function with no parameters.
$arrSomething = array();
To add an element to the array at index 15, use code such as:
$arrSomething[15] = "the sixteenth value in the array";
To add an element to the array at the next available index, simply leave the index blank, as in the following code:
$arrSomething[] = "the new value";
To output the value of the 16th item in the array (remember that arrays are indexed starting from number 0, so the 16th item actually has an index value of 15):
echo $arrSomething[15];
In our example, that statement will output the following text, since that is what we stored earlier at index 15 of the array:
the sixteenth value in the array
PHP associative arrays
PHP arrays, like Javascript arrays, can be indexed by either integers or Strings. When they are indexed by Strings, they are often called “associative arrays”. To create an element in an array indexed by a String, use code like this:
$arrSomething["my_birthday"] = "October 3".
To output the value of this item of the array later in the code:
echo $arrSomething["my_birthday"];
This will output the following text:
October 3
PHP multidimensional arrays
PHP, being a loosely-typed language, allows you to put anything you want into a variable. Variables can contain numbers, strings, objects, or any other data type. Arrays are the same. Array elements can hold numbers, strings, objects, and other data types including other arrays.
$myArray = array();
$myArray[] = 100; //array element 0 holds the number 100
$myArray[] = "some text"; // array element 1 now holds the string, "some text"
$myArray[] = array(); //array element 2 now holds an empty array
$myArray[] = array("this", "that", "the other"); //array element 3 now holds an array with three elements in it
echo $myArray[0]; //outputs the number, 100
echo $myArray[1]; //outputs the text, "some text"
echo $myArray[2]; //outputs the text, "Array"... this is probably not what you wanted.
echo $myArray[3]; //outputs the text, "Array"... this is probably not what you wanted.
echo $myArray[3][0]; //outputs the text, "this"
echo $myArray[3][1]; //outputs the text, "that"
echo $myArray[3][2]; //outputs the text, "the other"
Related posts:
Tags: class 7