Analyze and explain practice using variables

Tram Ho

In this article, let’s analyze and find the solution for the practice using variables. You use as always.

Fun practice 0

This one is pretty simple:

So we will get the result we want:

However, if we want to change the value of the variable myName and turn yourFriendlyNeighborhoodName then they must also change the value of the variable message and reply.

If we had to, we wouldn’t have fully exploited the benefits of using variables. Well then, if I could use the value of the variable myName in variable message so good to know. PHP provides us with a pretty simple way, you just need to change single quotes to double quotes and use variable names where needed.

The result is still the same but our writing style has changed, with this writing we have exploited the benefits of using variables. Try it out to see the benefits. Now I will change the variable myName to 'Bizz Doe' and yourFriendlyNeighborhoodName to 'Ai Doe' .

Looking at the code we see, what we need to do is simply change the value of the variable myName and turn yourFriendlyNeighborhoodName. And so the message and reply will automatically change accordingly. With this code, we don’t need to find and change in many places when the variable value changes. It will also help us make fewer mistakes in the process of change. Here is the entire code so far and the result:

Fun practice 1

This one is pretty simple, isn’t it?

Surely you will find that we need to change in many places when we want to change words PHP to Javascript and John Doe to Jane Doe. And the solution is we will use variable.

When changing, we just change the value of the variable:

Here is the code and results I did on replit:


So today we have analyzed, discussed and found answers to two exercises on variables. Thus, the flow of code is usually to solve the problem first, after success, then review the code and optimize the code. For example, for the two exercises above, I will approach by coding to get the desired results, then see where there is a lot of repetition but often changes, then set it to a variable, read it again and consider the name. reasonable variable. After all, we should have a piece of code that solves the problem we need and is neat.

Share the news now