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.
Filed under: Basic Programming, Sample Code by Avatar
No Comments »
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.
Filed under: Basic Programming by Avatar
No Comments »
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.
Filed under: Basic Programming by Avatar
No 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.
Filed under: Basic Programming, Sample Code by Avatar
No Comments »
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.
Filed under: Basic Programming, Sample Code by Avatar
No Comments »
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.
Filed under: Basic Programming by Avatar
No Comments »
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 . "
“;
}
?>
Filed under: Basic Programming, Sample Code by Celine
No Comments »
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.
Filed under: Basic Programming by Avatar
No Comments »
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.
Filed under: Basic Programming, Sample Code by Avatar
No Comments »
The sample page in the previous post contains two input fields that are name and age, when the user inputs the data and clicks on the submit button, the form’s data is sent to the file “welcome.php” file. The resulting php file would look something like this:
Welcome .
You are years old
The output of the script we made would look like:
Welcome Ernie
You are 35 years old.
That would be an overview of the full potential of the $_GET and $_POST variables and they will be discussed in depth later as we go deeper into their functionality in PHP scripts.
Filed under: Basic Programming, Sample Code by Celine
No Comments »