Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Apr 2016
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    How to bold specific numbers using modulos

    Hello,

    I will like to use PHP to generate the following numbers:

    12345678910111213141516171819202122232425262728293031323334353637383940

    And THEN bold and underline the following specific numbers like this:

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40.

    Please, how would I achieve this? I used the following codes but not working:

    While ($count => 40) {
    If ($count %2 ==1)
    Echo $count;
    }

    Please, kindly help me.

    Thanks and God blessed.

  2. #2
    Senior Coder deathshadow's Avatar
    Join Date
    Feb 2016
    Location
    Keene, NH
    Posts
    3,345
    Thanks
    4
    Thanked 482 Times in 470 Posts
    The modulo needed to target specific elements would be 10 == 0, since you want all the tens.

    echo $count % 10 == 0 ? $count : '<b>' . $count . '</b>', ' ';

    Though I'd have to know what the data is and WHY it's getting bold or why it isn't... since data dictates what the actual markup should be. From an efficiency standpoint in terms of what's created for markup, the above is trash.

    In any case, a modulo is just the remainder after a divide... so %2 means that 0 is 0, 1 is 1, 2 is 0, 3 is 1, and so forth. %10 means that you just get the final digit, 0..9 as the result...

    Code:
     0 / 10 == 0 remainder 0,  so 0 % 10 == 0
     2 / 10 == 0 remainder 2, so 2 % 10 == 2
    28 / 10 == 2 remainder 8, so 28 % 10 == 8
    30 / 10 == 3 remainder 0, so 30 % 10 == 0
    That's all it does, is give you the remainder... so to target all multiples of ten, you $count % 10 == 0

    Interstingly under the hood on x86 platforms the operation for divide and modulo are the same -- divide. It returns the integer result in AX and remainder in DX. This can lead to many C and other high level languages doing divide (a slow operation) multiple times when it doesn't have to.
    Last edited by deathshadow; Apr 14th, 2016 at 07:51 PM.
    “There are two ways of constructing a software design: One way is to make it so simple that there are obviously no deficiencies and the other way is to make it so complicated that there are no obvious deficiencies.” – C.A.R. Hoare, The 1980 ACM Turing Award Lecture
    http://www.cutcodedown.com

  3. #3
    New to the CF scene
    Join Date
    Apr 2016
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi,

    Thanks for your reply. I will go and try this and let you know if it works.

  4. #4
    New to the CF scene
    Join Date
    Apr 2016
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi,

    It works perfectly now. Thanks you so much.

    Please, how can I use for loop instead of while loop to generate the following numbers exactly:

    12345678910111213141516171819202122232425262728293031323334353637383940.

    I used the following codes but not working as expected:

    for ($i = 1; $i < 40; $++)
    {
    echo $i. '<b>'<u>'.$i. '</b>'<.u>';

    Thanks.


 

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •