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 »
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 »
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 »
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 $_GET variable is an array of variable names with the corresponding values sent by the HTTP GET method. It is used to collect values from a form by using the method=”get”. All information sent by a form using this method is visible to all on the browser’s address bar but it is limited to only 100 characters.
Sample:
If the user clicks on the submit button, the address bar would look like the output below:
http://www.samplepage.com/welcome.php?name=Ernie&age=35
Take note that the variable contents and names are verbosely displayed so do not forget to take that into account when using the variable for forms processing.
Filed under: Basic Programming, Sample Code by Avatar
No Comments »