HoTDaWg Posted December 23, 2008 Share Posted December 23, 2008 hi im currently learing php from a book and it did not explain the purpose of a % in an if statement: <?php if(($x % $perrow) ==0) { print "the staement ..."; } //it also uses it later... if (($x % perrow) != 0) { print "another statement"; } else { print "the final statement"; ?> the script basically outputs all the images it finds in a folder and places each inside of a table. The full code is: <?php error_reporting(E_ALL); $fdir = "Uploads"; $files = array(); print <<<HTML <html> <body> HTML; ($dir = opendir($fdir)) or die ("Cannot open \"$fdir\""); $grphnum = 0; while(!($file= readdir($dir)) === FALSE) { if (strpos($file,".png") || strpos($file,".gif") || strpos($file,".jpg")) { $grphnum++; $files["$grphnum"] = $fdir."/".$file; } } closedir($dir); array_multisort($files,SORT_ASC,SORT_STRING); print "\t<table border=1>\n"; print "\t<tr height=\"60\">\n"; $perrow=5; for ($x=1; $x <= $grphnum; $x++) { print "\t\t<td><img src=".$files["$x"]." width=\"50\"></td>\n"; if (($x % $perrow) == 0) { print "\t</tr>\n\t<tr height=\"60\">\n"; } } if (($x % $perrow) != 0) { print "\t\t<td> </td>\n\t</tr>\n"; }else{ print "\t</tr>\n"; } print "\t</table>\n"; print <<<html </body> </html> html; ?> so my quesiton what does the % do and why is it being used? any help would be greatly appreciated thanks Link to comment https://forums.phpfreaks.com/topic/138220-solved-what-does-the-character-do/ Share on other sites More sharing options...
Maq Posted December 23, 2008 Share Posted December 23, 2008 The % = modulus. Basically it's the remainder of the 2 values. Please read this. Link to comment https://forums.phpfreaks.com/topic/138220-solved-what-does-the-character-do/#findComment-722605 Share on other sites More sharing options...
HoTDaWg Posted December 23, 2008 Author Share Posted December 23, 2008 okay cool thanks so much, really helped:) Link to comment https://forums.phpfreaks.com/topic/138220-solved-what-does-the-character-do/#findComment-722607 Share on other sites More sharing options...
Maq Posted December 23, 2008 Share Posted December 23, 2008 Real men solve their own problems: Google PHP Manual I guess you're not a real man... Link to comment https://forums.phpfreaks.com/topic/138220-solved-what-does-the-character-do/#findComment-722616 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.