As the previous post discussed, PHP is very easy to use with web pages for the language has all the elements of a web page as a useable element. Now, going forward, forms or pages are easily handled and checked on the user end, meaning when you open a page the data input is easier to verify on the user side rather than have the input sent back to the server for checking and then returning any errors should there be any. This reduces load on the server and network having the data validated on the user end, allowing the information to be sent when the form receives all correct input. This type of handling is called client side validation where the input page verifies itself only requesting for the next step in a process when it returns true (all entries are correct). This way, the error checking and return function is on the client side reducing network and server load.
Filed under: Basic Programming 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 »
PHP as a scripting language means it is designed to work with ease of use on web page designs. Such pages often require the user to input data which can then be treated as input to the various elements of the program that are contained within the script to obtain the desired result. The PHP $_Get and $_Post, are samples of variables that are used to obtain information from forms like the registration form on a membership page.
HTML forms and PHP work hand in hand together in such a way that any element in an HTML page is readily available to PHP scripts.
Sample Form:
Filed under: Basic Programming, Sample Code by Avatar
No Comments »
That’s how easy a function can be made to do a series of operations by being influenced by parameters. We now focus on the area of having functions that returns a value, say the processing of a block of code or an operation done on a set of data.
Sample :
function add($a,$b)
{
$total = $a + $b;
return $total;
}
echo "1 + 15 =" . add(1,15);
?>
The output of the code would be:
1 + 15 = 16
Next we focus on forms and user input as we go on the fascinating world of the powers of PHP as a scripting language.
Filed under: Basic Programming, Sample Code by Avatar
No Comments »
Now, the first sample PHP function did something very simple that it displayed a string that does not change and was not influenced by any parameters. We then move to show functions with one or more parameters that make them more powerful and diverse. That is done by placing these parameters inside the “()”.
function displayMyName($myfName,$mark)
{
echo $myfName . "Dela Cruz" . $mark . "
“;
}
echo “My name is”;
$displaymyName(”Juan”,”.”)
echo “My name is”;
$displaymyName(”Jack”,”.”)
echo “My name is”;
$displaymyName(”June”,”.”)
?>
The output of the sample would be :
My name is Juan Dela Cruz
My name is Jack Dela Cruz
My name is June Dela Cruz
Filed under: Information 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 »
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 »
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 »

Sometimes, the things matters. If you write this test program in PHP 4 (tested on 4.4.7):
< ?php
function test($arg) { echo "talk like a parrot."; }
test();
?>
An error message pops up like the one below:
Warning: Missing argument 1 for test() in /usr/bin/- on line 2
The error warning illustrates the position of the purpose of the function, but really the mistake was in how the function was called. The necessary parameter to test did not go smoothly. This error will most likely irk you, forcing you to check a stack trace and find the actual error. For beginners, this maybe a death sentence.
But if you are using the new PHP 5 and run the same message, it will come out like this:
Warning: Missing argument 1 for test(), called in /Users/john/- on line 3 and defined in /Users/john/- on line 2
Awesome Improvement!
Filed under: Sample Code by Celine
No Comments »
The for statement on the other hand is used for loops that have a defined number of repetitions. The “for” statement requires three parameters; the initialization of a variable that would be incremented, the condition that is to be satisfied and the number of times the block of code is to be executed. If more than one variable is needed for the increment parameter and initialization, they should be separated by a comma. The condition must evaluate as either true or false.
Syntax:
for ($a=1; $a<=5; $a++)
{
echo "Hello World!
“;
}
?>
The sample code would result in a display of the “Hello World!” statement for 5 times.
Filed under: Basic Programming, Sample Code by Avatar
No Comments »