Use Variables Like a Pro!

Lucas F. Lu
5 min readFeb 25, 2020

--

In the previous article, we talked about why, when and how to use a variable with examples. In this session, we will dive into more details of using a variable and learn some tricks with practical examples.

A guide for beginners of any background to start writing codes

Variable initialization

In our previous example, we declared a variable called “names” and assigned a value “Tom” to it. This process is called value assignment. It takes two lines of code to do the declaration and the assignment.

But it turns out, we can do all these (declare a variable and assign a value to it) in just one line of code. Simply do the following:

<?php    $names='Tom';?>

Since this variable is declared and assigned a value at the same time, it must be the first time it was given a value to. We call the event of assigning a value to a variable for the first time “variable initialization”.

A more dynamic approach

What if we want to make our program a little more fancy, like asking the person who uses the program for his/her name then having the program greet the person?

If we want the computer to perform the action of asking for input, we then need to know the command for such action. Similar to the logic of adopting command “echo” when we want to display something. Every language has their own commands for this task, and in PHP, that command is called “readline”.

Therefore, our program is now written as follows:

<?php    $names=readline('What is your name: ');    echo 'Good morning master '.$names;?>

Code Explanation

Readline is a PHP command to read user inputs from the command line (that windows DOS thing again).

The data between the brackets “( )” after the readline command is a prompt message for the user to look at. Sort of like an instruction for humans to read and follow, knowing what the system is expecting them to put in.

Whatever a user puts in will then be assigned as a value to the variable named “names”.

Since this is the first time variable “names” is assigned a value to, it is the initialization of variable “names”.

We then use the value of variable “names” as a part of the display command.

This is where our knowledge of the direction of value assignment finally pays off. In our previous article, we briefly touched on the subject that, the assignment of a value to a variable were performed from the right to the left of an equal sign. Value is made available first on the right side of the equal sign (=) then assigned to the left side of it, which being the variable.

The flow of the process in our case today is essentially the same, from the right to the left, and it makes perfect logical sense.

The system performs the command readline first, takes its value and makes it available for assignment. The system then assigns that value collected from command readline and gives it to the variable called “names” on the left side of the equal sign (“).

Now when we run the program, it will ask the users to put in their names and display the greeting message with their name as a part of the message. Cool stuff!

Simplicity is beauty

When people write programs, they tend to reduce the number of lines written to a minimum. In our previous examples, we would write the variable declaration, initialization and the prompt for user inputs all in one line, instead of three separate lines.

What’s crazier in our example is that we can even further reduce the program into just one line of code. Because we now understand that programs run from top to bottom and from the right to the left. And there we have:

<?php    echo ‘Good morning master ’.readline(“What is your name: ”);?>

Code Explanation

When the computer reads this line of code,

It will first, execute the readline command and ask for an input.

Then, it will make this value entered by the user available for assignment.

After that, the computer will combine “Good morning master” with the value made available with the glue sign (in our case, the period sign ”.”).

At the very end, echo command was executed with the combined result as data and display the message to the user.

We eliminated the use of variables because, even if the data is changing all the time, they are no longer needed anywhere in the future except the use of echo command. So why not just go ahead and display whatever the user enters as part of the greeting message. There is no longer a need to store the data.

Here is an example of when we do need to have an variable:

<?php    $names=readline(“What is your name: ”);    echo ‘Good morning master ’.$names;    echo ‘How are you doing this morning master ‘.$names;?>

Variable is needed again because the value is used on multiple occasions.

We have the system display two messages after asking the user for his/her name. Therefore, a place is needed to store that value and can be retrieved multiple times.

Data Types

I’d like to take this opportunity and introduce a concept called data types.

We store data into variables, like the name of a person. But that’s not the limit. We can also store age, phone numbers, or anything you can imagine. They seem to be stored in the same variable but they are treated differently by the computers. Because from a computer’s point of view, they all have different data types.

A data type is the characteristic of data stored in a variable. Like the message displayed in our example is a “string” data type. Number “10” is “integer” type, “10.20” is “float” type.

We don’t need to remember these types for now as I was only trying to introduce the concept of data type here. We will take a closer look at these types in the near future. For now, let’s get in the habit of calling something a string if it looks like a sentence, an integer if anything that represent whole numbers.

Summary

  • Variable initialization is assigning a value to a variable for the first time.
  • Program runs from top to bottom and from right to left.
  • readline is a command in PHP to read from a user’s input.
  • We can chain multiple commands into one line for simplicity.
  • Variable is not needed if we don’t need to store the value for future use.
  • And of course, there’s this thing called data types. Author says more on that later.

Terms and keywords

I think it’s time for us to start choosing words professionally. Interchanging commands and instructions, data and information is starting to get confusing as you are becoming more knowledgeable on the subject of programming. Let’s define some keywords and terms.

  • Statement: a line of instructions or commands like echo or readline.
  • Value: data or information assigned to a variable.
  • Data type: the type of value a variable stores.
  • String: things like “how are you”, “good morning”.
  • Integer: things like 1,2,100,etc.
  • Assignment: statements like $name=”Tome”, $age=12.

That’s a lot of materials packed into this article. Let’s take a long break, and when we come back, we will take a look at something different and equally fun.

New articles are added every Wednesday. Please stay tuned.

--

--

Lucas F. Lu

Laravel Expert, AI enthusiasts. Recently fell in love with helping people rebuild their lives by learning how to program.