1

I have 3 input boxes with the following data

<input name="day" type="text" id="day" class="required" maxlength="2" minlength="2" style="width:60px;"> - <input name="mth" type="text" id="mth" class="required" maxlength="2" minlength="2" style="width:60px;"> - <input name="yr" type="text" id="yr" class="required" maxlength="4" minlength="4" style="width:100px;">

which when pulling using my php I want to convert to a string so I can do some stuff with it.

This is what I have so far

$dob = $data['yr'] + "-" + $data['mth'] + "-" + $data['day'];

But that's returning 2016! Could anyone please tell me what I need to do to make it display as 0000-00-00 as a string?

4 Answers 4

4

Javascript uses + for concatenation PHP uses .

$dob = $data['yr'] . "-" . $data['mth'] . "-" . $data['day'];

1
  • Ah. Thanks so much. I'm moving from ASP.net. :)
    – dpDesignz
    May 7, 2012 at 5:32
4
$dob = $data['yr'] ."-" .$data['mth'] ."-" .$data['day'];

if you want to concatenate in php use . operation you are using + oprerand which is used in javascript to concatenate

1
  • Ah. Thanks so much. I'm moving from ASP.net. :)
    – dpDesignz
    May 7, 2012 at 5:32
1

yes, if you want to get the string as a result, use cancatenate operator . as (dot) is used in php for cancatenation.like $dob = $data['yr'] ."-" .$data['mth'] ."-" .$data['day'];

1
  • Ah. Thanks so much. I'm moving from ASP.net. :)
    – dpDesignz
    May 7, 2012 at 5:31
1

This problem is that when you use + in php it converts your string data to numeric data which can be rather confusing for thouse who use other programming languages (for example in python) , to concatanate strings use . instead of +.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.