This is my homework in which I was asked to convert time from 12 hours to 24 hours where time was provided in this format 05:09:15AM. I am new to programming, that's why instead of going into loops I decided to do it with conditional statements. So, I created 4 conditions (I used conditions as shown here)
What's the problem then? The problem is I am getting an error stating $_time variable is undefined when I am printing $_time. As per my understanding, this is happening because the $_time variable is inside the functions. But, if that's the case, can you guide me how to do this?
<?php
$_a = ("07:29:23PM");
$_a = explode(':',$_a);
if($_a[0] == 12 && $_a[1] <= 59 && strpos("PM", $_a[2] !== FALSE))
{
$_rpl = str_replace("PM","",$_a[2]);
$_time = $_a[0].":".$_a[1].":".$_rpl;
}
elseif($_a[0] < 12 && $_a[1] <= 59 && strpos("PM", $_a[2] !== FALSE))
{
$_a[0] += 12;
$_rpl = str_replace("PM","",$_a[2]);
$_time = $_a[0].":".$_a[1].":".$_rpl;
}
elseif($_a[0] == 12 && $_a[1] <= 59 && strpos("AM", $_a[2] !== FALSE))
{
$_a[0] = 00;
$_rpl = str_replace("AM","",$_a[2]);
$_time = $_a[0].":".$_a[1].":".$_rpl;
}
elseif($_a[0] < 12 && $_a[1] <= 59 && strpos("AM", $_a[2] !== FALSE))
{
$_rpl = str_replace("AM","",$_a[2]);
$_time = $_a[0].":".$_a[1].":".$_rpl;
}
echo $_time;
?>
In your code there are few errors. The strpos syntax is wrong.
strpos("PM", $_a[2] !== FALSE) // this is incorrect
This you have to write
strpos($_a[2],"PM") //string first and search second.
This will return a integer, position of search string in the string, so don't use false instead use >-1
strpos($_a[2],"PM") > -1) //this is the correct method.
Also define $_time; in the starting and initialise it.
<?php
$_a = ("07:29:23PM");
$_a = explode(':',$_a);
$_time = ""; //initialised the variable.
if($_a[0] == 12 && $_a[1] <= 59 && strpos($_a[2],"PM") > -1)
{
$_rpl = str_replace("PM","",$_a[2]);
$_time = $_a[0].":".$_a[1].":".$_rpl;
}
elseif($_a[0] < 12 && $_a[1] <= 59 && strpos($_a[2],"PM")>-1)
{
$_a[0] += 12;
$_rpl = str_replace("PM","",$_a[2]);
$_time = $_a[0].":".$_a[1].":".$_rpl;
}
elseif($_a[0] == 12 && $_a[1] <= 59 && strpos($_a[2],"AM" ) >-1)
{
$_a[0] = 00;
$_rpl = str_replace("AM","",$_a[2]);
$_time = $_a[0].":".$_a[1].":".$_rpl;
}
elseif($_a[0] < 12 && $_a[1] <= 59 && strpos( $_a[2],"AM")>-1)
{
$_rpl = str_replace("AM","",$_a[2]);
$_time = $_a[0].":".$_a[1].":".$_rpl;
}
echo $_time;
?>
Actually, initialising variable was not causing the error. Error was in your strpos syntax, so none of the if condition was true, so no code executed, so while trying to echo $_time; it was undefined. But its good practice to initialise a variable in the starting itself.
You have inbuilt functions to convert the datetime objects. You can refer php manual for that.
If you want to convert manually, you can do like this.
<?php
$_a = ("10:29:23PM");
$_a = explode(':',$_a);
if(strpos( $_a[2],"PM") > -1) //if PM given
{
$_a[2] = str_replace("PM","",$_a[2]); //remove the PM
if($_a[0] <12) //if time less than 12
$_a[0] = $_a[0] + 12; //then add 12 hours
}
if(strpos( $_a[2],"AM") > -1) //if AM given
{
$_a[2] = str_replace("AM","",$_a[2]); //remove AM
if($_a[0]=='12') //if 12 AM
$_a[0]='00'; //make it 0
}
$newtime = $_a[0].':'.$_a[1].':'.$_a[1];
echo $newtime;
?>
The main point is only changing on hour value, adding +12 if PM and change to 00 if AM.
<?php
$s = "07:05:45PM";
$s = explode(':',$s);
$time = substr($s[2],2,4);
$s[2] = substr($s[2],0,2);
if($time == "PM") {
if($s[0] != "12")
$s[0] = $s[0]+12;
}
if($time == "AM") {
if($s[0] == "12")
$s[0] = "00";
}
echo implode(":",$s);
?>
$string="10:29:23PM";
$a=substr($string, 0, 8);
$b= substr($string, 8, 10);
$dates=$a." ".$b;
// 12-hour time to 24-hour time
echo $time_in_24_hour_format = date("H:i:s", strtotime("$dates"));
// 22:29:23
echo $time_in_24_hour_format = date("H:i", strtotime("$dates"));
// 22:29
Related
I'm trying to create a simple attendance application with Laravel. I'm just a little stuck with the logic side.
What I'm trying to do is to match up the time logs of users to the correct schedule.
So in our office, we could have from 2 to 3 breaks, so a typical work shift will look like this:
Time In - 06:00 AM
Break Out 1 - 07:15 AM
Break In 1 - 07:30 AM
Break Out 2 - 11:30 AM
Break In 2 - 12:00 PM
Time Out - 03:00 PM
Now a user typically just uses the biometric device and selects "in" or "out".
My problem is given something like this:
05:58 AM, in
07:17 AM, out
07:31 AM, in
05:58 AM, out
How can the system tell which time log (07:17 AM) belongs to which time slot (Break Out 1)?
Thank you so much for the help, and if my question is unclear, I'm sorry.
Here's a sample code of what I've done so far. It works but I think it's really messy:
$day_shift = [
"time_in" => date('H:i:s', strtotime("06:00 AM")),
"break_out_1" => date('H:i:s', strtotime("07:15 AM")),
"break_in_1" => date('H:i:s', strtotime("07:30 AM")),
"break_out_2" => date('H:i:s', strtotime("11:30 AM")),
"break_in_2" => date('H:i:s', strtotime("12:00 PM")),
"break_out_3" => '',
"break_in_3" => '',
"time_out" => date('H:i:s', strtotime("03:00 pM")),
];
foreach ($day_shift as $key => $userScheduleTime) {
if ($userScheduleTime !== "") {
$time = $logDate->date." ".$userScheduleTime;
} else {
$time = "";
}
$schedules[] = array('time' => $time, 'type' => $types[$i%2],'name'=>$key);
$i++;
}
// logTimes is a collection of time logs
foreach ($logTimes as $logTime) {
$logs[] = $logTime->toArray();
}
$cloneLogs = $logs;
$lastlog = end($cloneLogs);
if ($lastlog["log_type"] == "login") {
$dayOut = "";
} else {
$dayOut = $lastlog["log_time"];
}
$lastout = null;
$size = count($logs);
do {
if ($logs[$size-1]['log_type'] == 'logout') {
$lastout = $logs[$size-1];
break;
}
$size--;
} while ($size > 0);
$lastlog = null;
for ($i = 0; $i < count($schedules); $i++) {
$logTime = current($logs);
if ($lastlog == $logTime['log_type']) {
next($logs);
}
// checks the first log, calculates how late the person is
if ($i == 0) {
if ($logTime['log_type'] == "login") {
$timein = $schedules[0]['time'];
if (strtotime(date("Y-m-d H:i", strtotime($logTime['log_time']))) >
strtotime(date("Y-m-d H:i", strtotime($timein)))) {
$lates[$logTime['log_time']] = true;
$lates["totallate"] += getDifferenceInMinutes($logTime['log_time'], $timein);
}
$lastlog = $logTime["log_type"];
next($logs);
}
}
if ($schedules[$i]['type']==$logTime['log_type'] && $i!=0 && $lastlog !== $logTime["log_type"]) {
$nextSched = isset($schedules[$i+1])?$schedules[$i+1]:$schedules[$i];
$j = 1;
while ($nextSched['time']=="" && ($i+$j)<=count($schedules)) {
$nextSched = $schedules[$i+$j];
$j++;
}
if (strtotime($nextSched['time'])>strtotime($logTime['log_time']) && $logTime["log_time"] != $dayOut) {
// checks and calculates if the user has undertime
if (strtotime($nextSched['time']) > strtotime($logTime['log_time']) && $nextSched['type']=='login') {
if (strtotime($logTime['log_time']) < strtotime($schedules[$i]['time'])) {
$lates[$logTime['log_time']] = true;
$lates["totalunder"] += getDifferenceInMinutes($logTime['log_time'], $schedules[$i]['time']);
}
}
// checks and calculates if the user has overbreaks
if (date('H:i', strtotime($schedules[$i]['time'])) <
date('H:i', strtotime($logTime['log_time'])) &&
$logTime['log_type'] == 'login') {
$lates[$logTime['log_time']] = true;
$lates["totalover"] += getDifferenceInMinutes($schedules[$i]['time'], $logTime['log_time']);
}
$lastlog = $logTime["log_type"];
next($logs);
}
if ($i+1==count($schedules)) {
next($logs);
}
if ($logTime["log_time"] == $dayOut && $dayOut !== null) {
$timeOut = $schedules[count($schedules)-1]["time"];
if (strtotime(date("Y-m-d H:i", strtotime($logTime["log_time"]))) <
strtotime(date("Y-m-d H:i", strtotime($timeOut)))) {
$lates[$logTime["log_time"]] = true;
$lates["totalunder"] += getDifferenceInMinutes($logTime['log_time'], $timeOut);
$lastlog = $logTime["log_type"];
}
break;
}
}
}
I have the following code and it's giving me hell of a time as to why the variables inside the for ($x = 1; $x <= $quantity; $x++) loop are not returning anything but null when called in the database insert query that's following it, or when I try to debug with var_dump($cVnum); for instance.
Here is the code:
if ($cash >= $product['cost'] * $quantity) {
// Substract cash
$receiver = $userData['login'];
if ($receiver != "") {
//all variables safe
$database->setDB("account")->mkquery("UPDATE {{table}} SET ".$cashfield." = (".$cashfield." - ".$product['cost'] * $quantity.") WHERE id = '".$accountid."' LIMIT 1", "account");
// For each quantity
for ($x = 1; $x <= $quantity; $x++) {
// Insert vnums to item_award
for ($i = 1; $i <= 4; $i++) {
$cVnum = $product['vnum'.$i];
if ($cVnum > 0) {
$socket0 = 0;
$socket2 = 0;
if ($product['vnum'.$i.'_time'] > 0) {
if ($cVnum == 72701 || ($cVnum > 71069 && $cVnum < 71075) || ($cVnum > 72722 && $cVnum < 72731)) {
$socket2 = $product['vnum'.$i.'_time'];
} else if ($cVnum == 47001 ||
($cVnum > 41136 && $cVnum < 41145) ||
($cVnum > 45078 && $cVnum < 45084) ||
($cVnum > 71164 && $cVnum < 71168) ||
($cVnum >= 45139 && $cVnum <= 45144) ||
($cVnum >= 41311 && $cVnum <= 41314)) {
$socket0 = time() + ($product['vnum'.$i.'_time']*60*60*24);
} else {
$socket2 = time() + ($product['vnum'.$i.'_time']*60*60*24);
}
}
}
}
}
$database->setDB("player");
$insert = [
"login" => $receiver,
"vnum" => $cVnum,
"count" => $quantity,
"given_time" => array("func", "NOW()"),
"socket0" => 22,
"mall" => 1
];
$logok = $database->insert($insert, "item_award");
}
}
I've been at it for like six hours now and can't figure out what's wrong.
UPDATE: I narrowed down where the problem is with $cVnum = $product['vnum'.$i];. I noticed that when I removed $i and it became $product['vnum'] the variable returned the value I expected. Could there be a fix so that I can use with the $i variable though? Why is it returning null when $i variable is included?
With String Operators you have to use double quotes if you want PHP parse your variable : $cVnum = $product["vnum".$i];
i want to add all digit in a number and if it is 11,22 then i want to display only 11 or 22 else i want to make it a single digit.
example 30=3+0=3
28=2+8=10=1+0=1
i just made a codebut it have an error
please help.
<?php
$day = 17;
$month = 8;
$year = 1993;
function sumday($day)
{
if ($day == 11)
{
$sday = 11;
}
elseif ($day == 22)
{
$sday = 22;
}
elseif ($day == 29)
{
$sday = 11;
}
else
{
do {
$nday = $day . "";
$sday = 0;
for ($i = 0; $i < strlen($nday); ++$i)
{
$sday += $nday[$i];
}
while ($sday <=9);
}
return $sday;
}
First of all I would suggest you to learn to separate the tasks that a function do.
You ask to sum up the digits of a number, you may first create a function called sum_digits
<?php
function sum_digits($num) {
if ($num < 10)
return $num;
return $num % 10 + sum_digits(floor($num/10));
}
and then via conditional do whatever you need to do.
please refer to unnikked's answer, that's a good answer.
And here's the full code, combined with unnikked's answer
<?php
$day = 17;
$month = 8;
$year = 1993;
function sumday($day)
{
if ($day == 11)
{
$sday = 11;
}
elseif ($day == 22)
{
$sday = 22;
}
elseif ($day == 29)
{
$sday = 11;
}
else{
$sday = $day;
do {
$sday = $sday % 10 + floor($sday/10);
} while ($sday >= 10);
}
return $sday;
}
?>
EDIT: If you want to return the sum if it's 11,22,33 in the while loop, then put the conditions in the while loop rather than using if else condition, it's much simpler tho :)
function sumday($day)
{
$sday = $day;
while ($sday >= 10 && $sday != 11 && $sday != 22 && $sday != 29){
$sday = $sday % 10 + floor($sday/10);
}
return $sday;
}
EDIT: here's the logic that can split the day and sum them
function sumday($day)
{
$sday = $day;
$arrday = str_split($sday); // split the day into array
$sumarrday = 0;
for($i = 0; $i < strlen((string)$sday); $i++){
$sumarrday = $sumarrday + $arrday[$i]; // sum the day from the array
}
$sday = $sumarrday;
// here you can modify the condition of while statement for your needs
// for example, if you want to return 29 when 29 shows up, add this to your condition, && $sday != 29
while ($sday >= 10){
$sday = $sday % 10 + floor($sday/10);
}
return $sday;
}
Try this:
else {
$nday = $day . ""; //moved out wrom loop
do {
$sday = 0;
for ($i = 0; $i < strlen($nday); ++$i)
{
$sday += $nday[$i];
}
$nday = $sday . ""; // you forget this line
while ($sday <=9);
}
I'm having an asbolute nightmare dealing with an array of numbers which has the following structure :
Odd numbers in the array : NumberRepresenting Week
Even numbers in the array : NumberRepresenting Time
So for example in the array :
index : value
0 : 9
1 : 1
2 : 10
3 : 1
Would mean 9 + 10 on Day 1 (Monday).
The problem is, I have a an unpredictable number of these and I need to work out how many "sessions" there are per day. The rules of a session are that if they are on a different day they are automatically different sessions. If they are next to each other like in the example 9 + 10 that would count as a single session. The maximum number than can be directly next to eachother is 3. After this there needs to be a minimum of a 1 session break in between to count as a new session.
Unfortunately, we cannot also assume that the data will be sorted. It will always follow the even / odd pattern BUT could potentially not have sessions stored next to each other logically in the array.
I need to work out how many sessions there are.
My code so far is the following :
for($i = 0; $i < (count($TimesReq)-1); $i++){
$Done = false;
if($odd = $i % 2 )
{
//ODD WeekComp
if(($TimesReq[$i] != $TimesReq[$i + 2])&&($TimesReq[$i + 2] != $TimesReq[$i + 4])){
$WeeksNotSame = true;
}
}
else
{
//Even TimeComp
if(($TimesReq[$i] != ($TimesReq[$i + 2] - 1))&& ($TimesReq[$i + 2] != ($TimesReq[$i + 4] - 1)))
$TimesNotSame = true;
}
if($TimesNotSame == true && $Done == false){
$HowMany++;
$Done = true;
}
if($WeeksNotSame == true && $Done == false){
$HowMany++;
$Done = true;
}
$TimesNotSame = false;
$WeeksNotSame = false;
}
However this isn't working perfectly. for example it does not work if you have a single session and then a break and then a double session. It is counting this as one session.
This is, probably as you guessed, a coursework problem, but this is not a question out of a textbook, it is part of a timetabling system I am implementing and is required to get it working. So please don't think i'm just copy and pasting my homework to you guys!
Thank you so much!
New Code being used :
if (count($TimesReq) % 2 !== 0) {
//throw new InvalidArgumentException();
}
for ($i = 0; $i < count($TimesReq); $i += 2) {
$time = $TimesReq[$i];
$week = $TimesReq[$i + 1];
if (!isset($TimesReq[$i - 2])) {
// First element has to be a new session
$sessions += 1;
$StartTime[] = $TimesReq[$i];
$Days[] = $TimesReq[$i + 1];
continue;
}
$lastTime = $TimesReq[$i - 2];
$lastWeek = $TimesReq[$i - 1];
$sameWeek = ($week === $lastWeek);
$adjacentTime = ($time - $lastTime === 1);
if (!$sameWeek || ($sameWeek && !$adjacentTime)) {
if(!$sameWeek){//Time
$Days[] = $TimesReq[$i + 1];
$StartTime[] = $TimesReq[$i];
$looking = true;
}
if($sameWeek && !$adjacentTime){
}
if($looking && !$adjacentTime){
$EndTime[] = $TimesReq[$i];
$looking = false;
}
//Week
$sessions += 1;
}
}
If you want a single total number of sessions represented in the data, where each session is separated by a space (either a non-contiguous time, or a separate day). I think this function will get you your result:
function countSessions($data)
{
if (count($data) % 2 !== 0) throw new InvalidArgumentException();
$sessions = 0;
for ($i = 0; $i < count($data); $i += 2) {
$time = $data[$i];
$week = $data[$i + 1];
if (!isset($data[$i - 2])) {
// First element has to be a new session
$sessions += 1;
continue;
}
$lastTime = $data[$i - 2];
$lastWeek = $data[$i - 1];
$sameWeek = ($week === $lastWeek);
$adjacentTime = ($time - $lastTime === 1);
if (!$sameWeek || ($sameWeek && !$adjacentTime)) {
$sessions += 1;
}
}
return $sessions;
}
$totalSessions = countSessions(array(
9, 1,
10, 1,
));
This of course assumes the data is sorted. If it is not, you will need to sort it first. Here is an alternate implementation that includes support for unsorted data.
function countSessions($data)
{
if (count($data) % 2 !== 0) throw new InvalidArgumentException();
$slots = array();
foreach ($data as $i => $value) {
if ($i % 2 === 0) $slots[$i / 2]['time'] = $value;
else $slots[$i / 2]['week'] = $value;
}
usort($slots, function($a, $b) {
if ($a['week'] == $b['week']) {
if ($a['time'] == $b['time']) return 0;
return ($a['time'] < $b['time']) ? -1 : 1;
} else {
return ($a['week'] < $b['week']) ? -1 : 1;
}
});
$sessions = 0;
for ($i = 0; $i < count($slots); $i++) {
if (!isset($slots[$i - 1])) { // First element has to be a new session
$sessions += 1;
continue;
}
$sameWeek = ($slots[$i - 1]['week'] === $slots[$i]['week']);
$adjacentTime = ($slots[$i]['time'] - $slots[$i - 1]['time'] === 1);
if (!$sameWeek || ($sameWeek && !$adjacentTime)) {
$sessions += 1;
}
}
return $sessions;
}
Here is my little attempt at solving your problem. Hopefully I understand what you want:
$TimesReq = array(9,4,11,4,13,4,8,4,7,2,12,4,16,4,18,4,20,4,17,4);
// First just create weeks with all times lumped together
$weeks = array();
for($tri=0; $tri<count($TimesReq); $tri+=2){
$time = $TimesReq[$tri];
$week = $TimesReq[$tri+1];
$match_found = false;
foreach($weeks as $wi=>&$w){
if($wi==$week){
$w[0] = array_merge($w[0], array($time));
$match_found = true;
break;
}
}
if(!$match_found) $weeks[$week][] = array($time);
}
// Now order the times in the sessions in the weeks
foreach($weeks as &$w){
foreach($w as &$s) sort($s);
}
// Now break up sessions by gaps/breaks
$breaking = true;
while($breaking){
$breaking = false;
foreach($weeks as &$w){
foreach($w as &$s){
foreach($s as $ti=>&$t){
if($ti>0 && $t!=$s[$ti-1]+1){
// A break was found
$new_times = array_splice($s, $ti);
$s = array_splice($s, 0, $ti);
$w[] = $new_times;
$breaking = true;
break;
}
}
}
}
}
//print_r($weeks);
foreach($weeks as $wi=>&$w){
echo 'Week '.$wi.' has '.count($w)." session(s):\n";
foreach($w as $si=>&$s)
{
echo "\tSession ".($si+1).":\n";
echo "\t\tStart Time: ".$s[0]."\n";
echo "\t\tEnd Time: ".((int)($s[count($s)-1])+1)."\n";
}
}
Given $TimesReq = array(9,4,11,4,13,4,8,4,7,2,12,4,16,4,18,4,20,4,17,4); the code will produce as output:
Week 4 has 4 session(s):
Session 1:
Start Time: 8
End Time: 10
Session 2:
Start Time: 11
End Time: 14
Session 3:
Start Time: 16
End Time: 19
Session 4:
Start Time: 20
End Time: 21
Week 2 has 1 session(s):
Session 1:
Start Time: 7
End Time: 8
Hope that helps.
I'm trying to rewrite a pascal program to PHP, and don't understand what this part of pascal function do:
while (u[3] <> 1) and (u[3]<>0) and (v[3]<>0)do
begin
q:=u[3] div v[3];
for i:=1 to 3 do
begin
t:=u[i]-v[i]*q;
u[i]:=v[i];
v[i]:=t;
{writeln('u',i,'=',u[i],' v',i,'=',v[i]); }
end;
end;
if u[1]<0 then u[1]:=n+u[1];
rae:=u[1];
Please help to rewrite it to PHP.
Thanks.
A very literal translation of that code, should be this one:
while ($u[3] != 1 && $u[3] != 0 && $v[3] != 1 )
{
$q = floor($u[3] / $v[3]);
for ($i = 1; $i <= 3; $i++)
{
$t = $u[$i] - $v[$i] * $q;
$u[$i] = $v[$i];
$v[$i] = $t;
//writeln('u',i,'=',u[i],' v',i,'=',v[i]);
}
}
if ($u[1] < 0 )
$u1] = $n + $u[1];
$rae = $u[1];
Of course, u and v are arrays. Sorry for not giving any more info, but it's been like 10 years since Pascal and I last saw each other, but we had a profound romance for a long time, since I feel inlove for to hotties(C# and PHP) :)
while ($u[3] != 1) && ($u[3] != 0) && ($v[3] != 0) {
$q = floor($u[3] / $v[3]);
for ($i = 1; $i <= 3; $i++) {
$t = $u[$i] - $v[$i] * $q;
$u[$i] = $v[$i];
$v[$i] = $t;
echo "u$i={$u[$i]} v$i={$v[$i]}\n";
}
}
if ($u[1] < 0) {
$u[1] = $n + $u[1];
}
$rae = $u[1];
2 small corrections to David's code:
while ($u[3] != 1 && $u[3] != 0 && $v[3] != 1 )
should be
while ($u[3] != 1 && $u[3] != 0 && $v[3] != 0 )
and
for ($i = 1; $i < 3; $i++)
i never reaches the value of 3
for ($i = 1; $i <= 3; $i++)
May be the Writeln can be translated to
echo 'u'.$i.'='.$u[$i].' v'.$i.'='.$v[$i];
When you do the translation of arrays, take into account that arrays in php uses 0 as the first index.
$u= array( 3, 5, 22 )
echo u[1]; // prints 5
while($u[3] != 1 && $u[3] != 0 && $v[3] != 0)
{
$q = ($u[3] - ($u[3] % $v[3]) ) / $v[3]; //just the same as floor($u[3]/$v[3]), but i want to use % here :)
for ($i = 1; $i <= 3; $i++)
{
$t = $u[$i] - $v[$i]*$q;
$u[$i] = $v[$i];
$v[$i] = $t;
echo '<br />u'.$i.'='.$u[$i].' v'.$i.'='.$v[$i];
}
}
if ($u[1] < 0) $u[1] = $n + $u[1];
$rae = $u[1];
I dont know pascal But i have tried :)
while ($u[3]!=1 && $u[3]!=0 && $v[3]!=0) [
$q=floor($u[3]/ $v[3]);
for ($i=1;$i<3;$i++) {
$t=$u[$i]-$v[$i]*$q;
$u[$i]=$v[$i];
$v[$i]=$t;
echo "u".$i."=".$u[$i]."v".$i."=".$v[$i];
}
if ($u[1]<0) {
$u[1]=$n+$u[1];
}
$rae=$u[1];
In php variable Name Start With $
No Begin End used here in php only braces :)