PHP caveats – int/float type conversion

The following code checks various scenarios in which inbuilt functions like intval, floatval, is_numeric, is_float and filter_var would result for several different types of values. Feel free to run in your local, leave comments if I can make the results more interesting with some more functions.

https://gist.github.com/harshvardhanmalpani/8dd0222e4821d0e495532abe9007fef1

Output / Results

Test Value 1: (string)true
Integer converted Value: 0
Float converted Value: 0
❌ Invalid Integer as per is_numeric
❌ Invalid Float as per is_float
❌ Invalid Integer as per filter validate
❌ Invalid Float as per filter validate
----
Test Value 2: (boolean)1
Integer converted Value: 1
Float converted Value: 1
❌ Invalid Integer as per is_numeric
❌ Invalid Float as per is_float
✅ Valid Integer as per filter validate
✅ Valid Float as per filter validate
----
Test Value 3: (boolean)
Integer converted Value: 0
Float converted Value: 0
❌ Invalid Integer as per is_numeric
❌ Invalid Float as per is_float
❌ Invalid Integer as per filter validate
❌ Invalid Float as per filter validate
----
Test Value 4: (string)false
Integer converted Value: 0
Float converted Value: 0
❌ Invalid Integer as per is_numeric
❌ Invalid Float as per is_float
❌ Invalid Integer as per filter validate
❌ Invalid Float as per filter validate
----
Test Value 5: (integer)0
Integer converted Value: 0
Float converted Value: 0
✅ Valid Integer as per is_numeric
❌ Invalid Float as per is_float
✅ Valid Integer as per filter validate
❌ Invalid Float as per filter validate
----
Test Value 6: (double)-0
Integer converted Value: 0
Float converted Value: -0
✅ Valid Integer as per is_numeric
✅ Valid Float as per is_float
✅ Valid Integer as per filter validate
❌ Invalid Float as per filter validate
----
Test Value 7: (integer)0
Integer converted Value: 0
Float converted Value: 0
✅ Valid Integer as per is_numeric
❌ Invalid Float as per is_float
✅ Valid Integer as per filter validate
❌ Invalid Float as per filter validate
----
Test Value 8: (string)-0
Integer converted Value: 0
Float converted Value: -0
✅ Valid Integer as per is_numeric
❌ Invalid Float as per is_float
✅ Valid Integer as per filter validate
❌ Invalid Float as per filter validate
----
Test Value 9: (string)+0
Integer converted Value: 0
Float converted Value: 0
✅ Valid Integer as per is_numeric
❌ Invalid Float as per is_float
✅ Valid Integer as per filter validate
❌ Invalid Float as per filter validate
----
Test Value 10: (string)-0.0
Integer converted Value: 0
Float converted Value: -0
✅ Valid Integer as per is_numeric
❌ Invalid Float as per is_float
❌ Invalid Integer as per filter validate
❌ Invalid Float as per filter validate
----
Test Value 11: (string)+0.0
Integer converted Value: 0
Float converted Value: 0
✅ Valid Integer as per is_numeric
❌ Invalid Float as per is_float
❌ Invalid Integer as per filter validate
❌ Invalid Float as per filter validate
----
Test Value 12: (string)0
Integer converted Value: 0
Float converted Value: 0
✅ Valid Integer as per is_numeric
❌ Invalid Float as per is_float
✅ Valid Integer as per filter validate
❌ Invalid Float as per filter validate
----
Test Value 13: (string)0w
Integer converted Value: 0
Float converted Value: 0
❌ Invalid Integer as per is_numeric
❌ Invalid Float as per is_float
❌ Invalid Integer as per filter validate
❌ Invalid Float as per filter validate
----
Test Value 14: (string)0.0t
Integer converted Value: 0
Float converted Value: 0
❌ Invalid Integer as per is_numeric
❌ Invalid Float as per is_float
❌ Invalid Integer as per filter validate
❌ Invalid Float as per filter validate
----
Test Value 15: (string)0.1
Integer converted Value: 0
Float converted Value: 0.1
✅ Valid Integer as per is_numeric
❌ Invalid Float as per is_float
❌ Invalid Integer as per filter validate
✅ Valid Float as per filter validate
----
Test Value 16: (double)0.1
Integer converted Value: 0
Float converted Value: 0.1
✅ Valid Integer as per is_numeric
✅ Valid Float as per is_float
❌ Invalid Integer as per filter validate
✅ Valid Float as per filter validate
----
Test Value 17: (integer)1
Integer converted Value: 1
Float converted Value: 1
✅ Valid Integer as per is_numeric
❌ Invalid Float as per is_float
✅ Valid Integer as per filter validate
✅ Valid Float as per filter validate
----
Test Value 18: (string)1
Integer converted Value: 1
Float converted Value: 1
✅ Valid Integer as per is_numeric
❌ Invalid Float as per is_float
✅ Valid Integer as per filter validate
✅ Valid Float as per filter validate
----
Test Value 19: (double)10000
Integer converted Value: 10000
Float converted Value: 10000
✅ Valid Integer as per is_numeric
✅ Valid Float as per is_float
✅ Valid Integer as per filter validate
✅ Valid Float as per filter validate
----
Test Value 20: (string)2e
Integer converted Value: 2
Float converted Value: 2
❌ Invalid Integer as per is_numeric
❌ Invalid Float as per is_float
❌ Invalid Integer as per filter validate
❌ Invalid Float as per filter validate
----
Test Value 21: (double)20000
Integer converted Value: 20000
Float converted Value: 20000
✅ Valid Integer as per is_numeric
✅ Valid Float as per is_float
✅ Valid Integer as per filter validate
✅ Valid Float as per filter validate
----
Test Value 22: (string)2e4
Integer converted Value: 20000
Float converted Value: 20000
✅ Valid Integer as per is_numeric
❌ Invalid Float as per is_float
❌ Invalid Integer as per filter validate
✅ Valid Float as per filter validate
----
Test Value 23: (double)1337
Integer converted Value: 1337
Float converted Value: 1337
✅ Valid Integer as per is_numeric
✅ Valid Float as per is_float
✅ Valid Integer as per filter validate
✅ Valid Float as per filter validate
----
Test Value 24: (string)1e3
Integer converted Value: 1000
Float converted Value: 1000
✅ Valid Integer as per is_numeric
❌ Invalid Float as per is_float
❌ Invalid Integer as per filter validate
✅ Valid Float as per filter validate
----
Test Value 25: (double)10
Integer converted Value: 10
Float converted Value: 10
✅ Valid Integer as per is_numeric
✅ Valid Float as per is_float
✅ Valid Integer as per filter validate
✅ Valid Float as per filter validate
----
Test Value 26: (double)1.3
Integer converted Value: 1
Float converted Value: 1.3
✅ Valid Integer as per is_numeric
✅ Valid Float as per is_float
❌ Invalid Integer as per filter validate
✅ Valid Float as per filter validate
----
Test Value 27: (string)1.4
Integer converted Value: 1
Float converted Value: 1.4
✅ Valid Integer as per is_numeric
❌ Invalid Float as per is_float
❌ Invalid Integer as per filter validate
✅ Valid Float as per filter validate
----
Test Value 28: (string)1t
Integer converted Value: 1
Float converted Value: 1
❌ Invalid Integer as per is_numeric
❌ Invalid Float as per is_float
❌ Invalid Integer as per filter validate
❌ Invalid Float as per filter validate
----
Test Value 29: (string)1.5n
Integer converted Value: 1
Float converted Value: 1.5
❌ Invalid Integer as per is_numeric
❌ Invalid Float as per is_float
❌ Invalid Integer as per filter validate
❌ Invalid Float as per filter validate
----
Test Value 30: (integer)26
Integer converted Value: 26
Float converted Value: 26
✅ Valid Integer as per is_numeric
❌ Invalid Float as per is_float
✅ Valid Integer as per filter validate
✅ Valid Float as per filter validate
----
Test Value 31: (string)0x539
Integer converted Value: 0
Float converted Value: 0
❌ Invalid Integer as per is_numeric
❌ Invalid Float as per is_float
❌ Invalid Integer as per filter validate
❌ Invalid Float as per filter validate
----
Test Value 32: (integer)34
Integer converted Value: 34
Float converted Value: 34
✅ Valid Integer as per is_numeric
❌ Invalid Float as per is_float
✅ Valid Integer as per filter validate
✅ Valid Float as per filter validate
----
Test Value 33: (string)042
Integer converted Value: 42
Float converted Value: 42
✅ Valid Integer as per is_numeric
❌ Invalid Float as per is_float
❌ Invalid Integer as per filter validate
✅ Valid Float as per filter validate
----
Test Value 34: (integer)42
Integer converted Value: 42
Float converted Value: 42
✅ Valid Integer as per is_numeric
❌ Invalid Float as per is_float
✅ Valid Integer as per filter validate
✅ Valid Float as per filter validate
----
Test Value 35: (integer)-42
Integer converted Value: -42
Float converted Value: -42
✅ Valid Integer as per is_numeric
❌ Invalid Float as per is_float
✅ Valid Integer as per filter validate
✅ Valid Float as per filter validate
----
Test Value 36: (string)+42
Integer converted Value: 42
Float converted Value: 42
✅ Valid Integer as per is_numeric
❌ Invalid Float as per is_float
✅ Valid Integer as per filter validate
✅ Valid Float as per filter validate
----
Test Value 37: (string)-42
Integer converted Value: -42
Float converted Value: -42
✅ Valid Integer as per is_numeric
❌ Invalid Float as per is_float
✅ Valid Integer as per filter validate
✅ Valid Float as per filter validate
----

Jenkins auto deploying flask in test env with last build process kill and pipenv

#!/bin/sh
cd $WORKSPACE
export PY=/usr/local/bin/python3
export FLASK_RUN_PORT=4005
$PY -m pip install pipenv
$PY -m pipenv install --dev
export VENV_HOME_DIR=$($PY -m pipenv --venv)
source $VENV_HOME_DIR/bin/activate
flask db upgrade
flask populate-initial-data
export BUILD_ID=XYZapi
$WORKSPACE/killlastbuild.sh
flask run &
deactivate

killlastbuild.sh can have anything inside it depending upon your project requirements. I put 2 process killers to kill background-ed daemon processes from previous build command

ps -Af | grep "xyzapi" | grep -v grep | awk '{print$2}' | head -1 | xargs kill -9
ps -Af | grep "xyzapi" | grep -v grep | awk '{print$2}' | head -1 | xargs kill -9

Total inode count for a particular user in linux

Inode usage for current working directory

echo "Detailed Inode usage for: $(pwd)" ; for d in `find -maxdepth 1 -type d |cut -d\/ -f2 |grep -xv . |sort`; do c=$(find $d |wc -l) ; printf "$c\t\t- $d\n" ; done ; printf "Total: \t\t$(find $(pwd) | wc -l)\n"

Use cd ~ to change pwd to current user home

cd ~

Reference:

Magesh from 2daygeek : https://www.2daygeek.com/linux-check-count-inode-usage/

Radio buttons CSS for wordpress elementor form markup

[type="radio"]:checked + label:before {
background: #bc6060;
box-shadow: 0 0 0 0.25em #000;
}
[type="radio"]:focus + label:after {
content: '\0020\2190';
font-size: 1.5em;
line-height: 1;
vertical-align: -0.125em;
}
[type="radio"] {
border: 0; 
clip: rect(0 0 0 0); 
height: 1px; margin: -1px; 
overflow: hidden; 
padding: 0; 
position: absolute; 
width: 1px;
}
label {
display: block;
cursor: pointer;
line-height: 2;
font-size: 1.2em;
}
[type="radio"] + label {
display: block;
}
[type="radio"] + label:before {
content: '';
display: inline-block;
width: 1em;
height: 1em;
vertical-align: -0.25em;
border-radius: 1em;
border: 0.125em solid #fff;
box-shadow: 0 0 0 0.15em #000;
margin-right: 0.75em;
transition: 0.5s ease all;
}

This unique date 02 02 2020 will come after 1010 years – WRONG MATH

In dd/mm/yyyy format, 2nd February 2020 (02 02 2020) is a palindrome

In really common beliefs, whatsapp forwards were filled with this number being so rare and unique. However it would be stupid to say that such a rare date will occur only after 1010 years.

Here is how:

In whole millennium (1000 years), first few years of first few centuries have many of such dates. Even if you consider MM/DD/YYYY format, you can find a lot of such numbers in first few centuries.

Here are all of such dates in this century (2000 – 2099) in DD/MM/YYYY format

10/02/2001
20/02/2002
01/02/2010
11/02/2011
21/02/2012
02/02/2020
12/02/2021
22/02/2022
03/02/2030
13/02/2031
23/02/2032
04/02/2040
14/02/2041
24/02/2042
05/02/2050
15/02/2051
25/02/2052
06/02/2060
16/02/2061
26/02/2062
07/02/2070
17/02/2071
27/02/2072
08/02/2080
18/02/2081
28/02/2082
09/02/2090
19/02/2091
29/02/2092

10 tips for better freelancing

How to improve yourself as a freelancer? Here are 10 points to take care of:

  1. Communication Skills – convincing power
  2. Make yourself a brand, your ethics will define your future in freelancing. This will help you in scaling also
  3. Consistency, Focus and Dedication
  4. Productivity is Gold, know your value, do math and target for a monthly number
  5. Read!! know clients requirements in depth, if it is small task, fix already without getting the project. This approach will consume time without paying, but would probably make you better
  6. keep challenging yourself, keep improving
  7. Stay away from template bids, no body wants to know which app you did 4 years ago if the project not about an app
  8. Keep your portfolio updated
  9. Take care of clock, predict and quote project timelines intelligently
  10. Exceed expectations of your Client, under-promise and over-deliver

How to migrate Customers from Magento2 to WordPress?

Lets break it into 2 parts:

a. Migrate Customer data
b. Migrate Customer Passwords

1) Migrate Customer Data from Magento2 to WordPress

This is rather easy to be honest, there are several plugins for importing data from CSV and then mapping the CSV fields.

My recommended plugin is Customer/Order/Coupon CSV Import Suite by SkyVerge

It basically does everything from customer email to customer address data. It also provides column mapping and custom attribute import for all users. Also comes with multiple configuration options.

Best Free Alternative is https://wordpress.org/plugins/import-users-from-csv-with-meta
But in this one, you can not import magento2 passwords as of v 1.15.0.1
I will update about the solution very soon. So as of now only paid option is there.

2) Migrate Customer Password from Magento2 to WordPress

This wordpress plugin checks and updates passwords of users migrated from Magento2 to WordPress
Plugin Link: https://github.com/harshvardhanmalpani/migrate-password-from-magento2-to-wordpress

Requirements:

  1. user_pass column will have this kind data from Magento2 dcbdc524f215fd054502dcad5a23a702ec029c02ff8d7051d049f76e29927f8b:C8yVqeuPfkHWvkmipx0iKLPtOUGETpLL:1
  2. usermeta table must have a meta_key “migrated_cs” for this user, meta_value can be anything positive
  3. This plugin file password-migrator.php should be in wp-content/plugins and must be an active plugin

What is does?

For new wordpress based users which dont have anything to do with magento2, it doesnt do anything

For all migrated users (those have the key “migrated_cs”), this plugin checks if input password matches old password from magento2 or not, if matched, it clears the key migrated_cs and updates the password using wordpress’ algorithm ; else results false.

How to solve MySQL server gone away error Maria Db update on cPanel and WHM?

This is related to new MariaDb upgrade from 10.1.41 to 10.1.42 and also for servers which were updated from 10.2.27 to 10.2.28

Go to solution ( Special Thanks to @Valetia )

You will see errors in following formats/messages:

  • No file or input found
  • MySQL Server has gone away
  • Connection to MySQL Server failed
  • sqlstate[hy000]: general error: 2013 lost connection to mysql server during query
  • General error: 2006 MySQL server has gone away
  • ERROR 2006 (HY000): MySQL server has gone away
  • ERROR 2013 (HY000): Lost connection to MySQL server during query

Log files (/var/lib/mysql/$hostname.err) will have or can have any of these errors:

  • assertion fail /home/buildbot/buildbot/padding_for_CPACK_RPM_BUILD_SOURCE_DIRS_PREFIX/mariadb-10.2.28/storage/innobase/dict/dict0dict.cc line 1467
  • the resulting row size is greater than maximum allowed size (8126) for a record on index leaf page
  • stack_bottom = 0x0 thread_stack 0x49000 mysys/stacktrace.c:268(my_print_stacktrace)[0x5564e62807bb] sql/signal_handler.cc:209(handle_fatal_signal)[0x5564e5d4b4f5]

Other Symptoms:

/etc/init.d/mysql start
Starting MySQL/etc/init.d/mysql: line 159: kill: (10704) – No such process
[FAILED]


Solution:

yum downgrade MariaDB-* -y
whmapi1 update_updateconf RPMUP=manual UPDATES=manual

(run as root #)

This will downgrade MariaDb installation to previous version
Line 2 will cancel automatic update on WHM/CPanel installations

If you are not using WHM, use line 1 only OR you can downgrade the repository individually too

yum downgrade MariaDB-server MariaDB-common MariaDB-shared MariaDB-client MariaDB-compat MariaDB-devel

References:

A basic time difference calculator in google script js

In your Google Sheet, Click “Tools” and then choose “Script Editor“. Put the code given below. Don’t forget to use function in your result column as “=dodiff(cell1,cell2)

function dodiff(a, b) {
if (a)
a = fix(a);
if (b)
b = fix(b);
if (Array.isArray(a) && Array.isArray(b)) {
if (b[1] > a[1]) m = b[1] - a[1];
else {
m = b[1] - a[1] + 60;
b[0]--;
}
if (b[0] >= a[0]) h = b[0] - a[0];
else {
h = b[0] - a[0] + 12;
}
m = m / 60;
return (h + m).toPrecision(4);
}
}
function fix(a) {
if (a)
x = a.toString().replace(" ", '').replace("am", "").replace("pm", "").replace("PM", "").replace("AM", "");
x = x.split(":");
x[0] = parseInt(x[0]);
x[1] = parseInt(x[1]);
return x;
}