Link to home
Start Free TrialLog in
Avatar of Jazzy 1012
Jazzy 1012

asked on

Last 4 numbers of a variable

In a form a user inputs there information and it gets sent through a variable called $four_numbers

$four_numbers = 22345987823;

Open in new window


How can I get the last four digits?
"7823" ?
ASKER CERTIFIED SOLUTION
Avatar of Nicholas
Nicholas

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Dave Baldwin
Nicholas, that won't work because the substr() function only works on strings.  The '%' or modulo function will work if you have a recent 64-bit version of PHP.  On this computer with an old version of PHP, the first two tries fail.  But on my web hosting with a newer version, all three succeed.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
 "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<title>Untitled</title>
</head>
<body>
<?php 
$four_numbers = 22345987823;
echo $four_numbers;
echo "<br>";
echo $four_numbers % 10000;
echo "<br>";
$four_numbers = 2345987823;
echo $four_numbers;
echo "<br>";
echo $four_numbers % 10000;
echo "<br>";
$four_numbers = 345987823;
echo $four_numbers;
echo "<br>";
echo $four_numbers % 10000;
 ?>
</body>
</html>

Open in new window

Avatar of Nicholas
Nicholas

Thought PHP was agnostic of a string vs a number - anyway worked fine for me but if indeed he is on a 64 bit system then your code is nice and clean!
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
From PHP's own documentation...
a variable's type is determined by the context in which the variable is used
So why write extra code to tell PHP it is a string when it's obviously to be treated as a string?
Since it was written as a number, I treated it that way.  I tend to 'unjuggle' types and make sure they're the type I'm expecting.  And I assume that this question is just part of the asker's problem so now they have several possible solutions.
a variable's type is determined by the context...
That is true until it is not true.  Anyone who has some experience with different programming languages and data interchange formats has run into the problems associated with loose data types and type juggling.  Try putting a zero into an XML tag then using the zero in a PHP object made from the XML.  Does it mean FALSE or zero?  If you get it wrong you are dealing with a run-time failure.  JavaScript has good parts and awful parts.  So does PHP.

It's worth the small extra effort to understand data types and respect them, even if PHP does not handle this sort of thing very well.

A good learning resource for new PHP programmers is available here:
https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html

For those a little more advanced, this can be helpful:
https://www.experts-exchange.com/articles/12293/AntiPHPatterns-and-AntiPHPractices.html

Should you be doing comparisons with == or === ?  Your job interview (exit interview) will be able to tell you if you don't already know!
Why pretend that PHP isn't a loosely typed language and force it to be such - is it ever going to change to be like Java, highly doubt it.

There is very good reasons for using === as opposed to == but that is not relevant to the question, where not comparing variable types
I don't have enough time left in my life to argue the fine points of PHP edge cases than can cause run-time failures.  Experience is a great teacher.

Have it your way, Nicholas.

Good luck, Jasmine.

Thanks for trying, Dave.