help with substr

Posted on 2008-06-17
Last Modified: 2010-04-21

I'm trying to work on a check digit routine and need to do 3 things
1) find the length of the variable $paymentReference1
2) set the variable $_toCheck to be all the digits except the last one
3) set the variable $_toMatch to be the last digit

Not sure if the code I have attached is the best way to do this or could it be simpler. Will my code even work?

$paymentReference1 = "10948055";

$_len = strlen($paymentReference1)-1;

$_toCheck = substr($toCheck, 1, $_len);

$_toMatch = substr($toCheck, $_len, 1);

Open in new window

Question by:NeilT
  • 2
LVL 49

Expert Comment

Comment Utility
Looks fine, 3) can be
$_toMatch = substr($toCheck, -1);
LVL 49

Expert Comment

Comment Utility
Actually, 2 is incorrect. Should be
$_toCheck = substr($toCheck, 0, $_len);
LVL 48

Accepted Solution

hernst42 earned 500 total points
Comment Utility
Just try:

$toCheck = $paymentReference1;
$_toCheck = substr($toCheck, 0, -1);
$_toMatch = substr($toCheck, -1);

Author Closing Comment

Comment Utility

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now