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 https://replit.com/languages/php_cli as always.
Fun practice 0
This one is pretty simple:
1 2 3 4 5 6 7 8 9 | $myName = 'John Doe'; $message = 'John Doe: Hello, my name is John Doe.'; $yourFriendlyNeighborhoodName = 'Jane Doe'; $reply = 'Jane Doe: Hello John Doe. My name is Jane Doe. Nice to meet you.'; echo $message . PHP_EOL; echo $reply . PHP_EOL; |
So we will get the result we want:
1 2 3 | John Doe: Hello, my name is John Doe. Jane Doe: Hello John Doe. My name is Jane Doe. Nice to meet you. |
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
.
1 2 3 4 5 6 7 8 9 | $myName = 'Bizz Doe'; $message = 'Bizz Doe: Hello, my name is Bizz Doe.'; $yourFriendlyNeighborhoodName = 'Ai Doe'; $reply = 'Ai Doe: Hello Bizz Doe. My name is Ai Doe. Nice to meet you.'; echo $message . PHP_EOL; echo $reply . PHP_EOL; |
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.
1 2 3 4 5 6 7 8 9 | $myName = 'John Doe'; $message = "$myName : Hello, my name is $myName."; $yourFriendlyNeighborhoodName = 'Jane Doe'; $reply = "$yourFriendlyNeighborhoodName: Hello $myName. My name is $yourFriendlyNeighborhoodName. Nice to meet you."; echo $message . PHP_EOL; echo $reply . PHP_EOL; |
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'
.
1 2 3 4 5 6 7 8 9 | $myName = 'Bizz Doe'; $message = "$myName : Hello, my name is $myName."; $yourFriendlyNeighborhoodName = 'Ai Doe'; $reply = "$yourFriendlyNeighborhoodName: Hello $myName. My name is $yourFriendlyNeighborhoodName. Nice to meet you."; echo $message . PHP_EOL; echo $reply . PHP_EOL; |
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?
1 2 3 4 | $message = "My name is John Doe. I am a PHP developer. In my free time, I enjoy learning PHP. If you have any PHP job, please contact me. Thank you. Signature: John Doe"; echo $message . PHP_EOL; |
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.
1 2 3 4 5 6 | $myName = 'John Doe'; $myProgrammingLanguage = 'PHP'; $message = "My name is $myName. I am a $myProgrammingLanguage developer. In my free time, I enjoy learning $myProgrammingLanguage. If you have any $myProgrammingLanguage job, please contact me. Thank you. Signature: $myName"; echo $message . PHP_EOL; |
When changing, we just change the value of the variable:
1 2 3 4 5 6 | $myName = 'Jane Doe'; $myProgrammingLanguage = 'Javascript'; $message = "My name is $myName. I am a $myProgrammingLanguage developer. In my free time, I enjoy learning $myProgrammingLanguage. If you have any $myProgrammingLanguage job, please contact me. Thank you. Signature: $myName"; echo $message . PHP_EOL; |
Here is the code and results I did on replit:

Conclude
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.