PHP Why can't initialize static variable to time()? - static

I received error when set static variable initial value to time().
The error message is "syntax error, unexpected '(', expecting ',' or ';' in [...][...] on line 7"
<?php
define("DEBUG", true);
define("NEWLINE", "<br>");
...
function debugMsg($msg) {
static $lastTime = time();
if(DEBUG==true) echo date('Y-m-d H:i:s', time())." ".$msg." (".(time()-$lastTime)." seconds)".NEWLINE;
$lastTime = time();
}
?>
debugMsg("XXX task completed");
//My expected output like below:
//2015-05-01 15:04:47 XXX task completed (2 seconds)

From the PHP Documentation link
Static variables may be declared as seen in the examples above. Trying to assign values to these variables which are the result of expressions will cause a parse error.
You are trying to assign a value to a variable which is the result of an expression

Related

What does "no error" mean when writing to a file in Lua?

I am writing a script to add one line in a .txt per video while using MPV.
However, I am getting a weird error on line 68 with the for loop.
It merely tells me: no error. If I add an error parameter to file:write(message, error), it gives me another error message, stating: bad argument #2 to 'write' (string expected, got function). Any help would be appreciated.
function on_file_end(event)
if not paused then totaltime = totaltime + os.clock() - lasttime end
local message = (totaltime .. "s, " .. timeloaded .. ", " .. filename)
local lines = {}
local file = io.open(logpath, "r+")
if file_exists(logpath) then
for l in file:lines() do
if not l:find(message, 1, true) then
lines[#lines+1] = 1
file:write(message)
end
end
file:close()
end
end
bad argument #2 to 'write' (string expected, got function)
error is not an "error parameter" it is a global function that allows to raise your own errors in Lua.
See https://www.lua.org/manual/5.4/manual.html#pdf-error

How to create a for loop when the variable is in a sentence?

I want to create a for loop when the variable is in a string format.
my code is:
for i in range(1,32):
DEP_RATE = pd.read_sql_query('select DAYNUM,YYYYMM,sum(DEP_RATE) from ASPM.aspm_qtr where LOCID="ABQ" and DAYNUM = i:"{i}" group by YYYYMM',{i:str(i)},rconn)
the error is:
'dict' object has no attribute 'cursor'
I used this code:
for i in range(1,32):
DEP_RATE = pd.read_sql_query('select DAYNUM,YYYYMM,sum(DEP_RATE) from ASPM.aspm_qtr where LOCID="ABQ" and DAYNUM = "i" group by YYYYMM',rconn, params={i:str(i)})
It hasn't error but doesn't work. the problem is:
("Truncated incorrect DOUBLE value: 'i'")
This appears to be a PANDAS question, yes? I believe it's an argument mismatch with the read_sql_query API. The function signature is:
pandas.read_sql_query(sql, con, index_col=None, coerce_float=True, params=None, parse_dates=None, chunksize=None)
Which roughly says that the first two positional arguments are required, and everything else is defaulted or optional.
To make it more obvious, consider putting your core SQL string on a separate line:
sql = 'SELECT daynum, ... AND daynum = :i'
for i in range(1, 32):
DEP_RATE = pd.read_sql_query(sql, {i: str(i)}, rconn)
With this formulation, it's more obvious that your arguments do not match the API: your second argument is off. Perhaps you want (note the additional keyword argument params):
DEP_RATE = pd.read_sql_query(sql, rconn, params={i: str(i)})
I corrected the code. The correct answer is:
DEP_RATE = list(np.zeros((1,1)))*31
for i in range(1,32):
DEP_RATE[i-1] =np.array(pd.read_sql_query('select DAYNUM,YYYYMM,sum(DEP_RATE) from ASPM.aspm_qtr where LOCID="ABQ" and DAYNUM = '+str(i)+' group by YYYYMM;',rconn, params={i:str(i)}))

loop problems with string variable - error messages

I am trying to make a loop for my dataset 'data1' to check and make a sum of the variable 'hours2015' in case the type of caregiver is the same (string variable= doctor, nurse, physical therapist, etc. So for each type of caregiver I would like to make the sum of total hours worked in 2015. I keep getting a syntax error messages. I can't seem to get the code right. Can anyone help me please? Thanks!
for(i in 1:133) {
totalhours2015[i] <- data1$hours2015[i] + data1$hours2015[i+1]
if {("data1$typecaregiver" [i] == "data1$typecaregiver" [i+1]) }
}
Error: unexpected 'if' in:
"for(i in 1:133){ .
totalhours2015[i] <- newrusthuizendata$uren2015[i] + newrusthuizendata$uren2015[i+1] if"
}
Error: unexpected '}' in "}"

CodeIgniter unknown SQL error

Here is my selection code from db:
$q = $this->db->like('Autor1' or 'Autor2' or 'Autor3' or 'Autor4', $vyraz)
->where('stav', 1)
->order_by('id', 'desc')
->limit($limit)
->offset($offset)
->get('knihy');
return $q->result();
Where $vyraz = "Zuzana Šidlíková";
And the error is:
Nastala chyba databázy
Error Number: 1054
Unknown column '1' in 'where clause'
SELECT * FROM (\knihy`) WHERE `stav` = 1 AND `1` LIKE '%Zuzana Šidlíková%' ORDER BY `id` desc LIMIT 9
Filename: C:\wamp\www\artbooks\system\database\DB_driver.php
Line Number: 330
Can you help me solve this problem?
Your syntax is wrong for what you're trying to do, but still technically valid, because this:
'Autor1' or 'Autor2' or 'Autor3' or 'Autor4'
...is actually a valid PHP expression which evaluates to TRUE (because all non-empty strings are "truthy"), which when cast to a string or echoed comes out as 1, so the DB class is looking to match on a column called "1".
Example:
function like($arg1, $arg2)
{
return "WHERE $arg1 LIKE '%$arg2%'";
}
$vyraz = 'Zuzana Šidlíková';
echo like('Autor1' or 'Autor2' or 'Autor3' or 'Autor4', $vyraz);
// Output: WHERE 1 LIKE '%Zuzana Šidlíková%'
Anyways, here's what you need:
$q = $this->db
->like('Autor1', $vyraz)
->or_like('Autor2', $vyraz)
->or_like('Autor3', $vyraz)
->or_like('Autor4', $vyraz)
->where('stav', 1)
->order_by('id', 'desc')
->limit($limit)
->offset($offset)
->get('knihy');

Compact C Folding in Vim

I'm trying to make a simple Vim script that would create very compact top-level folds for c files. Ideally, if it was run on this code:
static void funca(...)
{
...
}
/* Example comment */
static void funcb(...)
{
...
}
Then it would create folds which would look like this when closed:
+-- x Lines: static void funca(...)----------------------
+-- x Lines: static void funcb(...)----------------------
So basically it would be like foldmethod=syntax with foldlevel=1, except that each fold would start one line further up, and would extend further down to include all following blank lines.
I know how to make one of these folds (assuming foldmethod=manual):
/^{<cr>kVnn?^$<cr>zf
But I'm not sure how to put it into a function. This is my effort:
function Cfold()
set foldmethod=manual " Manual folds
ggzE " Delete all folds
while (/^{<cr>) " Somehow loop through each match
kVnn?^$<cr>zf " This would work fine except for the last function
endwhile
endfunction
map <Leader>f :call Cfold()<cr>
But it isn't valid, I'm not entirely sure how functions work. Also, it won't work for the last function in the file, since it won't find '^{' again. If someone could help me get this working, and somehow add a case for the last function in a file, I would be extremely grateful.
Thanks in advance :)
You can create folds programmatically using the foldexpr and foldtext. Try this, though you may have to tweak CFoldLevel so it doesn't swallow non-function parts of the code:
function! CFoldLevel(lnum)
let line = getline(a:lnum)
if line =~ '^/\*'
return '>1' " A new fold of level 1 starts here.
else
return '1' " This line has a foldlevel of 1.
endif
endfunction
function! CFoldText()
" Look through all of the folded text for the function signature.
let signature = ''
let i = v:foldstart
while signature == '' && i < v:foldend
let line = getline(i)
if line =~ '\w\+(.*)$'
let signature = line
endif
let i = i + 1
endwhile
" Return what the fold should show when folded.
return '+-- ' . (v:foldend - v:foldstart) . ' Lines: ' . signature . ' '
endfunction
function! CFold()
set foldenable
set foldlevel=0
set foldmethod=expr
set foldexpr=CFoldLevel(v:lnum)
set foldtext=CFoldText()
set foldnestmax=1
endfunction
See :help 'foldexpr' for more details.

Resources