ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel

PHP Tutorial: A Beginner's Guide on Writing PHP Scripts

Updated on May 31, 2013
Source: Drupal.org via google images
Source: Drupal.org via google images

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.

Source: oss.sgi.com via google
Source: oss.sgi.com via google

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:

Example:

<?php
	echo"This is how to print words in PHP...<br/>";

	print('And this is the other way');
?>

Sample Output:

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?

See results

Comments

    0 of 8192 characters used
    Post Comment

    • profile image

      Sham 

      4 years ago

      This is awesome... Thanks a million

    working

    This website uses cookies

    As a user in the EEA, your approval is needed on a few things. To provide a better website experience, hubpages.com uses cookies (and other similar technologies) and may collect, process, and share personal data. Please choose which areas of our service you consent to our doing so.

    For more information on managing or withdrawing consents and how we handle data, visit our Privacy Policy at: https://hubpages.com/privacy-policy#gdpr

    Show Details
    Necessary
    HubPages Device IDThis is used to identify particular browsers or devices when the access the service, and is used for security reasons.
    LoginThis is necessary to sign in to the HubPages Service.
    Google RecaptchaThis is used to prevent bots and spam. (Privacy Policy)
    AkismetThis is used to detect comment spam. (Privacy Policy)
    HubPages Google AnalyticsThis is used to provide data on traffic to our website, all personally identifyable data is anonymized. (Privacy Policy)
    HubPages Traffic PixelThis is used to collect data on traffic to articles and other pages on our site. Unless you are signed in to a HubPages account, all personally identifiable information is anonymized.
    Amazon Web ServicesThis is a cloud services platform that we used to host our service. (Privacy Policy)
    CloudflareThis is a cloud CDN service that we use to efficiently deliver files required for our service to operate such as javascript, cascading style sheets, images, and videos. (Privacy Policy)
    Google Hosted LibrariesJavascript software libraries such as jQuery are loaded at endpoints on the googleapis.com or gstatic.com domains, for performance and efficiency reasons. (Privacy Policy)
    Features
    Google Custom SearchThis is feature allows you to search the site. (Privacy Policy)
    Google MapsSome articles have Google Maps embedded in them. (Privacy Policy)
    Google ChartsThis is used to display charts and graphs on articles and the author center. (Privacy Policy)
    Google AdSense Host APIThis service allows you to sign up for or associate a Google AdSense account with HubPages, so that you can earn money from ads on your articles. No data is shared unless you engage with this feature. (Privacy Policy)
    Google YouTubeSome articles have YouTube videos embedded in them. (Privacy Policy)
    VimeoSome articles have Vimeo videos embedded in them. (Privacy Policy)
    PaypalThis is used for a registered author who enrolls in the HubPages Earnings program and requests to be paid via PayPal. No data is shared with Paypal unless you engage with this feature. (Privacy Policy)
    Facebook LoginYou can use this to streamline signing up for, or signing in to your Hubpages account. No data is shared with Facebook unless you engage with this feature. (Privacy Policy)
    MavenThis supports the Maven widget and search functionality. (Privacy Policy)
    Marketing
    Google AdSenseThis is an ad network. (Privacy Policy)
    Google DoubleClickGoogle provides ad serving technology and runs an ad network. (Privacy Policy)
    Index ExchangeThis is an ad network. (Privacy Policy)
    SovrnThis is an ad network. (Privacy Policy)
    Facebook AdsThis is an ad network. (Privacy Policy)
    Amazon Unified Ad MarketplaceThis is an ad network. (Privacy Policy)
    AppNexusThis is an ad network. (Privacy Policy)
    OpenxThis is an ad network. (Privacy Policy)
    Rubicon ProjectThis is an ad network. (Privacy Policy)
    TripleLiftThis is an ad network. (Privacy Policy)
    Say MediaWe partner with Say Media to deliver ad campaigns on our sites. (Privacy Policy)
    Remarketing PixelsWe may use remarketing pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to advertise the HubPages Service to people that have visited our sites.
    Conversion Tracking PixelsWe may use conversion tracking pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to identify when an advertisement has successfully resulted in the desired action, such as signing up for the HubPages Service or publishing an article on the HubPages Service.
    Statistics
    Author Google AnalyticsThis is used to provide traffic data and reports to the authors of articles on the HubPages Service. (Privacy Policy)
    ComscoreComScore is a media measurement and analytics company providing marketing data and analytics to enterprises, media and advertising agencies, and publishers. Non-consent will result in ComScore only processing obfuscated personal data. (Privacy Policy)
    Amazon Tracking PixelSome articles display amazon products as part of the Amazon Affiliate program, this pixel provides traffic statistics for those products (Privacy Policy)