Number Counter in PHP

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

33 Replies - 671 Views - Last Post: 31 January 2018 - 11:35 PM Rate Topic: -----

#16 priya05   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 29-January 18

Re: Number Counter in PHP

Posted 31 January 2018 - 03:57 AM

View Postandrewsw, on 31 January 2018 - 03:55 AM, said:

What $value did you use and what output did it give?



<?php
function validatesum($value)
{

echo str_pad($value,10,"0",STR_PAD_LEFT);

}
$value="3479";
validatesum($value);
?>



Output :0000003479
Was This Post Helpful? 0
  • +
  • -

#17 andrewsw   User is offline

  • So Smart! S-M-R-T!
  • member icon

Reputation: 6578
  • View blog
  • Posts: 26,719
  • Joined: 12-December 12

Re: Number Counter in PHP

Posted 31 January 2018 - 03:57 AM

Note that $myNumber = 0000000000; will only assign $myNumber the numeric value of 0, the leading zeroes are just a display option. That is, when you print or display a number you have the option to format the output to show leading zeroes.
Was This Post Helpful? 0
  • +
  • -

#18 priya05   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 29-January 18

Re: Number Counter in PHP

Posted 31 January 2018 - 03:59 AM

View Postandrewsw, on 31 January 2018 - 03:57 AM, said:

Note that $myNumber = 0000000000; will only assign $myNumber the numeric value of 0, the leading zeroes are just a display option. That is, when you print or display a number you have the option to format the output to show leading zeroes.



I'm not using $myNumber = 0000000000; here
Was This Post Helpful? 0
  • +
  • -

#19 andrewsw   User is offline

  • So Smart! S-M-R-T!
  • member icon

Reputation: 6578
  • View blog
  • Posts: 26,719
  • Joined: 12-December 12

Re: Number Counter in PHP

Posted 31 January 2018 - 04:00 AM

Output :0000003479

This is what you want isn't it? So it's working.

But you should store $value as a number (not a string "3479") so that you can easily increment it.

View Postpriya05, on 31 January 2018 - 10:59 AM, said:

View Postandrewsw, on 31 January 2018 - 03:57 AM, said:

Note that $myNumber = 0000000000; will only assign $myNumber the numeric value of 0, the leading zeroes are just a display option. That is, when you print or display a number you have the option to format the output to show leading zeroes.



I'm not using $myNumber = 0000000000; here

You were using it in the previous post.
Was This Post Helpful? 0
  • +
  • -

#20 priya05   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 29-January 18

Re: Number Counter in PHP

Posted 31 January 2018 - 04:08 AM

Yup, but this the final coding.


<?php
function validatesum($value)
{

echo str_pad($value,10,"0",STR_PAD_LEFT);

}
$value="3479";
validatesum($value);
?>



It is correct format or not?

This post has been edited by andrewsw: 31 January 2018 - 04:14 AM
Reason for edit:: Removed previous quote, just press REPLY

Was This Post Helpful? 0
  • +
  • -

#21 andrewsw   User is offline

  • So Smart! S-M-R-T!
  • member icon

Reputation: 6578
  • View blog
  • Posts: 26,719
  • Joined: 12-December 12

Re: Number Counter in PHP

Posted 31 January 2018 - 04:13 AM

Try this:

$value = 3479;
validatesum($value);

// $value is a number so we can add to it
$value += 10;
// the formatting still works
validatesum($value);

Your function is currently misnamed, though, as currently it only displays the value.

And please wrap your code in code tags, it shows how to do this directly in the area where you type your posts.
Was This Post Helpful? 0
  • +
  • -

#22 priya05   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 29-January 18

Re: Number Counter in PHP

Posted 31 January 2018 - 04:36 AM

Its showing like this :

Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 65488 bytes) in C:\xampp\htdocs\password\numbercounter.php on line 5
Was This Post Helpful? 0
  • +
  • -

#23 andrewsw   User is offline

  • So Smart! S-M-R-T!
  • member icon

Reputation: 6578
  • View blog
  • Posts: 26,719
  • Joined: 12-December 12

Re: Number Counter in PHP

Posted 31 January 2018 - 04:41 AM

It works for me so post your code that the error refers to.
Was This Post Helpful? 0
  • +
  • -

#24 priya05   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 29-January 18

Re: Number Counter in PHP

Posted 31 January 2018 - 04:44 AM

View Postandrewsw, on 31 January 2018 - 04:41 AM, said:

It works for me so post your code that the error refers to.

<?php
function validatesum($value)
{

echo str_pad($value,10,"0",STR_PAD_LEFT);

}
$value="3479";
validatesum($value);
?>


Was This Post Helpful? 0
  • +
  • -

#25 priya05   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 29-January 18

Re: Number Counter in PHP

Posted 31 January 2018 - 04:54 AM

Did you find out the output?
Was This Post Helpful? 0
  • +
  • -

#26 andrewsw   User is offline

  • So Smart! S-M-R-T!
  • member icon

Reputation: 6578
  • View blog
  • Posts: 26,719
  • Joined: 12-December 12

Re: Number Counter in PHP

Posted 31 January 2018 - 05:20 AM

I don't understand what you have just asked.

That code runs, and works, it doesn't produce the error message you posted above.



Even though the code works with the string "3479" I prefer, as mentioned, that the variable is declared as a number, without the quotes.
Was This Post Helpful? 0
  • +
  • -

#27 priya05   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 29-January 18

Re: Number Counter in PHP

Posted 31 January 2018 - 05:23 AM

View Postandrewsw, on 31 January 2018 - 05:20 AM, said:

I don't understand what you have just asked.

That code runs, and works, it doesn't produce the error message you posted above.



Even though the code works with the string "3479" I prefer, as mentioned, that the variable is declared as a number, without the quotes.



<?php
function validatesum($value)
{

echo str_pad($value,10,"0",STR_PAD_LEFT);

}
$value="3478";
validatesum($value);
?>



Replace your code here i will check it.
Was This Post Helpful? 0
  • +
  • -

#28 andrewsw   User is offline

  • So Smart! S-M-R-T!
  • member icon

Reputation: 6578
  • View blog
  • Posts: 26,719
  • Joined: 12-December 12

Re: Number Counter in PHP

Posted 31 January 2018 - 05:33 AM

Quote

Replace your code here i will check it.

Do what?
Was This Post Helpful? 0
  • +
  • -

#29 priya05   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 29-January 18

Re: Number Counter in PHP

Posted 31 January 2018 - 05:35 AM

The code which you suggest for me, replace it in my code. You said you got the output.

This post has been edited by andrewsw: 31 January 2018 - 08:44 AM
Reason for edit:: Removed previous quote, just press REPLY

Was This Post Helpful? 0
  • +
  • -

#30 astonecipher   User is offline

  • Senior Systems Engineer
  • member icon

Reputation: 2587
  • View blog
  • Posts: 10,363
  • Joined: 03-December 12

Re: Number Counter in PHP

Posted 31 January 2018 - 07:40 AM

What are the specs of the computer running this, if it runs out of memory that quickly?
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3