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: 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: 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.

Javascript vs PHP, which to start learning first for beginners?

Javascript has been getting very popular in the past 2 years mainly because together with HTML5, you can create iPhone or Android apps.

Though it is known that in terms of speed, Javascript will never be able to catch up with native apps, there are still many applications that are suitable for developing with Javascript.

If you delve into these 2 languages, you would be surprised how different they are.  You can say C and Java are very similar but you can’t say PHP and Javascript are similar.

What makes PHP so popular? It’s the simple reason that PHP is server side and the code written is hidden in the server.

If you are a new web programmer, I strongly suggest that you start with Javascript first.  Javascript has a better separation between the script and HTML making it a lot more easy to code.

When you first learn about Javascript, you do not have to worry about HTML or CSS.  You can learn it as if you are writing in C.  However, when you are writing PHP, you need to put the HTML into the code making it really confusing for the beginners.