PHP is a scripting language used on a multitude of websites and servers alike. It stands for – Hypertext Pre-processor and is one of the easiest and most effective ways of making a web page interactive. It is so simple that you can create PHP pages using a simple program like Notepad. PHP pages do, however, require certain software like Oracle being installed on servers or PC’s in order for the query language to run on the given machine. This is not a major set-back as one can download this software for free from the internet and most web-hosting companies already have this software installed on their servers and thus support this format. In this light, let’s look at just what it takes to begin using PHP – creating the classic “Hello World” program and then showing the reader how to get and display input on a web page. Let’s get started
3 Great Tools for Introducing Programming to Your Kids
To begin, a PHP page must include a PHP file extension. So open Notepad. Select the ‘File menu’, select ‘save as’ – and now comes the tricky part, now click change file extensions to all (*.*) – do not forget this or the given PHP file will not be saved correctly – then type your filename (testfile) and then the file extension which is: “.php”. Okay – now we have a PHP file open. Let us start scripting.
In order to let the server, PC or web-browser know that we want to start scripting in PHP the following tag must be included: “” – to close the tag at the end of our script or code. As PHP is very similar to the C programming language it is case sensitive, or in other words make sure that you use the correct capitalization. Also like in the programing language C a semicolon must be included at the end of each line. Do not forget this or your script will not run – at all!
Let’s get started on our Hello World application. Let the machine know you are entering PHP code – as demonstrated earlier. Now, just like all other real programing languages (which html is not!) PHP uses certain syntax to perform a task. In order to display something on screen a simple PHP command is: ‘echo ‘. So, type the following:
Now save the file as ‘helloworld.php’.If you have Oracle or other software that supports PHP installed on your PC than simply open this file in your web browser and as you can see, our application is working. If not upload the file to a serve or web-hosting company server that supports PHP and then open the file. You should see a simple screen displaying the text: ‘HelloWorld’
Okay so we can display strings. It’s a start – but how about capturing and displaying user input? These tasks can be used in a number of scenarios. In this line of thought, to get input from a user one would use the “form post” command and post data stored in variables to, normally, a process page which captures and processes the data. Let’s take a look at how this is done.
Firstly, we need to create a form to capture input from the user. To do this, create a new PHP file as demonstrated earlier. Save the file as: ‘ Formpost.html’ . Input the default HYML tags:
<title>Form Post Example- User Input</title>
Now Input the following html tags ‘between the body tags to create our form:
<form action=”process.php” method=”post”>
<p>Your name: <input type=”text” name=”name” /></p>
<p><input type=”submit” /></p>
Okay, let me explain what we have just done. The first line of code creates a form that passes data to a page named “process.php” using the “post” method. So you specify the page that you want to send data or input to in the “action” attribute of the form tag. And we specify the form post method by setting the method attribute to “post “.
Following this we created a text input field on the page which captured the input in the form of “text” and saved it in a variable named “name”. Therefore we specify data type with the input type attribute of the: “input” field and we specify the variable name with the: “name” attribute. Don’t forget the submit button to send the data. Save the file.
Okay – now we have a form that captures input – someone’s name. Let’s process and display the data now. Create a new PHP file named process. Input the default header and body tags then input the following:
$name1 = $_POST[‘name’];
echo “Hello “;
Let’s take a look at what we have done here. Firstly we created a variable for capturing data saved in the variable name “name” using the “$_POST” command. To use this command simply include inside of the square brackets single quotation marks the name of the input variable to be posted to this form and include before it the variable that you wish to store the data. In PHP a variable name must have a “$” in front of it – do not forget this. Remember semicolons and capitalization. Save the file.
Now we have two new PHP files to open or upload depending on your software as mentioned earlier. If needs be upload the files – to the same directory on your PHP friendly web server. If this is not needed open : “Formpost.html” from your machine.Input your name as prompted and click on “Submit query”. And what do you know – we can capture data and now display it on screen. This might not seem like much but it is the beginning to a lot of useful projects such as creating password protection, user accounts and a guestbook to name just a few.
To conclude, in this tutorial you learned what PHP is, how to use it to display text and finally how to capture and display input on screen and pass data between two forms using the form post command. Although none of these tasks seems like that much of a feat, these are some of the building blocks we need to learn in order to create bigger, more interactive projects.