Array Combine

This function combines two arrays where the first array is treated as the key and the second array as the contents of the said table. The syntax goes like this : array_combine(array1,array2), wherein the array1 is the table which contains the keys values, and the array2 is the contents. It should be noted that these two tables or array�s should have equal amounts of contents for it would become problematic if there was an error in the combination process. An example of the process is shown below:

$a1=array('a','b','c','d')
$a2=array2("Mouse","Rat","Rodent","Mice")
print_r(array_combine($a1,$a2))
?>

The output of which would give us a single table :

Array ( [a] => Mouse [b] => Rat [c] => Rodent [d] => Mice )

The function combined the two tables giving a single table that combines the keys and the contents. More in-depth discussion on the different array functions which in future posts would be the backbone of applications we will be building.

Arrays

Arrays are what tables are to C-based programming languages and what databases are for SQL-based languages. Arrays or tables as they are sometimes called can be used to store the contents of several variables and to create one, you use the following syntax:

Array(key=>value)

The array in the syntax refers to the name of the array being created, the key is the index which is set automatically to a numeric character or string if none is specified. Value is the assigned value or content of the said array which can be seen easily in the following array creation example:

$b=array('z'=>‘Comedy”,’y'=>”Horror”,’x;’=>”Action”);
print_r($b);
?>

This piece of code would produce an output of :

Array ([0] => Comedy [1] => Horror [2] => Action)

More on array functions in the next posts.

More Programming Basics

As with all programming languages PHP has different variable types such as numeric, character, string and Boolean types. Boolean variables in PHP always return either true or false, integers are whole numbers, floating points are decimal or scientifically notated and strings are a chain of characters. Sounds familiar, well they are and they are mostly standard across the various programming languages. For a more in-depth discussion on the different data types of PHP go visit the manual page.
We next discuss operators such as the assignment operator which allows you to assign values to variables allowing complex operations to be constructed into more and more functional programs.

PHP and other Programming Languages

The major notable difference with PHP against other languages with regards to variables is that PHP is more “intelligent”. In C for example, variables have to be explicitly defined as either numeric or alpha-numeric and can only be used to store that defined specific form of data. PHP like all other languages supports a lot of variable types such as integers, floating point numbers, arrays and strings but with one major difference, variables are recognized automatically based on their use and the context of their use. This makes your (programmer’s) life a whole lot easier. PHP variables are defined with a “$” symbol preceding the variable name. It should also begin with either an underscore or an alpha character.

Embedding Comments

Now, to make you a better programmer we all know the value of comments. This allows you to understand the code that you have written defining and given meaning to operations as you build them up. You start with the terminators used by PHP and end with them as well. Single line comments look like this �// comment� and Multi-line ones use the syntax /* comment comment*/. A better example would be the one below:

//comment
/* comment
Comment*/
?>

In the next post we take on the best parts of PHP which would be variables which is essential in all programming languages.

More into the syntax of PHP

As you might have seen, all of the PHP statement ends with �;� which would be somewhat similar to Perl. The valid HTML code that was handed back to the server was :



Who are You?

My name is MacGyver.

More in the coming posts when we dig deeper as we widen our understanding of PHP.

Dissecting/Understanding the first program

The first post had you making a program that was equivalent to the “Hello World” program used for teaching basics of a programming language and here’s how it worked. When the script was requested by opening the web page, Apache intercepted the request and passed it onto PHP which parsed the script looking for the code in between the terminators and then doing the requested operation which was to display the text contained within the echo command. This result was given back to the server then again to the client. The output contained a valid HTML so the browser was able to understand it and execute the requested operation.

Loop Statement – foreach

The foreach loop is mostly used to parse through an array that for every loop, the value of the current array content/value is assigned to the $value (after which the array pointer increments by a factor of one) then the next loop results in the next array content/value.

Syntax:
foreach (array as value)
{
code to be executed;
}

Sample :
The sample below would show a loop to print verbosely the contents of a given array.


$a=array("three", "two", "one");
foreach ($arr as $value)
{
echo "Value: ". $value . "
“;
}
?>


PHP Functions

Now that we know the basic loop statements that are commonly used in PHP, we come to one of PHP’s real power in the form of it’s built in functions. PHP has over 700 built-in functions which gives the programmer a lot of quick and fun ways to do things they would otherwise have to create code for. Functions are simply blocks of code that are pre-made and called upon when needed to perform their task returning control to the calling statement after execution. In PHP, one creates functions by using the “function()” statement, which is then assigned a name that is simple yet descriptive of the process the function is to do (though this is not necessary, it would be nice to have a function that is easily determined through its descriptive name), that way when you debug programs you can have a general idea what that particular function is supposed to do. a function can start with an underscore or a letter but never a number. As the program starts, the “{” signals the start of the function after the opened curly braces, the function code or block of code is then inserted then followed by the closing curly brace, “}” to close the function.

A sample of a function will be given on the next post.

Function Samples

Sample :

function displayMyName()
{
echo "Juan Dela Cruz";
}
displayMyName()
?>


Sample use of a function in a PHP script.


function displayMyName()
{
echo "Juan Dela Cruz";
}
echo "Hello World!
“;
echo “My name is “;
displayMyName()
echo “That’s right, “;
displayMyName();
echo ” is my name.”;
?>


The output of the sample would be:

Hello World!
My name is Juan Dela Cruz.
That’s right, Juan Dela Cruz is my name.

More on functions in the next post as we discuss further the possibilities of these powerful ready made blocks of code.