Learning PHP: Part 1: Variables

Learning PHP

PHP: Hypertext Preprocessor (PHP) is a server-side scripting language frequently used as part of web development and as a command-line tool for common tasks.


Variables

A common metaphor for understanding variables in programming languages is a bucket. When values are assigned, something is put in the bucket. When a variable is used, whatever is in the bucket is examined.

Screenshot 2018-10-29 14.22.21

In PHP variables start with a dollar sign. Immediately after the dollar sign is the name of the variable. Like many other programming languages, variable names can only contain letters, numbers, and the underscore. Special characters cannot be used.

Loosely Typed

PHP is a loosely typed programming language. This means that, unlike some other languages in which variables have data types, they do not in PHP. A variable, back to the metaphor of a bucket, is a generic one. Any type of value can be in variable at nearly any time.

Freely Created

Variables can also be freely created. There are very few set rules on where and how variables can be created on PHP. Simply using a dollar sign and a new name will create that variable for use. Whenever they are needed, variables can be created freely.

Command-Line Tool

While PHP can be used as a server-side language, testing using a Command-Line Interface (CLI) is often recommend for development and learning purposes. To see the values in variables, the functions echo and print can be used to show output on the command-line.

Play with the example on Repl.it!