Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5

PHP - Part 1

How to Program PHP
Tutorial, Part 1

Ever wanted to get your feet wet with a programming language, but simply haven't had the money to pay for expensive books or online courses? Of course, any programming language takes time to master, but I will attempt to cover the introduction to the PHP programming language through these tutorials. I'm living proof that you don't need an $80 manual to learn a programming language. Due to the fact that I can't write 400 pages worth of really awesome content effectively without writing a book, I won't attempt to dive into concepts that are too deep, but I hope I'll be able to give you a good introduction to the language. If you'd like to go further, w3schools is a great reference resource that I've used, and once you've gotten your feet wet, it is a great resource to use if you want to continue past these tutorials.

I'm assuming that anyone reading this already has a good understanding of basic HTML, and at least knows what they are looking at if they see some javascript. If you are not at this stage, I won't attempt to review any HTML in these tutorials, so I'd recommend taking a good HTML course first. I would personally recommend if you are completely new to HTML, but if HTML is not new to you, then we're ready to dive in!

PHP introduction - part 1

What exactly is PHP anyway?

PHP is a programming language that runs straight from the server, and is used to power many websites. Forum software, social networking solutions, or any other software solution that works with dynamic content will usually use PHP (or a similar language) to generate a page, then will send it to the browser. PHP is extremely useful for that reason. It differs from HTML because it is not a markup language that simply defines the format that a page is sent to the browser with. PHP is a programming language that can be used to generate the HTML pages sent to the browser.

Of course, there are alternatives as well. PHP is probably the most common language used to achieve its task (for non enterprise use, at least), so this will be the first tutorial I will write here at Makestation.

Hold on, prep work first! ( skip this section if you already have a server or localhost )

While you certainly might be able to get familiar with some concepts you haven't seen before by simply reading these tutorials, you are much more likely to get the hang of any programming language by actually following along with the tutorials with your own scripts. You do not need to have a server to write and execute PHP scripts. For windows users, I recommend installing wampserver to your computer, which will allow you to run PHP files straight from your computer. If you cannot get wampserver to work, a simple free webhost such as zymic or 000webhost should be enough for this project. (Be sure to read your webhost's terms of service carefully if you choose to go that route. )

I would strongly recommend setting this up before continuing so that you can follow along with these tutorials with your own scripts. If you are a windows user, WAMP is probably the easiest route. Once you have that set up, simply copy/paste the scripts that are posted here into a .php file inside of the c:/wamp/www directory, and as long as wampserver is started on your computer, you can go to localhost/myfilename.php in the browser of your choice to execute it.

Your first PHP script

I've seen quite a few tutorials that spend quite a bit of time teaching the theory behind a concept before actually jumping to the concept itself. Personally, I'm the kind of guy who likes to just jump straight into something and see stuff happen, so I won't be wasting too much time. In these tutorials, I will try to give a brief introuction, go straight to the code, then explain it afterwards. If something looks a little confusing, don't worry. Just keep reading, and I'll explain it later on. So, without wasting time, here will be your first PHP script.

echo "this is my first php script!";

The first thing that is important are the <?php an the ?> tags. Simply naming a file .php isn't enough for PHP to actually execute the code as PHP. Without the <?php and ?> tags, the PHP server will assume that your file is actually an HTML file that is simply named as PHP file. In other words, all PHP code must be enclosed in the opening and closing PHP tags in order for it to actually be executed by the server.

The echo command is the command that sends data to the browser, which in this case is the "this is your first php script" message. This command is probably the most important command in PHP, since it's what sends data generated by the PHP script to the browser. (As stated before, no PHP code actually gets sent to your browser. PHP is all processed on the server, so all that the browser will ultimately receive is an HTML page generated and sent by the PHP script. )

Also, notice the semicolon (the ; character) at the end of the line. This character must go at the end of every statement in PHP. It essentially tells PHP that this command is complete, and that it is ready to execute. If the semicolon is missing, PHP will get confused and flag an error instead of executing the PHP code. It turns out that a missing semicolon is one of the most common errors in code that PHP programmers make. Whenever I see an error in my code, the first thing I'll usually check for is a missing semicolon.

Example 2:

echo "this is line #1";
echo "this is line #2";
echo "Why are you still reading this? ";
echo "forget it! PHP is easy! ";

As you can see, we've made use of more echo statements. Each one has a semicolon at the end of it to mark the end of the command. If the semicolon is removed, PHP will get confused as it executes the code because it won't know where the end of the statement is. Also, notice again how all code is inside of the <?php and ?> tags.

Introduction to PHP - part 1.5

We've just gotten into the absolute basics of how a PHP file could be structured, what we need to do in order to get it to execute, and how to send text to the browser, but there are a few other quick concepts we'll need to dive into before we enter the next tutorials. We'll be getting into more interesting concepts in the coming tutorials, but for now we need to make sure we understand what we need to know before moving on.

Anyway, the first concept we'll get into before heading into the next tutorials is the concept of the text string. This is really more a definition we'll be bringing up later. A text string is exactly what it sounds like. It's just a string of text. For example, for this line of code...

echo "hello world!";
"hello world" is just a text string. Now there is nothing complicated about that, but what if we need to merge two strings into one string?

echo "string 1 is " . "going to be combined with string 2";

Try running this script, and you will see "string 1 is going to be combined with string 2". Notice how the period between the two strings combined both strings into one. This is one of many "operators" in PHP that can manipulate data in different ways. PHP also has many additional operators. The simplest of these operators are the math operators (+, -, and so on... Tongue ) We'll get into more of these operators in the later tutorials. For now, I encourage you to try these scripts on your own to ensure you understand these concepts.

Either way, we'll probably be using the period to combine different strings quite frequently in later tutorials, so feel free to play around with the above example to get a feel for how it works.

PHP comments
In PHP, it is possible to make notes in the code without it actually executing as code. There are multiple formats that you can use to define any text in the code as a comment, but for these tutorials, we'll just put "//" at the beginning of the line. This tells PHP that the rest of the line is not code, so it will just skip over it. This is very useful for making notes to yourself about the code.

echo "hello world!";
// this is a comment! It will not execute, since we have two slashes at the beginning of the line. 

The next tutorial

In the next tutorial, we'll begin to explore variables. Once we get some basic concepts down in the next few tutorials, we can begin playing around with some interactive applications. Stay tuned! Big Grin

Next Tutorial: Introduction to Variables
- Directory of PHP Tutorials - Our next project...
Did you make this tutorial? It's really great!
Thanks Big Grin

I've been working on them slowly over the past few months, and I figured I'd post what I had written so far. - Our next project...
I'll definitely read these when i get some time. Thanks for making them! Smile
Just read this. Going to the next tutorial after i +rep you. Tongue

Possibly Related Threads…
Thread Author Replies Views Last Post
  PHP - Part 6 Darth-Apple 2 5,171 November 12th, 2013 at 5:31 PM
Last Post: Damian B.
  PHP - Part 5 Darth-Apple 5 8,761 August 28th, 2013 at 10:10 PM
Last Post: Darth-Apple
  PHP - Part 2 Darth-Apple 2 5,676 August 19th, 2013 at 9:26 PM
Last Post: Darth-Apple
  PHP - Part 4 Darth-Apple 0 3,556 August 13th, 2013 at 4:29 PM
Last Post: Darth-Apple

Users browsing this thread: 1 Guest(s)

Makestation Theme/Design Selector

Contact Us | Makestation | Return to Top | Lite (Archive) Mode | RSS Syndication 
Proudly powered by MyBB 1.8, © 2002-2021
Forum design by Makestation Team © 2020 - a modern day time capsule | Makestation Ajax Chat Hosting