PHP Lessons Part 1: Introduction to PHP and Variables

by
Guest-GS

What is php?

PHP is a powerful programming language for programmers to add interactivity to web pages, making them dynamic. What does “dynamic” mean? Simply “not static”! It may seem strange, but this is exactly what it is. Traditionally, web pages were “static”, coded in HTML. Nowadays, these pages are filled contents that change over time. Think about a blog. Aren’t new articles added from time to time? Well in the past, someone had to go edit a webpage, add code and add the article there manually. Nowadays, the article is pulled from a database, and dynamically added to a page as it is posted. That’s what PHP does: generating pages on the fly with content, with the help of HTML and other languages of course.

PHP is free (as in no cash needed, and open source – you can download and view the PHP source code) and is among the most popular programming languages used for Website Developement, to do processing.

What does PHP stands for?

PHP means: Hypertext Pre-Processor (HP, right? It’s a recursive acronym! It originally meant, Personal Home Page). It is a server side language and are executed on a server (Not on a local machine like Javascript, HTML and CSS). PHP can interact with Many databases out there such as MySql, Oracle, etc. To fully make use of PHP, a minimum knowledge of HTML is required, as well as some basic CSS if you wish to make things look nicer, but CSS is not directly required.

How do we get started with PHP?

First of all, PHP being a server-side programming language, you will need (guess?) a server! Now to cut things short, just grab a “ready-made” package that includes Apache web server, PHP processor and MySQL in built. I recommend XAMPP for its ease of use. XAMPP is easy enough to install and run. Just run the XAMPP Control Panel, and start Apache as a start. You can also try WAMP or LAMP if you are on Linux. Any server with PHP capabilities would do. Oh, a “server” can be any computer huh! No need to have some huge supercomputer in your basement to host a web server. Any recent enough computer, with network connection and Internet access (if you want to make pages public) would do.

For development, you will usually have a server running on your computer, code your web pages and see if everything is working, without having to constantly access a server on the Internet. If all is right, then you will move these pages to a “real” server on the Internet, for people to use your pages.

Now, go to where you installed your web server. On Windows it will probably be on the C: drive. In there, find a folder named “www”. Sometimes, it will be named “public_html”. Some servers have both. We use “www” here. Go in it. In there, you can create a folder. E.g. “phplearn”

Now to test your server, first activate it. In the server’s control panel, “start” the server. Then type this address in your browser: “http://localhost”. If you see a page, your web server is running! Congratulations!

PHP file should have the extension .php (eg: myfile.php) and should usually be found in that “www” folder or in some sub-folder of the “www” folder. To access your own file, say “test.php” inside “phplearn”, you would type: “http://localhost/phplearn/test.php”. Easy enough right? If you are wondering, “localhost” simply means that you are accessing your own computer.

All php scripts always starts with <?php and should end with ?>. You can use php anywhere in a document as long as the extension is respected (Eg .php). Do not forget those <?php and ?> tags. If you miss those, your page will not work. You will probably see your code in your browser if you miss those.

PHP can also starts with <? and end with ?>

Some notes to remember:

There are Shorthand Form tags for php and the standard Tags.

The shorthand tags are like below:

<?
?>

You will notice that I didn’t write php just like i did earlier. But it’s best pratice to use the Standard Method (i.e. <?php   ?>) since some servers disable the Shorthand tags by default.

Why should I use php?

1) Nowadays, PHP is available everywhere and runs on all available Platform (Windows, Linux, MAC OS etc).

2) PHP runs fast and is easy to understand according to some other programming languages.

3) Using PHP, you can secure areas on your website

4) It’s free! Why shouldn’t you use it?

In what way will PHP help me?

This one is a good question, with PHP, you can give life to HTML FORMS and let users interact with the website, send you information, record users who visit your website, interact with them, send emails, etc… There are a variety of ways in which you will benefit from PHP.


How do I start?

Maybe with the very basics of PHP: variables. The first lesson starts below:


What are Variables in PHP?

Well, think of a Variable as a container which stores all the values you ask it to store. It can store, strings(text), numbers, arrays (There will be a large post about arrays later) etc… Unlike other languages, PHP variables have no type restriction. They can hold any kind of data. There are some important security issues regarding the use of un-initialized variables. We’ll briefly talk about it below.

All php variables start with the Dollar Sign – $

Using variable makes your scripts cleaner and easier to modify when needed and can be used anytime you want.

The way we write or declare a variable in php is like below:

<?php
$myVariable = "This is some text";
?>

What you just did, between those <?php and ?> is called an “assignment operation”. You are assigning a value to a variable. Also, note that all PHP statements MUST end with a semi-colon.

Now there is something you should know, a variable name is something that you can choose. For example, instead of using $myVariable like I did above, I could have used something like $yourVariable = “This is some text”; ! However, use meaningful names. “$userName” is easier to understand than “$a”, though “$a” is valid. Note that variable names have some restrictions, e.g. they cannot start with a number, so “$1var” is invalid. Valid names start with a letter or underscore, followed by letters, numbers or underscores.

Also, you cannot use “this” as a variable name (e.g. “$this = 20;”). It is reserved for other purposes, as you will see in a later article.

Now, the way we access the variable is very easy. We just need to echo or print it out like below:

<?php
$myVariable = "This is some text";
echo $myVariable; // Will write this is some text in the browser
?>

As you can see, when echoing out the variable, it will show whatever value you assigned to it. In this case, it will show “This is some text” without the double quotes. If I was to change the value, then I would just edit the text assigned to the variable.

“echo” simply means, “output this variable to the browser so that I can see its value”. Those two slashes indicate a comment. Whatever comes after 2 slashes are ignored, and not considered computer executable code. Use those to leave reminders for yourself regarding what a piece of code does.

Comments can also be enclosed in multi-line comments as follows:

/*
Your comment here
Another line
*/

Another example of variables using numbers:

<?php
$myVariable = 4 + 4;
echo $myVariable;
?>

The output will be 8, since 4+4 = 8 😉

This is just a basic introduction of what variables are and how they can make life easier when programming in PHP. Do try other manipulations with variables. Hopefully, now you know how to use Variables in PHP. Check back for more PHP lessons!

You can also investigate “variable-variables”, which is an interesting feature of PHP.

For further reference, the PHP Manual is THE place to check.

This article was contributed by guest blogger Tipa of Mu-Anime!

[seriesposts title=”PHP Lessons” titletag=h3 listtype=ul orderby=date]