Accessing portion of data obtained via a @$_POST

I have a lines of code like:
$s_first_year = @$_POST[‘first_year’];
$s_last_year = @$_POST[‘last-year’];
and want to access the last two digits of the $s_xxxx_year variables.
The first_year and last_year retrieved are from a form via a previous submit.
I thought the $_POST returned an array and recoded the $s_xxxx_year variables as $s_xxx_year[] thinkingI could access them using substr($s_xxx_year, -2) but that didn’t work.
Can somebody point out my misunderstanding of reality.

First of all don’t error suppress with @ use filter_input or filter_var instead (php.net).
Second I don’t exactly know what you’re trying to do? PHP is a server-side language and is kind of hard in what I think you are trying to do. With Javascript it would be easy to do $_ + var + _year (pseudo code) for the variables are not loaded, whereas in PHP they are already loaded. I guess you could do it, but it would require reloading the page (annoying to the user) and some trickery that isn’t worth it in my opinion.

The way I would tackle the problem (If I get what you are trying to do) is use DateTime class to manipulate the dates to get the desired results you want and stick them into an array (or object).

Maybe someone else here knows what you are really trying to do or maybe you can describe it better? Sorry I wasn’t much help.

I thought the $_POST returned an array and recoded the $s_xxxx_year variables as $s_xxx_year[]
Depends how the form sends it. PHP doesn't modify it, it just receives it.
I could access them using substr($s_xxx_year, -2) but that didn't work.

Why doesn’t that work? What does it do instead?

Sponsor our Newsletter | Privacy Policy | Terms of Service