PHP: An Introduction

Have you had an idea for an application? Perhaps you have an idea for a web-app. Maybe you have a niche problem you'd like to solve, or maybe you just want to learn more about programming and software development. PHP is very capable and it's easy to get started. This is the first in a series of posts teaching PHP. I'll also have posts on SQL so you can work with information in databases and Git so you can use version control for your code. So, why PHP?

PHP is a versatile programming language and it's visually similar to the HTML or CSS you may already know. However, it can pull data in from the browser, it can build content with that data and pass it back to the browser, it can send/receive cookies, and it can read from / write to a database. PHP is a recursive acronym, standing for PHP: Hypertext Processor. What separates it from similar programming languages, such as JavaScript, is that PHP works on the server that the browser is talking to as opposed to working in the browser on the client computer.

Similar to HTML, all of your PHP code will exist between tags (known as delimiters), which look like

<?php
     echo "My first line of PHP"; 
?>

PHP lines also end in a semicolon (;) at the end of each statement (usually a line of PHP code). PHP can be written into normal HTML pages. However, the file must be saved as a .php file so that the web server knows that the page contains PHP code. Any HTML code will not be affected.

With PHP, we can echo text, just like I did above. As long as it's within double-quotes and ends with a semicolon, it'll work fine. The text I have above ("My first line of PHP") is referred to as a string.

PHP can also do math. For example, if I were to type

<?php
     echo 17*123;
?>

The result on the page would come out to 2091

PHP can store variables as well. We start by declaring a variable and then we call that variable, when it's echoing it or using it in another part of our PHP code. For example,

<?php
     $myName = "Andrew";
     echo $myName;
?>

This would echo the string "Andrew" on the page. Variables are case-sensitive. $myName, $MyName, $MYNAME and $myname are all different variables.

Like most languages of code, you can comment within the code as well. A comment will simply be a line that won't be processed by the PHP processor. Comments will usually look like

<?php
     echo "My first line of PHP"; 
     //This is a comment.
?>

I encourage people who code to comment their code accordingly. My general rules for commenting are the same for how detailed my documentation is, which is that I write for 1) the least knowledgeable member of the team and 2) for myself a year or more later.

This has been a brief introduction to PHP and it's syntax. I'll be writing more about PHP so stay tuned. 

Comments

Popular posts from this blog

Installing CentOS 7 on a Raspberry Pi 3

Modifying the Zebra F-701 & F-402 pens

How to fix DPM Auto-Protection failures of SQL servers