PHP Variables and Expressions for Complete Beginners
Written by Ian Elliot   
Thursday, 09 January 2020
Article Index
PHP Variables and Expressions for Complete Beginners
Expressions

Variables and expressions are the core of programming in general and no less so in PHP. If you have never encountered the ideas here is a gentle introduction.

 

Introduction to PHP

phpcover

Contents

  1. Getting started
    Getting Started With NetBeans PHP - Local Projects
  2. What PHP Does
  3. PHP Variables and Expressions for Complete Beginners
  4. PHP Control Structures 1 - if and else
  5. PHP Control Structures 2 - switch and elseif
  6. PHP loops
  7. Advanced loops
  8. Functions
  9. Ten minutes to PHP objects
  10. PHP Inner Functions And Closure
  11. NetBeans Remote Projects and debugging

A key idea in programming is the "variable".

A variable is just a named area of storage or somewhere you can store a value.

In PHP a variable is indicated by a leading $ sign before its name.

For example, $a is a variable and you can store a value in a variable using the equals sign.

For example:

$a=1;

The temptation is to read this as

"dollar a equals one"

however you will find it much more helpful to understand what is going on if you always read this sort of instruction as

"store one in $a".

You can also think of the variable $a as a name for a sort of bucket that you can put something into for later use.

The whole expression is called an assignment statement - because you assign a value to $a.

Notice also that a variable can only store one value at a time and storing a new value erases any trace of what was already stored.

For example:

$a=2;

stores 2 in $a and overwrites anything that was already stored.

This raises the question of how you get something out of a variable?

The answer is you simply use its name where you would have used the value. For example, you can write:

echo $a;

which doesn't send the text "$a" to the output but the value stored in $a, i.e. the number 1.

If you have used other programming languages you might know that they often use specific types of variable to store specific types of data. PHP doesn't work this way and it allows you to store almost any type of data you care to store in a variable with minimum fuss. This is usually good but it can occasionally cause problems.

PHP is said to be weakly typed or untyped. 

Similarly PHP doesn't demand that you make a list or declare all of the variables you are using at the start of your program – simply using a variable is enough for PHP to create it and keep track of it.

In the main PHP tries to be as easy to use as possible.

Working with variables

There are some rules about what you can call a variable but most things will work. There are also some special and predefined variable names that you can't use. You can have any number letters or numbers but you can't include any space. You can use the underscore character to make it look as if there is a space as in:

$My_Variable

You can also use upper and lower case characters to emphasise the meaning but notice that variable names are case sensitive. That is:

$name

and

$Name

are two different variables.

This is something that often causes errors so be very careful how you mix upper and lowercase letters.

If you are using NetBeans or Eclipse as your PHP development system then you can make use of automatic prompting which shows you a list of possible variable names as you type – you can select one of the possibilities as an easy way to enter a variable name and a way of minimising the likelihood of entering a variable name incorrectly.

 

variables

Selecting variables from a list is far less error prone than typing them in.

 

In Eclipse names of all of the variables that you have used are listed in the Outline window. If you click on one of the variable names then every use of that variable name is highlighted throughout the program.

 

outline

The Outline window helps you keep track of your variables and find them in your PHP code.


NetBeans provides the facility where you can right click on any variable and select Navigate to find where the variable is first used, highlight all occurences and so on. 

Data types

PHP lets you store almost any type of data in a variable. At the moment you might not have noticed that there are different types of data – but there are.

For example, "abcd" is a string of characters and 3.14 is a number.

The difference is made very clear when you consider the fact that it makes sense to do arithmetic with 3.14 but not with "abcd".

In fact this is a good definition in that data types differ in the operations you can perform on them.

PHP recognises four basic types of data:

  • Boolean – which is just true or false

  • integer – a whole number like 1 or 2

  • float - a number with a decimal point like 2.1

  • string – a collection of characters.

For example:

  $a=true;

stores the Boolean value true in $a

  $a=1;

stores the integer 1 in $a

  $a=2.1;

stores the float 2.1 in $a and

  $a= "Hello";

stores the string Hello in $a.

Strings are incredibly useful and there is a great deal to say about them and they deserve an article devoted to them.

Sometimes the differences between these types might seem arbitrary.

For example is 2.0 a float or an integer?

After all it's a whole number but it has a decimal point.

The answer is that it's a float because it has a decimal point. More subtle is the difference between "2.1" and 2.1 – the first is a string of characters (that just happen to be digits and hence look like a number) and the second is a float, i.e a number with a decimal point.

 

Banner

 

In most cases you can simply ignore the differences between different types of data because PHP will attempt to automatically convert between types as necessary – there is more to say on this topic but after the introduction of expressions.

Also notice, especially if you are familiar with other programming languages that in PHP variables do not have a data type - you can store anything in any variable. However at any given time a variable will have something stored in it that does have a data type and what operations are applicable to that variable will change according to what is stored in it. 

 

<ASIN:1491918667>

<ASIN:1491905018>

<ASIN:0672329166>

<ASIN:0321784073>

<ASIN:1449392776>

<ASIN:144936375X>



Last Updated ( Thursday, 09 January 2020 )