Basic PHP Tutorial

Ghost

Seasoned Veteran
Blogger
Jun 25, 2009
3,198
185
213
Earth
wubur.com
FP$
2,483
Today I am going to go over the most basic PHP so you can learn how to do various things with the language. This post covers beginner topics that can get you started. I will show you how to store information in your script, show it on the page, and set up a basic If / if-else statement. Even if you do not want to learn the language in full, it can help you understand some code on your current website (forum, blog, etc) if it uses PHP. This tutorial is very basic, but if you want to know more you can reach out to me here or contact me at our official company (Wubur) website. We are a development and marketing company! You can also check out my Basic JavaScript Tutorial if you are interesting in creating modern user displays, manipulating the page, and taking user information from forms to do cool things!

First off, we should quickly go over what PHP is primarily used for and what it's good/bad at.
  • PHP is a server side technology, which means your actual server (or host) is running the script
  • For that reason, faster, more efficient, and more expensive servers can run a script better than an older, slower, and probably much more affordable server
  • However, PHP is a fairly basic language and good code can be run quickly and well on almost any web server.
  • PHP can do things like generate blocks of HTML to show on a page, handle things like changing what the user sees (requires a page refresh), but these are best left to raw HTML and JavaScript rather than using the server side language to manage a user side visual
  • PHP is a great language to learn if you want to store user information in a database or file, allow people to share images/videos/files, let users log in to a secure dashboard or access account features, sell products online, and much more.
There is a lot more to it, but let's jump into some basic PHP!

We start coding in PHP by creating a file ending with .php (example: index.php)
Even once this is done, we cannot just open up the PHP file with Chrome, Firefox, or another internet browser. This is because PHP is not like HTML/CSS/JavaScript (which are automatically interpreted by web browsers). It's a server side language, and that means the server must be set up to run PHP. I will not cover that process in this article. For most of you, if you own a website or plan on having one you will be able to upload PHP files to your web hosting provider and run them without an issue. This is because PHP is used in a large amount of web scripts / softwares, including the most famous paid & free forum software available.

If you have a website already and want to follow along, create a folder on your website. Go inside this folder and create the file 'tutorial.php'. Then in your browser you can navigate to Your-website.com/folder-name/tutorial.php

Inside tutorial.php, we will write all of the PHP mentioned below.
You must begin a PHP file with the "php tags". These let the server know that you are using PHP code. You might think that it's obvious because it's a ".php file", but you can still include non-PHP in a .php file. For example, you can use PHP at the top of the file and then HTML at the bottom, or vice versa.

Let's start with that... Anything in italics should be considered PHP code from now on!

tutorial.php
<?php
// opening tag

/* Now let's write the 'php end tag', which lets our server know that the php code inside tutorial.php is ending.
It's even more simple than the opening tag! */
?>

As you can see above, I did a few things. I wrote the PHP start tag, and then I used // (double slash).
The double slash is used to start a 'one line comment'. The comment cannot extend on to two different lines.

Then below, you can see I used /*, which starts a 'multi line comment'. I then wrote a couple sentences on two different lines, and then ended the multi line comment with */
Comments are used to remind you and anyone else working on the script what the code is for. I highly recommend using comments throughout your code, but remember that having too many comments can often be nearly as bad as having no comments. It should also be noted that sometimes companies will release their code without comments if they do not want their code to be easily understood or overly criticized by competitors. This is why you may see some code without any comments, even if those comments WERE included in the code when it was being developed.

Now, we have got a basic PHP file with the start and end tags, and some comments. Now let's learn how to actually set a variable. A PHP variable is basically much like a variable in math. You can say that y = 3, and if you have 2y, it's the same as saying 2*y, or 2*3 (2 multiplied by 3). So if you said that y = 3, and z = 2y , then z = 6. A variable is just a way to store a piece of information. In PHP you can store a lot of things, but let's start with storing basic numbers and 'strings'. Strings are words or sentences of regular text.

tutorial.php
<?php

/*
Set a variable:
A variable in PHP starts with the dollar sign ( $ )

You give the variable any name/title/label that you want. Variables have to begin with a letter (a-z) or an underscore ( _ ) and cannot include spaces or symbols. For example, $1a is not a valid variable, but $a1 or $_1a are both valid.

After you declare your variable, you then use the equals ( = ) sign. After that you can assign many things, but we'll start by just assigning my name as a string, and my age as a number (integer).

After that we need to tell PHP that the line of code is 'done'. We use a semi colon ( ; ) to let that happen. If you forget the semi colon then the PHP script will not function properly.
*/

$name = 'William'; // this is a string
$username = "Ghost"; // this is also a string
$age = 25; // this is an integer

?>


In that example, I showed you how to declare a variable for a string or an integer. I also showed you two ways to declare the string. You will notice that I used single quotes ( ' ) to surround my name, and double quotes ( " ) to surround my username. Both of these are valid in PHP, and a string must always be surrounded by either single quotes or double quotes. You cannot switch it up mid string like this: 'william" -- As you can see there, I started with a single quote, but ended with a double quote. That would cause an error. I could however do something like this: "william's apples" -- I started and ended with double quotes and because of that I was allowed to actually include a single quote inside the string without causing an error. Whatever you start with, you must end with!

Keep the code you wrote above and let's add some more! You can see the finished version of this script at the bottom of this article to make sure you did not forget anything!
tutorial.php
<?php

// let's show our variables on the page!
echo $name;
echo $age;
echo $username;


?>

In this example, we use 'echo'. Echo basically tells the script that you want to show the information on the page. If you go to yourwebsite.com/folder/tutorial.php, your script should be showing your name, age, and username. If your variables are NOT declared above the echos, PHP will have no values to show on the page.

However, there's a "problem" in that example.
With the way I wrote that code, it would show "William25Ghost" because I did not add any spacing. There are plenty of ways to add spacing.

tutorial.php
<?php

echo "Example 1: $name $age $username";
echo "<br />"; // echoing HTML is easy
echo "Example 2: " . $name . " " . $age . " " . $username;

?>

In example 1, use double quotations (very important for this example) to include variables inside of my string being echo'd.
This shows on the page: Example 1: William 25 Ghost
If you use single quotes, PHP will not let you do that. Basically when you use double quotes for a string (whether it's being declared or echo'd) you can put other PHP variables inside of it. The next example will show you that being done.

In example 2, I used something called concatenation. This is when you use a period ( . ) to add different variables or strings together. I start by echoing "Example 2: " -when I use the period after it, I am basically telling PHP "AND also echo ...". You will see a better example of concatenation below.
This shows on the page: Example 2: William 25 Ghost

Both examples show the same thing. You will notice that I also chose to echo some HTML (a <br /> tag) to tell the page to show Example 1 and 2 on two different lines. You can always include valid HTML in your PHP strings to make your content appear better. You can make your information be echo'd out by PHP with different fonts, colors, etc - just make sure the HTML is valid! Remember, PHP is a server side code, so using it to style a display for a user or even add information to the page is not always wise. The industry standard is to limit PHP's interaction with your designs as much as possible and instead use PHP to send and retrieve data, while using JavaScript or extremely limited PHP to display it.

tutorial.php
<?php

$name = "William";
$username = "Ghost";
$age = 25;

$sentence = "$name's username is $username and his age is $age.";

echo $sentence;
?>

If you go to your tutorial.php page, you should now have a sentence on one line that shows:
William's username is Ghost and his age is 25.

Now that I have shown you how to declare variables and show them on the page, I will show you how to create an "if statement". What this means is your script is asking "if this is true" and if so, "do this". There are many ways to set these up, but let's stick to a very basic if statement for this tutorial.

tutorial.php
<?php
$name = "William";
$username = "Ghost";
$age = 25;
$lovesPHP = 1;

// our original sentence
$sentence = "$name's username is $username and his age is $age.";

// let's set up an If statement for whether they love PHP or not
if($lovesPHP === 1){
$extraSentence = "He loves PHP.";
} else {
$extraSentence = "He does not love PHP.";
}


// echo our full combined sentence
echo "Full Sentence: $sentence $extraSentence";

?>


In this example we see: William's username is Ghost and his age is 25. He loves PHP.
If we change $lovesPHP to equal ANY other number than 1, we would get:
William's username is Ghost and his age is 25. He does not love PHP.

In that if statement we are saying "if the variable $lovesPHP does equal 1 { do this..... } otherwise do this instead { .... } "
You can also do something like this:
<?php

if($lovesPHP === 1){
$extraSentence = "He loves PHP.";
} else if($lovesPHP === 0){
$extraSentence = "He does not love PHP.";
} else {
$extraSentence = "He does not know what PHP is.";
}


// echo our full combined sentence
echo "Full Sentence: $sentence $extraSentence";

?>


In the case, it will only say I love PHP if $lovesPHP is equal to 1, only say I do not if it is equal to 0, and if it's ANY other number it will say I do not know what PHP is. As you can see, you can have any number of if statements, else statements, or else if statements. Else-if basically allows you to check multiple different values or statements before going to the catch all "and if none of that is true, do this" } else { statement. If you do not include the final else statement at the end, that's fine, but you may run into issues if another part of your script doesn't anticipate that. For example, if I do not include that final } else {, my script would not have access to $extraSentence variable because it's not declared, and that would happen if I made $lovesPHP equal to anything other than 0 or 1.

Now, I want to give you a final example of why all of this is useful. I will be only using the knowledge taught in this mini tutorial, but it will hopefully give you a better idea of how to apply your new skills!

newfile.php
<?php
$siteOpen = 1;
$siteName = 'My Site';
$siteDesc = 'A description about my site.';

// now we will end our php tags to include some HTML after it...
?>

<html>
<title>
<?php echo $siteName; ?>
</title>

<body>
<h1><?php echo $siteName ?></h1>
<?php

if($siteOpen === 1){
echo $siteDesc
} else {
echo "$siteName is not currently open.";
}

?>
</body>
</html>

--------

If that's our new file, you will notice that the browser window/tab title is our site name, and the site name is also included on the page in h1 (header tags), with the description under it. We also set up an if-else statement to either show the description or say that the site is not open. It's not ideal to be mixing PHP into HTML, but without going into a lot of detail, I cannot accurately explain the best practices for retrieving data with PHP & showing it on the pages. That last example hopefully gives you a good idea of how you can use PHP variables to set up your web pages with HTML templates. For example, a user profile page can rely on ONE version of an HTML design, and use PHP to fill that template with data. That way when you want to redesign the profile page, you only edit one file instead of having to edit a separate HTML file for every profile page. This is called a dynamic (non static) website. Often the data will be stored in a database, PHP will retrieve that data and set variables for all of it, and then show it inside the HTML template.

This is how you have things like ?forum=63...
The script says "oh forum = 63, let me pull the data for forum ID 63 from the database... then echo $forum_name, $forum_desc, etc on the forum HTML display template".

Anyways, this ended up being a lot longer than anticipated, but should hopefully give you a good idea of how to start learning PHP. It really is a relatively easy language to learn, and it can be so useful to have these tricks up your sleeve.

Here's our finished copy of tutorial.php:

<?php
// always start with <?php to let server know you are running php code


// let's set some variables:
$name = 'William'; // this is a string , with single quotes around it
$username = "Ghost"; // this is also a string , with double quotes around it
$age = 25; // this is an integer

/*
let's show our variables on the page!
this example shows "William25Ghost
*/
echo $name;
echo $age;
echo $username;


echo "<hr>"; // separate example with a line

/*
let's echo out our variables with some HTML and concatenation below!
woo!
*/
echo "Example 1: $name $age $username"; // double quotes lets you use variables inside a string
echo "<br />"; // echoing HTML is easy
echo "Example 2: " . $name . " " . $age . " " . $username; // concatenation ( .) lets you build a string


echo "<hr>"; // divider

/*
Let's use if, else-if, else logical statements to decide what text to show!
*/
if($lovesPHP === 1){
$extraSentence = "He loves PHP.";
} else if($lovesPHP === 0){
$extraSentence = "He does not love PHP.";
} else {
$extraSentence = "He does not know what PHP is.";
}


// echo our full combined sentence
echo "Full Sentence: $sentence $extraSentence";
// you can change the values of $lovesPHP to see the differences!


?>

You can play around with this code to get an idea of how it all works, or even try adding some PHP to your existing web pages! Remember, every PHP page must end in .php, so if you have a file called "home.html", you can just convert it to "home.php" to start using PHP inside of it and expect it to work!

I hope this tutorial helped you, whether you're from FP or from a search engine :)
 
Last edited:
  • Like
  • Useful
Reactions: edenwax and Jon

Ghost

Seasoned Veteran
Blogger
Jun 25, 2009
3,198
185
213
Earth
wubur.com
FP$
2,483
This is really great @Ghost, I had a friend just yesterday ask me if I could point them in the right direction with PHP and I'm going to be sure to send them over here to have a read. I appreciate your taking the time to share this amazing tutorial with the Forum Promotion community! :bluesquarerolleyes:
Awesome danthex, that's great! You should definitely send them a link to this. I wanted to go into a lot of detail because PHP is a bit "messy" in the sense that a lot of different things will "work", but aren't always the best, and it can be a little tricky to debug things if you aren't aware of all the rules. ... so, if they have any questions just let them know to message me!

very good.. very helpful. even that I didn't even read it all
I appreciate the reply Empire! Of course if you aren't learning PHP, there's no need to read it all :)
 
  • Like
Reactions: Jon