spiderman07 Posted May 9, 2007 Share Posted May 9, 2007 Hi all, programming newbie here. how to program in PHP to get the following output? x 0 0 0 0 0 x 0 0 0 0 0 x 0 0 0 0 0 x 0 0 0 0 0 x use for loop? any idea? for($i=0;i<4;i++) for(j=??????) { } something like right? thank you! Quote Link to comment https://forums.phpfreaks.com/topic/50583-how-to-do-this/ Share on other sites More sharing options...
spiderman07 Posted May 9, 2007 Author Share Posted May 9, 2007 any idea??? Quote Link to comment https://forums.phpfreaks.com/topic/50583-how-to-do-this/#findComment-248670 Share on other sites More sharing options...
maxim Posted May 9, 2007 Share Posted May 9, 2007 print "x 0 0 0 0\n"; print "0 x 0 0 0\n"; print "0 0 x 0 0\n"; print "0 0 0 x 0\n"; print "0 0 0 0 x\n"; Quote Link to comment https://forums.phpfreaks.com/topic/50583-how-to-do-this/#findComment-248675 Share on other sites More sharing options...
Nameless12 Posted May 9, 2007 Share Posted May 9, 2007 <?php $n = 0; for ($i = 0; $i < 5; $i++) { for ($j = 0; $j < 5; $j++) echo ($j == $n) ? ' x ' : ' 0 '; echo PHP_EOL; $n++; } ?> if you are trying to create a board game such as chess it will be a lot easier if you use bitwise as a method for checking what value is on what square, anyway goodluck. Quote Link to comment https://forums.phpfreaks.com/topic/50583-how-to-do-this/#findComment-248727 Share on other sites More sharing options...
spiderman07 Posted May 9, 2007 Author Share Posted May 9, 2007 could you please explain it? i don understand the code. sorry. really newbie here. Quote Link to comment https://forums.phpfreaks.com/topic/50583-how-to-do-this/#findComment-248898 Share on other sites More sharing options...
Nameless12 Posted May 9, 2007 Share Posted May 9, 2007 Ill just explain the parts you might not be familiar with the rest you should learn by experimenting. Try modifying my source code to understand it, its easier to just see how it works than type how it works. echo ($j == $n) ? 'x' : '0'; this line is actually a short if statement, ?: is called the terinary operator $var = (condition) ? '$var == this text' : 'otherwise $var is this'; this can be rewritten as if (condition) $var = ''; else $var = ''; PHP_EOL is just a constant the constants value is "\n", EOL stands for END OF LINE. Quote Link to comment https://forums.phpfreaks.com/topic/50583-how-to-do-this/#findComment-248920 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.