Learn PHP for beginners

Dead-i

x10Hosting Support Ninja
Community Support
Messages
6,084
Reaction score
368
Points
83
Hello there! I've briefly looked around for a basic PHP tutorial and I haven't found one yet, so I thought I'd make one myself. In this tutorial, I will show you the basics of PHP, what PHP is and how you can incorporate it your website. X10Hosting offers PHP 5.3.0 currently, so you can take advantage of the vast range of features PHP has to offer. There are many opensource softwares that are written in PHP, so PHP is definately a good language to lean.

What is PHP?
PHP is a Hypertext Preprocessor language that allows developers to create complex or simple scripts that perform actions instead of boring, static HTML. For more information on PHP then look at the Wikipedia Article. Also, what is a better place to look than the Official PHP website?

Beginning a PHP document
All PHP documents are given the extension .php. This tells the server that the contents of the document should be parsed by PHP and not just output the source code. Unlike other languages like Javascript and HTML, your documents source code is hidden from the user. This gives you ultimate security, and protects your precious source code from the user's view. So, start editing your PHP document with an editor like Metapad or the cPanel File Manager's Code Editor and get started. All PHP scripts start with <?php and end with ?>, so let's start off your document:
PHP:
<?php

?>
Tip: You can also use <? to start off a PHP script if your server supports it and your php.ini (configuration file) has short tags enabled. However, <?php is the most supported and widely used, so let's just stick with that.

Simple Statements
So now that we have our PHP document all setup, let's start off with coding a few simple statements into our PHP document. Inside your <?php and ?> tags, we're going to use the ECHO statement to give the user some HTML output. Let's echo 'Hello World', and examine the code that we use to do that.
PHP:
<?php
echo "Hello World!";
?>
If you now navigate to the PHP document in your web browser, you will see this:
Hello World!
And now we have created our first PHP document! Let's examine the code that we used. We entered 'echo' to tell PHP we would like to state ECHO, and then we entered "Hello World" which defined the output. Notice that we have a semi-colon at the end: Since PHP doesn't recognize new lines, we must enter a semi-colon to state that the echo statement is finished.
Tip: Notice that it our code we wrapped Hello World with speech marks. You can also use apostrophes, like 'Hello World' instead.
Note: On the very last statement (only) of your PHP script, you can remove the semi-colon if you wish. However, it is very good practise to keep it on, so I recommend you leave it for the time being.
Warning: If you see the source code of the document when you navigate to it in your web browser, it means PHP is incorrectly configured on your webserver.

Conditional Statements
So far we have made simple boring text in our PHP document which we could have accomplished with HTML anyway. So let's start getting a little bit exciting and show you what makes PHP unique to HTML: conditional statements. With PHP, we can use the IF statement ELSE statement and ELSEIF statement to perform simple checks with PHP. But first, let's take a minute to talk about variables. Imagine variables as little boxes that hold a bit of data; they could be a string of text or a number, depends how they are configured. You can set a variable like this:
PHP:
$hello = "Hello World";
The above statement assigns the $hello variable to a string called 'Hello World'. This allows us to us this variable later in the script. We're going to use variables now, in a conditional statement:
PHP:
<?php
$hello = "Hello World";

if ($hello == 'Hello World') {
echo "Positive";
}else{
echo "Negative";
}
?>
In the above code, first we assign the Hello World string to the $hello variable. Then, we check whether $hello is Hello World with the if statement. If $hello is Hello World, then it will output Positive, else it will be Negative. Try this out for yourself; and run it in your browser:
Yes, it's positive because $hello matches 'Hello World'. Notice that in the if statement we used ==. This is an operator, which means equals. There are also many different operators, like != for doesn't equal, > for more than, < for less than, >= for more than or equals, and <= for less than or equals. So now that we've made a simple conditional statement, let's change the hello variable slightly. Change the $hello variable to something else and see what happens.
PHP:
<?php
$hello = "Bye World";

if ($hello == 'Hello World') {
echo "Positive";
}else{
echo "Negative";
}
?>
It's negative, because we set the $hello variable to 'Bye World' so the if statement returned false, triggering the }else{ statement. We can also use the }elseif{ statement, which pretty much does what it says on the tin. Let's try it out in another, similar script:
PHP:
<?php
$hello = "Bye World";

if ($hello == 'Hello World') {
echo "Hello to you too!";
}elseif ($hello == 'Bye World') {
echo "Goodbye then";
}else{
echo "Negative";
}
?>
And the browser returns
Goodbye then
Warning: In the event that you forget one of the } then PHP will give you an error that says unexpected $end.
Warning: In the event that you forget the semi-colon at the end of each statement, PHP will give you an unexpected T_STRING error.

Form Inputs
Another advantage of using PHP is it's ability to use the input from the user. In this tutorial I'm only going to talk to you about to methods: $_POST and $_GET. The POST array allows you to get the input from a HTML form that was submitted to the PHP document through a form. Take a look at the below script. I will explain the code bit by bit by using PHP comments, which you can use by entering // at the start of a line and then a comment. As an alternative to that method, you could also wrap comments round with /* and */, but // is easier. The file name must be name.php since that is what the FORM ACTION is pointing to:
PHP:
<?php

// If the form hasn't been submited
if (!isset($_POST['button'])) {

// Echo the HTML form
echo "<form method=post action=name.php>";
echo "What is your name: <input type=text name=name>";
echo "<input type=submit name=submit>";
echo "</form>";

// Otherwise
}else{

// Assign the form text box called NAME to a variable
$name = $_POST['name'];

// Echo the person's name from the $name variable
echo $name;

// Finish the if statement
}

?>
The above code is one of the most simplest ways you can use form input; there are many other ways using PHP's other functions that is available. You can use the input like any other string. The other array we're going to take a look at is the $_GET array, which gets any output from the URL. For example, if the URL was name.php?name=deadi then PHP would be able to grab the name (deadi) from the URL. To accomplish this, we can simply use the $_GET array:
PHP:
<?php

// Assign the $_GET input to a variable
$name = $_GET['name'];

// Echo the name variable
echo $name;

?>
Of course you can do much much more with this information.

Where to go from here?
In this tutorial I have only taught you the basics of PHP. There are many other functions available in the PHP universe, and many different ways you can use PHP with. I've tried to keep this tutorial as simple as possible, but there are also other ways you can use PHP, like MySQL for storing information in a database, SESSION for passing information from one page to another, and COOKIES to store data on the user's computer. I hope you have enjoyed this tutorial; thank you for reading :)
 
Top