PHP: The language

This entry is part 1 of 54 in the series PHP Tutorial

PHP is a popular general-purpose scripting language that is especially suited to web development.

Fast, flexible and pragmatic, PHP powers everything from your blog, as in this wordpress blog, to the most popular websites in the world.

When it comes to developing for the web, you could say PHP provides everything for us to build a complete web site.  And it does certain things really well such as in accessing the backend database.

If you have gone through the Javascript tutorial, you would find that Javascript gives a better separation between HTML and script whereas for PHP, the HTML is embedded in the script making it more difficult to learn.

If you are at this point and wanted to learn PHP, I would suggest that you also brush up on database and have some basic understanding of database tables, querying using select statements, insert and deleting records in the database table.

I will create a MySQL tutorial at a later time.

PHP: Our first PHP script

This entry is part 2 of 54 in the series PHP Tutorial

We will first look at a few PHP examples.  Here I assume that you have some understanding of HTML which is sufficed for us for now.

<html>
 <head>
  <title>PHP Test</title>
 </head>
 <body>
 <?php echo '<p>Hello World</p>'; ?> 
 </body>
</html>
Try The Code

Echo is similar to writeln in C.

In this example, the entire string <p>Hello World</p> will be output to the browser and this string is also what the browser will see.

And as for us, we will see Hello World in the browser when the script is ran.

PHP: Using Netbeans

This entry is part 3 of 54 in the series PHP Tutorial

I just found out that it is relatively easy to enable PHP development in Netbeans.

We will first have to download and install the WAMP server that includes Apache, PHP Engine and MySQL.

Try to install WAMP to C:\wamp if possible.

Download Netbeans if you do not already have it. Always download the All bundle version.

1.  Start Netbeans

2.  Go to Tools->Plugins.  We need to download the PHP engine plugin for Netbeans.

3.  In the Available Plugins, look for PHP and activate it.

php

4. Now we want to the Netbeans to use the PHP engine that you have downloaded together with WAMP.

5. Go to Tools -> Options.

php2

6.  In the PHP 5 Interpreter, browse to the PHP engine.  For example, the path in my notebook is C:\wamp\bin\php\php5.5.12\php.exe.

7.  Now start the WAMP server so that the Apache is started.  Ensure that you get a green icon in the task bar on the bottom right corner.

8. Now we will start a new PHP project.

9.  From Netbeans, go to File -> New Project.

php3

10.  Select PHP and PHP Application

php4

 

11.  In the Project Name, give it a new project name.  In the screenshot, I am using NewPHPProject.

12.  For the Source Folder, we put it under the www folder in WAMP.

13.  So it will be C:\Wamp\www\NewPHPProject.

14.  Click on Next.

php5

 

15.  Since we have the Apache server in WAMP, we use the default setup with Local Web Site.

16. Click Next and then Finish.

17.  A PHP index.php file will be found in C:\wamp\www\newphpproject\

php6

18.  Put an echo line in the code.

<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
        <?php
        // put your code here
        echo '<p>Hello World</p>';
        ?>
    </body>
</html>

19.  To run the code, just click on the green arrow button in the menu.

20.  We will have the following output in the browser.

php7

 

Posted in

PHP: Variables

This entry is part 4 of 54 in the series PHP Tutorial

Sit back and relax.

Let’s not hurry and move to the more complex example.  Sometimes, it is good to just run code written by others.

We now look at how variables are declared in PHP.

<?php

$myText = "Happy New Year! "; 
$myNum = 2014;

echo $myText;
 
echo "The year is ". $myNum;
?>
Try The Code

In PHP, a variable is preceded by a $ sign and declaration is actually not needed.

As in the example above, if you have $myNum = 2014, the PHP parser will interprete $myNum as an integer directly.

The $myText is a string variable as the text Happy New Year! is enclosed in a double quote.

Notice in line 8, there is a .(dot) in the line.  The . in PHP is a string concatenation. It joins a string and an integer or a string to a string together.

PHP: Types of variables

This entry is part 5 of 54 in the series PHP Tutorial

Just like any other programming languages, we can have different types of variables.

<?php
$var1 = 0;                          // Integer
$var2 = 3.14;                       // Float
$var3 = 'I am a string';             // String
$var4 = true;                       // Boolean
$arrayVar = array(250, 300, 325, 475);  // Array
  • Integer – Whole numbers. These can be either positive or negative.
  • Float – Numbers with decimal places. These can also be either positive or negative.
  • String – A mixture of letters, numbers, and symbols. Strings are surrounded by quotes.
  • Boolean – One of two values: true or false. The value must not have any quotes as this would turn it into a string.
  • Array – A multi-level storage type, similar to a table.