What's new

Web PHP with MySQL - Getting Started

kent55

kent55

Enthusiast
Messages
7
Reaction score
13
php-med-trans.png

PHP or Hypertext Preprocessor is a multi-purpose scripting language that is used on websites all over the web. Common websites include Facebook, TheTechGame, GameTuts and Se7enSins. Before we can begin, as PHP is a server side language, we need a web server to run the scripts. There are a few choices you have when it comes to this, they are:

  • Local Development Environment
  • Remote Development
I will be using the first option, local development. If you wish to work remotely, i.e. off of a live web server then that's fine. Otherwise, if you've decided to work locally like me, then you have three options, these are listed below:
  • XAMPP (Windows, Linux, Mac)
  • WAMP (Windows)
  • MAMP (Mac, Windows (In Beta))
To minimise the length of each post, I will not show how to install either of these however here are three great installation videos for each.
  • XAMPP -
  • WAMP -
  • MAMP -
For this I will be using XAMPP, however if in future tutorials I'm on my mac, I may be using MAMP, however they all generally function in the same way.

So, you should now have your development environment setup if your running locally or if you're working remotely, ensure you have an area created for this course. The next step before we can begin writing code is the text editor. Now, there are many options here, a lot of you may want to go with Notepad++, that is absolutely fine. However I will be using Zend Studio for this course, note that it isn't free. If you wish to go with an IDE (Integrated Development Environment) over a text editor, then here are some great free choices:
There are others, however a simple Google search will find you many different options.

Hopefully by now you should have your development environment set up and you're ready to start programming. Well, we're almost there, one last thing we need to do is create a folder to store all our work in. Let's do this now. So, open your text editor or IDE and create a new folder titled "php-basics" in the htdocs or public_html directory of your XAMPP, WAMP or MAMP installation. I'll demonstrate with Zend Studio.

First, I need to create a new local PHP project:
N12


For this course I'll be using PHP 5.5, however you can use PHP 5.6. As for PHP 5.4 the only problem you may have is when we build the forum software due to the password_hash function. You can however use the password_compatibility library for this, I will cover this when we get there in later tutorials.

DZm

As you can see next to where it says "Location" it is currently pointing to my workspaces folder. As I haven't added a PHP server to Zend Studio, I need to change this to one in our XAMPP, WAMP or MAMP installation. You may have already done this, but I'm going to create this folder when changing the location from within Zend Studio.

vm2


I'm going to create the new folder titled "php-basics" now.

U3f


Click "OK" and now we have our location successfully set! :smile:

5OR


So, I'll click finish, and as you can see we now have our folder in the left hand panel.

Pcy


Now, we can begin writing code, it's about time anyway :tongue:

So, first things first, let's create a new PHP file called "hello_world.php". The extension .php tells Apache, or the web server that you are running that the file contains PHP code and is to be interpreted. While I'm on the subject, this is what separates a programming language from a scripting language. A programming language such as Java needs to be compiled, whereas a scripting language such as PHP is interpreted at run-time by the web server.

7KW


So, you should now have this file, if you haven't already, open it. So, we now have a blank file:

ilA


Before we write any PHP, let's turn on our web server. I'm using XAMPP. At this stage we only need Apache, so do not bother starting MySQL.

Soy


Now, to the code. We open our PHP tags with
PHP:
<?php
and we close them with
PHP:
?>
I am going to say this now, unless you are closing the PHP tags due to HTML you should always omit the closing tag. For example, let's right a hello world statement. Open the PHP tags (do not close them) and write
PHP:
echo 'Hello World';
I have done so below:

46B

We end a statement in PHP with a semi-colon, as you can see. Now, let's load this up in our web browser. Navigate to the file. So my path is: localhost/php-basics/php-basics/hello_world.php. The reason mine does it twice is because I created a folder when I was creating a new PHP project. This is due to the fact Zend Studio creates the folder itself :tongue: Silly me. Anyway, yours should be similar, minus the second "php-basics" folder.

If you browse to this file in your browser you should see the following:

ovY

If by any chance you see an error, you may want to check your syntax against mine above. Now, we just outputted a string, and we did so using single quotes. You may be wondering, well can we use double quotes? The answer is yes, but double quotes parse variables which means it will analyse the string for PHP entities, and if found will convert them into their stored value. I will come onto variables and demonstrate this usage in the next tutorial, however if you'd like to try with double quotes, go ahead, however I recommend you stick with single quotes unless you wish to use PHP entities, such as variables in your string.

Now, we can also output integers, we can do this without the quotes, i.e.
PHP:
echo 7;

and in our browser:

WMA

Well, that doesn't look right??? Ah, well PHP doesn't read white space, so there's nothing telling PHP to separate these two onto separate lines. If we want to do that, we can add a break tag, i.e.
PHP:
echo 'Hello World<br />';
echo 7;

Now, reload your browser, and...

3Kd

Hot potato pie, it works :tongue:

Play around with this, and in the next tutorial we'll cover: concatenation, variables and arrays.

Please let me know what you thought, anything that can be improved etc... :smile:
 
Last edited:
TheInvisibleMan

TheInvisibleMan

Enthusiast
Messages
4
Reaction score
1
Wow... great thread thank you :tongue:
 
Visual Studio

Visual Studio

The Original Shiba Inu
Odysseus' Summit Nevar gon' happen in your lifetime Programmer
Messages
2,756
Reaction score
1,486
php-med-trans.png

PHP or Hypertext Preprocessor is a multi-purpose scripting language that is used on websites all over the web. Common websites include Facebook, TheTechGame, GameTuts and Se7enSins. Before we can begin, as PHP is a server side language, we need a web server to run the scripts. There are a few choices you have when it comes to this, they are:

  • Local Development Environment
  • Remote Development
I will be using the first option, local development. If you wish to work remotely, i.e. off of a live web server then that's fine. Otherwise, if you've decided to work locally like me, then you have three options, these are listed below:
  • XAMPP (Windows, Linux, Mac)
  • WAMP (Windows)
  • MAMP (Mac, Windows (In Beta))
To minimise the length of each post, I will not show how to install either of these however here are three great installation videos for each.
  • XAMPP -
  • WAMP -
  • MAMP -
For this I will be using XAMPP, however if in future tutorials I'm on my mac, I may be using MAMP, however they all generally function in the same way.

So, you should now have your development environment setup if your running locally or if you're working remotely, ensure you have an area created for this course. The next step before we can begin writing code is the text editor. Now, there are many options here, a lot of you may want to go with Notepad++, that is absolutely fine. However I will be using Zend Studio for this course, note that it isn't free. If you wish to go with an IDE (Integrated Development Environment) over a text editor, then here are some great free choices:
There are others, however a simple Google search will find you many different options.

Hopefully by now you should have your development environment set up and you're ready to start programming. Well, we're almost there, one last thing we need to do is create a folder to store all our work in. Let's do this now. So, open your text editor or IDE and create a new folder titled "php-basics" in the htdocs or public_html directory of your XAMPP, WAMP or MAMP installation. I'll demonstrate with Zend Studio.

First, I need to create a new local PHP project:
N12


For this course I'll be using PHP 5.5, however you can use PHP 5.6. As for PHP 5.4 the only problem you may have is when we build the forum software due to the password_hash function. You can however use the password_compatibility library for this, I will cover this when we get there in later tutorials.

DZm

As you can see next to where it says "Location" it is currently pointing to my workspaces folder. As I haven't added a PHP server to Zend Studio, I need to change this to one in our XAMPP, WAMP or MAMP installation. You may have already done this, but I'm going to create this folder when changing the location from within Zend Studio.

vm2


I'm going to create the new folder titled "php-basics" now.

U3f


Click "OK" and now we have our location successfully set! :smile:

5OR


So, I'll click finish, and as you can see we now have our folder in the left hand panel.

Pcy


Now, we can begin writing code, it's about time anyway :tongue:

So, first things first, let's create a new PHP file called "hello_world.php". The extension .php tells Apache, or the web server that you are running that the file contains PHP code and is to be interpreted. While I'm on the subject, this is what separates a programming language from a scripting language. A programming language such as Java needs to be compiled, whereas a scripting language such as PHP is interpreted at run-time by the web server.

7KW


So, you should now have this file, if you haven't already, open it. So, we now have a blank file:

ilA


Before we write any PHP, let's turn on our web server. I'm using XAMPP. At this stage we only need Apache, so do not bother starting MySQL.

Soy


Now, to the code. We open our PHP tags with
PHP:
<?php
and we close them with
PHP:
?>
I am going to say this now, unless you are closing the PHP tags due to HTML you should always omit the closing tag. For example, let's right a hello world statement. Open the PHP tags (do not close them) and write
PHP:
echo 'Hello World';
I have done so below:

46B

We end a statement in PHP with a semi-colon, as you can see. Now, let's load this up in our web browser. Navigate to the file. So my path is: localhost/php-basics/php-basics/hello_world.php. The reason mine does it twice is because I created a folder when I was creating a new PHP project. This is due to the fact Zend Studio creates the folder itself :tongue: Silly me. Anyway, yours should be similar, minus the second "php-basics" folder.

If you browse to this file in your browser you should see the following:

ovY

If by any chance you see an error, you may want to check your syntax against mine above. Now, we just outputted a string, and we did so using single quotes. You may be wondering, well can we use double quotes? The answer is yes, but double quotes parse variables which means it will analyse the string for PHP entities, and if found will convert them into their stored value. I will come onto variables and demonstrate this usage in the next tutorial, however if you'd like to try with double quotes, go ahead, however I recommend you stick with single quotes unless you wish to use PHP entities, such as variables in your string.

Now, we can also output integers, we can do this without the quotes, i.e.
PHP:
echo 7;

and in our browser:

WMA

Well, that doesn't look right??? Ah, well PHP doesn't read white space, so there's nothing telling PHP to separate these two onto separate lines. If we want to do that, we can add a break tag, i.e.
PHP:
echo 'Hello World<br />';
echo 7;

Now, reload your browser, and...

3Kd

Hot potato pie, it works :tongue:

Play around with this, and in the next tutorial we'll cover: concatenation, variables and arrays.

Please let me know what you thought, anything that can be improved etc... :smile:
PHPStorm by JetBrains is also a great (if not the best) IDE for PHP development.
 
kent55

kent55

Enthusiast
Messages
7
Reaction score
13
PHPStorm by JetBrains is also a great (if not the best) IDE for PHP development.
It is very good, I'm surprised I forgot to mention that. As far as professional development goes, i.e. comprehensive PHP and JavaScript debugging, team collaboration, and remote server support, I'd personally say Zend Studio is the best for PHP, but it lacks on decent styling. PhpStorm is still a great IDE, and if you have a student email, you can get it free :smile:

P.S. I added phpStorm :smile:
 
Top Bottom