-1

It maybe a duplicate question but I do not have enough reputation to comment on someone's answer. I want to create a user_id in this format 2018-00001, but when I implement the answer here it gives me an error.

syntax error, unexpected 'return' (T_RETURN)

This error is pointing out this line.

$fiveDigitNumber = return str_pad((int) $latestNumber,5,"0",STR_PAD_LEFT);

I created a new table named year_table with two columns LatestNumber and Year, then implemment the function.

function generateStudentId(){
$year = date("Y");
$latestNumber = 0;
$res = mysql_query("select * from year_table where Year=$year");
$result = mysql_fetch_array($res);
if($result)
    $latestNumber = $result['LatestNumber'];
$latestNumber++;
if($result){
    mysql_query("update year_table set LatestNumber=$latestNumber where Year=$year");
}
else
    mysql_query("insert into year_table values($year,$latestNumber)");

$fiveDigitNumber = return str_pad((int) $latestNumber,5,"0",STR_PAD_LEFT);
$studentId = $year.$fiveDigitNumber;
return $studentId;
}
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Mihae Kheel
  • 2,441
  • 3
  • 14
  • 38
  • 1
    u don't have to write return at third last line return str_pad((int) $latestNumber,5,"0",STR_PAD_LEFT); – Pritam Jinal Dec 01 '18 at 13:07
  • Oh the hell, you were right I didn't notice that. – Mihae Kheel Dec 01 '18 at 13:09
  • 3
    The version of php may have mysql turn off but you should be using mysqli anyway. Another problem with your code is that you are not connecting to the database. –  Dec 01 '18 at 13:13
  • sorry I am new to PHP – Mihae Kheel Dec 01 '18 at 13:37
  • This PHP method is prone to **race conditions**, ideally you should do this in MySQL only by using a VIEW which generates the `fiveDigitNumber` – Raymond Nijland Dec 01 '18 at 13:42
  • Is there anyway to do this in a right way? – Mihae Kheel Dec 01 '18 at 13:46
  • Yes but i cant answer because the question is closed.. – Raymond Nijland Dec 01 '18 at 13:48
  • 1
    @RaymondNijland As it should remain closed. The OP would need to ask a new question with their new code and they shouldn't overwrite this one as some end up doing. – Funk Forty Niner Dec 01 '18 at 13:49
  • 1
    @FunkFortyNiner "As it should remain closed" i know i didn't say it was a bad thing this question was closed because you are totally right.. I should have mentioned in mine other comment that the topicstarter should open a new question.when he wants to ask for a better solution for this current code and maybe even refer to this question – Raymond Nijland Dec 01 '18 at 13:59

2 Answers2

1

It is causing T_RETURN because you're using unnecessary return on that line.

syntax error, unexpected 'return' (T_RETURN)

It should be like this,

$fiveDigitNumber =  str_pad((int) $latestNumber,5,"0",STR_PAD_LEFT);
A l w a y s S u n n y
  • 36,497
  • 8
  • 60
  • 103
-1

Just remove the return statement. A return statement is not required in a function call.

$fiveDigitNumber = str_pad((int) $latestNumber,5,"0",STR_PAD_LEFT);
Revanth M
  • 113
  • 10