
Variables
Programming in any language is very limited without something called “variables”. Variables allow one to store data for later use, either to reference directly or to manipulate the data. You define a variable by using the dollar sign “$” and then the variable name. There are some limitations imposed on PHP variable names. Variable names cannot start with a number (0-9), or an underscore (_), and cannot contain a non-ASCII character anywhere.
After you name your variable, it’s always a good idea to initialize your variable. All this means is that at the time you make the variable you give it a value. This can help with the security of your code, as well as prevent logic errors from uninitialized variables.
First, you may see some variables that you’re not familiar with, which is fine for now, those will be covered later in this article. Secondly, the variable names are done using what’s called “Camel Case” (or camelCase), which capitalizes the first letter in each word of a variable name. This is not required, nor is giving your variable a name that describes its use, but both help the legibility of your code considerably.
Basic Variable Types
As hinted at above, in Variables, there are different types of variables. These are:
Int (short for integers), which just means whole numbers. Examples of this are 1 10 22 5832… and so on.
Float (short for floating point number), which just means decimals. Examples of this type are 1.99, 2.0013 … and so on.
String, which is a string of characters, examples of this are, “string”, “this is a string”… and so on.
Array, an array is a grouping of variables accessed by a single variable name. For example, $myArray[0], $myArray[‘test’] … and so on.
Boolean, (short for boolean logic) is just a fancy way of saying it’s a binary value (1 or 0, on or off, true or false).
- – Int
The possible values for int get a little complicated, it varies based on the bit level of your processor and operating system. However, it is safe to assume that the range (based on a 32-bit system) is -2147483648 to 2147483647 when using signed numbers (negative is possible). However, unsigned ints are always double the positive range of their signed relatives, so unsigned the range would be 4294967294.
- – Float
Floating point numbers in PHP go out to a very large amount of decimal places. However, care must still be taken to avoid what’s known as “round error”. If you are working with a number that exceeds the PHP floating point range, the number will be rounded off to the largest decimal place. If you do this too much, round-off errors can accumulate, thereby affecting your final calculation. Furthermore, be careful when attempting to compare two floats exactly, as one number may be 9.1111112 and the other might be 9.1111113, for your purpose the numbers might be close enough, but for PHP they’re not equivalent.
- – String
Strings are a bunch of characters strung together, usually to form a word or some other meaningful grouping. Strings can be defined via double or single quotes (“or ” respectively). Make sure to define strings using quotes, otherwise, your code will error out (or act unpredictably). You can also place variables directly in strings and have them be processed, though only with strings defined using double quotes.
However, you should try and do string appending instead, as that allows more manipulation of the variable. Along with using variables in strings, double quotes are required for special escape characters to be interpreted as well. Escape characters will be covered in more detail later.
- – Array
As stated earlier, an array is a group of variables referenced by a single variable. You can then reference your different variable values using a unique index that is associated with that value.
- – Boolean
Boolean variables are used for logic and code flow. Boolean values can mean 0/1, true/false, on/off…etc.
Basic Operators
What use is a programming language if you cannot manipulate data? If you couldn’t manipulate the data in any way, the best you could do is store values and print them out, not too useful, is it? Well, that is where operators come into play. An operator allows you to manipulate one or more variables using one or more variables, and/or compare variables.
First, we will cover arithmetic operators. These are the ones you would use to do mathematical operators on numeric/float variables. There are only a few, but you can achieve very complex calculations from these basic arithmetic operators.

Ryan Goose, a seasoned PHP developer and tech enthusiast, brings a wealth of knowledge in web technologies. With a passion for coding and a knack for simplifying complex concepts, Ryan’s articles are a treasure trove for both budding and experienced PHP developers.

