JoshDiaw01 Posted August 3, 2016 Share Posted August 3, 2016 Hello, I am new to PHP programming and recently I've been looking over random generated programs. I ran across this code and I would like to understand it better because the person that wrote it did not write comments or use descriptive names for the variables. Thank you <?php function randColor() { $r = rand(0,255); $g = rand(0,255); $b = rand(0,255); return sprintf('%02X%02X%02X', $r, $g, $b); } $tdata=''; for ($i=0; $i<10; $i++) { $tdata .= "<tr>"; for ($j=0; $j<20; $j++) { $colors = randColor(); $tdata .= "<td style='background-color:#$colors'>$colors<br><span class='wtext'>$colors</span></td>"; } $tdata .= '</tr>'; } ?> <html> <head> <style type='text/css'> table { border-collapse: collapse; } td { width: 60px; height: 60px; font-family: sans-serif; font-size: 8pt; text-align: center; } .wtext { color: white; } </style> </head> <body> <table border='1'> <?=$tdata?> </table> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/301743-help-understanding-code/ Share on other sites More sharing options...
Barand Posted August 3, 2016 Share Posted August 3, 2016 HTML colours have three colour components, namely red, green and blue. Each of these has a value ranging from 0 to 255. These are expressed as hexadecimal values 00 to FF. #FF0000 = red #00FF00 = green #0000FF = blue equal quantities of each component gives a grey color, for example #888888 gives a mid-grey. In the function, the variables $r, $g and $b are the red, green and blue values. Those look fairly meaningful to me, better than $peter, $paul and $mary. Quote Link to comment https://forums.phpfreaks.com/topic/301743-help-understanding-code/#findComment-1535530 Share on other sites More sharing options...
JoshDiaw01 Posted August 3, 2016 Author Share Posted August 3, 2016 (edited) Thank you. It all makes sense now since I was confused as to what the single characters meant. Does that mean that "T"data is for the table? And in the for loop we have temporary variables that could be named anything, right? Edited August 3, 2016 by JoshDiaw01 Quote Link to comment https://forums.phpfreaks.com/topic/301743-help-understanding-code/#findComment-1535535 Share on other sites More sharing options...
Psycho Posted August 3, 2016 Share Posted August 3, 2016 Some notes: The function randColor() is used to create a random color. As Barand explained, when defining colors in an HTML page, they are composed of RGB (Red Green Blue) values, with each component having a value from 0 to 255. FYI: The same RGB process is used in many different contexts to define color. The function defines three variables for those three components with a random value from 0 to 255 using the rand() function. The function then takes those three values and composes them into a string format which can be used in the HTML output. A variable ($tdata) is defined to store the output for the table (i.e. 'tdata' is the table data). There are two loops that use a for() loop. The for loops 1) set an arbitrary variable ($i or $j) to 0, 2) Create a condition to continue the loop as long as the variable is less than a certain value, and 3) increment the variable on each iteration of the loop. for ($i=0; $i<10; $i++) { The first iteration of this loop will set $i equal to 0. The loop will continue as long as the condition of $i being less than 10 is true. $i will be incremented by 1 on each iteration of the loop. Therefore the loop will run 10 times. The outer loop is for the rows of the table and the inner loop is for the columns in the rows (there will be 10 rows). The outer loop adds an opening row tag to the variable to store the output. The inner loop creates the individual cells for the row (there will be 20 cells/columns in each row). First, it calls the function described above to get a new random color for each cell. That color is used in creating the content for the cell that is also stored in the output variable. After each completion of the inner loop a closing row tag is added to the output at the end of the outer loop. Finally, after all the PHP processing is complete, the variable $tdata holds the entirety of the output that was produced. After the PHP code comes the actual HTML output that will be sent to the user. The majority of this is simply a hard coded HTML page. But, the variable with the dynamic content created above is then inserted into the HTML table element. The result of all of this is that on every call to this page an HTML table will be displayed. It will have 10 rows and 20 columns. Each cell in that table will display a random color. Quote Link to comment https://forums.phpfreaks.com/topic/301743-help-understanding-code/#findComment-1535538 Share on other sites More sharing options...
JoshDiaw01 Posted August 3, 2016 Author Share Posted August 3, 2016 (edited) Thank you for taking the time to go into detail. I've mostly been learning from the tutorial at Code Academy. The insight that you guys have provided me has been very helpful. Edited August 3, 2016 by JoshDiaw01 Quote Link to comment https://forums.phpfreaks.com/topic/301743-help-understanding-code/#findComment-1535547 Share on other sites More sharing options...
Psycho Posted August 3, 2016 Share Posted August 3, 2016 What I would suggest is to go through code a line at a time. For each line see what function(s) are used and/or the data. Look up the functions in the manual and understand what is happening on that line of code. Then go to the next line. Sometimes you will have to "go back" to previous code to understand the current line you are on. I think it really helps to learn logic that way (assuming the code has good logic). However, if you have someone that has a line of code that does 10 different things, it can be hard to decipher into something meaningful Quote Link to comment https://forums.phpfreaks.com/topic/301743-help-understanding-code/#findComment-1535552 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.