- Computers & Software»
- Computer Science & Programming»
- Programming Languages
PHP Tutorial: A Beginner's Guide on Writing PHP Scripts
A beginners guide in Learning PHP
This hub offers the step by step processes on how to program using PHP. You can program or make PHP scripts on PHP editors, such as Notepad++ and Netbeans IDE. If you do not know what PHP is, and what you need to learn about PHP; you can read this hub first. In learning PHP, it is advisable to know a bit of HTML Basics. HTML wouldl help you a lot in understanding PHP better.
Anyway, granted you already have a background on HTML Basics; let's now start learning PHP.
I used to code PHP scripts in Notepad++ as well as in Netbeans. For the beginners, coding in Notepad and/or Notepad++ is the best way to sharpen their skills in coding PHP. Because using those editors require hard coding. It means no drop and down features, which are essential on the beginner's learning in using and manipulating the language well.
Don't Mess With Programmer Shirts
Before Coding, What You Need To Do First?
1. Open a Notepad++ or Netbeans editor. But, I suggest you use Notepad++. If you do not have this editor yet, you can google and download notepad++ for free. Just type on google search box the notepad++ free download.
2. Open the PHP server, preferably XAMPP. IF you do not have XAMPP, download free XAMPP also. If you have XAMPP but do not know how to use it. Read on how to run php code with XAMPP here.
Granted that you already done the requirements above. Now let’s begin on learning the PHP basics.
The Steps on How to Program Using PHP
Step 1. Use the PHP Opening and Closing Tags
PHP like other scripting and server-scripting languages has also its own opening and closing tags. Without it, the PHP codes or scripts would not be executed by the PHP servers. So in coding PHP, it is important to note that before you can make any PHP scripts you should type first the opening and the closing tags.
Opening Tag: <?php
Closing Tag: ?>
See code below:
<?php ...php scripts here ?>
Step 2. Know How to Declare Variables in PHP
PHP has also it own share on how to declare variables. The function of variables in PHP is similar as the other programming languages. They are also meant to hold numbers, characters and string. However PHP has no data types. The declaration of variables comes with the dollar sign ($), name of the variable and ends with semi-colon (;). If you want to instantiate a variable with a string, or character, or number, see the code below for the right declaration.
//sample for declare variables <?php $firstName; $lastName; $position; $salary; ?> //sample on how to instantiate variables with value <?php $firstName = "Andwer"; $lastName = "Morphy"; $position = "CEO"; $salary = "$10000"; ?>
Step 3. Input and Output on PHP
Before we go to how to read the input in PHP programming, let us look first on how to output a string of characters.
To output string, you can use the echo or print() function:
<?php echo"This is how to print words in PHP...<br/>"; print('And this is the other way'); ?>
To get the value of the input in PHP, the learner should at least know the basic of HTML. For we will be tackling about html forms here. The concern functions on getting the input in PHP are the the $_GET and $_POST built-in functions. These 2 functions were built to get the information entered by the user.
What is the difference and the similarity of $_GET and $_POST functions in PHP?
When a PHP Programmer use the $_GET to get the information, the information that would get by the $_GET function would be shown on the URL of the page, while the $_POST makes it invisible from the user's browser.Clearly, the most secure to use in getting the input is the $_POST, though there are also situations that the $_GET seems the better one to use.
How to use $_GET and $_POST?
Good question. To use it, we must first design an html form. To make things simpler, my example is the signup.html that has only two input fields, the username and email address. See the code below.
<html> <form action="print.php" method="POST"> <table> <tr> <td> UserName: </td> <td> <input type="text" name="username"> </td> </tr> <tr> <td> Email Address: </td> <td> <input type="text" name="emailAdd"> </td> </tr> <tr> <td> </td> <td> <input type="submit" name="submit" value="Submit"> </td> </tr> </table> </form> </html>
Sample Code Output
Now, to get the information entered by the user in those blank fields and by clicking the submit button, the $_POST on another PHP file named print.php would be used. Review the html form again. You would see this: <form action = "print.php" method = "POST">, the action there means that after the user clicks the submit button, he/she will be directed to print.php file, while the POST on the method means that the $_POST function will be used to get the information entered by the user. You could also replace it by GET, if you want to use the $_GET function. The code for the print.php is the following:
<?php $username = $_POST['username']; $email = $_POST['emailAdd']; echo "New User: ".$username. "<br />"; echo "Email Address: ".$email; ?>
The situation is this: If the user will enter his/her information on the signup.html and clicks submit, the user will be directed to the print.php which will display the new user and his/her email address. The print.php scripts get only the information entered by the user and display it on the browser. To understand better, refer to the photos below:
User enters her username and email address and clicks submit
User directed to print.php and the PHP file get and display the information entered by the user
If you want more example on $_GET and $_POST, refer to the link below this hub.
The Part II of this tutorial will include:
-Learning the different PHP Operators
-Learning Different PHP Operators
-Learning how to manipulate PHP Loops
The Part III of this tutorial will include:
-Learn how PHP Arrays behave
-Learn how to make PHP Functions
Are you interested in PHP Programming?
Other Sample Codes and Tips in Learning PHP
- How to become a PHP Developer: Learn and Earn
- How to run PHP Code or PHP Scripts and See the Result on Your Browser
- HTML Basics: Learn The Basics of HTML and Create Your Own Webpage on Your Browser
- PHP Sample Codes On Function:How to make Function in PHP
- PHP Code Example on Associative Array and Multidimensional Array
- PHP Code Example on Foreach Loop and For Loop
- PHP Code Example on Do-while Loop
- PHP Code Example on Numeric Array
- PHP Code Example on While Loop
- PHP Code Example on Switch Statement
- PHP Code Example on if else-if Statement
- PHP Code Example for if-else Statement
- PHP code Example for if statement