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
How can I get the last four digits?
"7823" ?
$four_numbers = 22345987823;
How can I get the last four digits?
"7823" ?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
From PHP's own documentation...
a variable's type is determined by the context in which the variable is usedSo 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
There is very good reasons for using === as opposed to == but that is not relevant to the question, where not comparing variable types
Open in new window