ob_get_clean and ob_get_contents return content to screen instead of putting it into variable when used with var_dump - php-5.3

I use this function for debugging:
function d($v,$tofile=null) {
static $wasused;
ob_start();
var_dump($v);
$dump = ob_get_clean();
if (is_array($v)) $dump = preg_replace("#=>\n#",'=>',$dump);
if (strlen($dump)>1000 or $tofile) {
fileput('debug.txt',$dump,$wasused);
echo n.n."strlen=".strlen($dump)." >> debug.txt".n.n;
}
elseif (strlen($dump)<80) echo $dump;
else echo n.n.$dump.n.n;
$wasused=true;
}
the problem is it sometimes return content to console, particularly when this content is var_dump result on a big array,
anyone of you have seen this problem before ?

If this is in your php.ini:
implicit_flush = On
change it to this:
implicit_flush = Off

Before assuming that there is a problem with var_dump itself, one would need to verify that fileput() does exactly what the question implies.

Related

stumped by simple groovy variable comparison

I have a groovy script (a Jenkins pipeline) and a simple variable comparison is not working like I expect. I have a class defined to hold some constants, like this:
class email_when {
static final int ON_FAILURE = 0
static final int ALWAYS = 1
}
At a certain point in the script I set an environment variable to one of these states, like this:
env.EMAIL_WHEN = email_when.ALWAYS
Then later, I check the value. This check is always failing and I don't understand why.
echo ("email when = "+env.EMAIL_WHEN+ " always = "+email_when.ALWAYS);
if (env.EMAIL_WHEN == email_when.ALWAYS)
{
echo ("Send email.")
}
else
{
echo ("NO EMAIL")
}
So this always prints
email when = 1 always = 1
NO EMAIL
I don't understand why?
I thought maybe it was some sort of object/value comparison thing? Although I am directly setting env.EMAIL_WHEN to email_when.ALWAYS.
I tried this and it still did the same thing:
if (env.EMAIL_WHEN.equals(email_when.ALWAYS))
Can anyone explain what I am missing?
Thanks!
Everything in env map automatically converted to String
so 1 != '1'

How to use Jenkins local defined variable in IF statement?

I have below code
def result = readFile('res.txt')
echo "${result}"
if ("${result}" > 5 )
{
echo "Yes"
} else {
echo "No"
}
}
It is printing NO but the answer in ${result} -> 11 which means it should print Yes. Can u pls help.
As noted in comments - you should cast the result to integer. Both result.toInteger() and Integer.parseInt(result) will work, although the first one is more straightforward and doesn't complain about extra white space (e.g. trailing endline character).
Besides, you don't need to mess with strange constructs like "${result}" because result is a normal variable, so your code may look like this:
def result = readFile('res.txt')
echo "${result}"
if (result.toInteger() > 5 ) {
echo "Yes"
} else {
echo "No"
}

How to pass a table key in a function for use in a for loop?

For some reason it doesn't appear to work to pass in a table key as a function argument, what is the trick to do this?
I'm trying to wrap the for loop iteration technique in vanilla Lua into a function that has three arguments: (1) the table to iterate, (2) the table_key to check each time, and (3) the value to find. If a match is found, return it, otherwise return nil.
function table_find_match(table, table_key, match_value)
for i=1, #table do
local this = table[i]
if this[table_key] == match_value then
return this[table_key]
end
end
return nil
end
local table_example = {
{
key_example = "string_value_1"
},
{
key_example = "string_value_2"
}
}
local result = table_find_match(table_example, key_example, "string_value_1")
print(result)
Found a solution, if I pass in the table key as a string it works, such as
table_find_match(table_example, "key_example", "string_value_1")
but I really dislike having to convert it into a string, if anyone knows any other workaround to this please share
If you pass it like table_find_match(table_example, key_example, "string_value_1")
the key_example is now considered as a (nil) variable if not defined before executing, so it has to be like
local key_example = "key_example"
local result = table_find_match(table_example, key_example, "string_value_1")
print(result)

Parse Arguments to Lua File

I have File: data.lua
#! /usr/bin/env lua
local a = {
b = {
c = {
version = "z.y"
},
d = {
version = "z.w"
},
getcversion = function ( self )
print( self.c.version )
end
}
}
Now I need to 'getcversion()' Function..
a.b:getcversion()
The problem is, I need to call it from the outside:
data.lua "a.b:getcversion()"
I tried everything that I could, but I couldn't solve this..
Does any one knows how I could call 'getcversion()' ?
Thanks in Advance,
Regards
You'll need to add something like the following to your script:
load(arg[1], "<string>", "t", {a = a})()
load will load the content of the passed parameter (arg[1]) and will return the function that will execute that code in a specific environment (provided as {a = a} table), as you need to pass the values of local variables to your code (Lua 5.2+).
Keep in mind that this will allow the caller to pass arbitrary Lua code to your script, which may be a security issue.
The error handling is left as the exercise for the reader.
Add return a to the end of data.lua to turn it into a module, and then:
$ lua -e 'a = require("data"); print(a.b:getcversion())'
z.y

Youtube API returns wrong value

This is a part of my code:
$key = 'XXX';
function check_video($id, $url) {
$x = get_data('https://www.googleapis.com/youtube/v3/videos?part=status&id='.$url.'&key='.$key);
$x = json_decode($x, true);
if($x['items'][0]['status']['embeddable'] == true){
echo "$id = TRUE --- ";
}else{
echo "$id = FALSE --- ";
}
}
check_video(1, '9bZkp7q19f0');
The function should check if the video is embeddable and then simply echo true or false but it keeps printing "1 = FALSE --- " even though a video is embeddable.
Example of video: http://i.gyazo.com/7a84e78cc665ca4920cef18e341f09b6.png
First, in your code above the variable $key in function check_video() is undefined. The variable $key has to be defined in the function or passed in. If it's just a simple copy/paste snafu then continue:
I don't see the definition for function get_code() so I'll assume it's a simple cURL wrapper.
Second, put the finished url into your browser and see if it works.
https://www.googleapis.com/youtube/v3/videos?part=status&id=9bZkp7q19f0&key=YOUR_KEY
Third, put a print_r($x) after json_decode() to see if the YouTube API is returning an error because of something in the get_code() function (like a timeout). You should check for an error regardless.
For me it worked fine.

Resources