r/code • u/AndreasSkole • Nov 18 '19
Need some simple PHP coding explained.
So we (my class) is getting started with PHP coding and were given the task to write a program that would write the following text:
(Watch image)

For most of you this is going to be easy, but I have no clue where to begin. So i checked what the final answer is supposed to be:
<?php $rowNumber = 10; while( $rowNumber > 0 ) { $columnNumber = 0; while ( $columnNumber < $rowNumber ) { echo "\*"; $columnNumber++; } echo "\\n"; $rowNumber--; }?>
I just don't understand how I'm supposed to see this. Anyone got any advice for how I can practice these kinds of tasks?
0
Upvotes
3
u/TheoGau Nov 18 '19
Hi,
When you need to solve a problem using programming (whatever the language is), you need first to understand and decompose the problem, then translate it using keywords and structures available in the language.
For example, here, you need to write X lines. And for each line, you need to write a varying Y asterisks. In PHP (like in many other languages), you have a keyword (while) to iterate over a specific thing. You need to know about the language capabilities to know what to use.
Now, you can decompose the problem into 2 parts : displaying X lines and displaying asterisks in lines.
For the first part, you need to display like 10 lines, the content is not interesting for now. So you define the number of lines wanted, then use a "while" to make a loop while the condition is true. In this loop, you will print something, it's not important yet. You will also update the number to decrement it until reaching 0 and go outside the loop. Now, you have 10 lines displayed.
Then, for the second part, as you can see, 10 is also the number of asterisks of the first line. 9 is the number of asterisks for the second one. It finishes by 1 for the last line. So this number could be reuse to determine the number of asterisks for each line. So you can write another "while" directly inside the first one. You need to create a variable to count how many asterisks you have written and use it in the condition of the second while to go outside this second when the line in done. You can also invert the condition and copy the current number of lines then decrement it until 0 (like the first while). You need to copy to do not decrement the number used for the first while.
You need to understand that the first while will loop 10 times, and the second while will loop 10 times for the first line, 9 times for the second, etc... Because it's nested.
So, to resolve a such issue you need to:
understand the problem and find usable patterns (here the number of lines for the number of asterisks)
divide the problem in subtasks
work on each subtask independently using keywords and structures available in the language
then do the link between subtasks to solve the entire problem
Sometimes you can solve independently in whatever order the different tasks, sometimes you will need to solve A to be able to solve B (like in your specific case).
I hope it will help you :)