Showing posts with label php. Show all posts
Showing posts with label php. Show all posts

Wednesday, April 17, 2019

Refresh: Not a Number (NaN)

Today I answered the question from Enki on NaN (i.e. not a number). I failed (along with 40% of other developers).


My failure could be due to my lack of experience using NaN. Nonetheless, here's the explanation by Enki.


Wow, I never knew that there was an IEEE 754 standard corresponding to the NaN value. So naturally, I had to learn more because the simple explanation from w3schools was insufficient and I never ran into a case where I had to examine NaN === NaN or even NaN == NaN. Since I already knew what the comparison operator (equal type, equal value), I thought it could apply the same understanding as with the null object and undefined constant (see JavaScript data types).



But, I didn't see any quick examples from w3schools (my main online reference for quick understandings on standard web languages). Before I start digging, I refreshed my understanding from the quick lesson JavaScript's Numbers and now notice this new reference to the IEEE 754 standard. I definitely don't remember seeing that there, but could just be me.


Integers have an accuracy up to 15 digits - got it. Now, JS Numbers are always 64-bit double data types - got it. Reading further on this page, definitely don't remember an "Infinity" and I rarely remember the HexDecimal. Needless to say, refreshed my understanding of NaN.




Finally, refreshed my understanding with using the equality operator on objects. Unless comparing standard JavaScript data types (implicitly via var declaration), the object type doesn't matter because whenever two defined objects are compared the result is always false.

Mozilla provides further explanation with comparing NaN and its usage.



Okay, that's cool, but where did Enki get there explanation saying that all NaN will never have the same value?

I don't know and it would help if Enki did provide this online reference.

ICYMI like me, NaN is not just used in JavaScript. Surprise, that's right! So, double check your programming language if it has NaN (and Infinity).

Happy Coding!


Programming Languages using NaN (Brief List)



References






Thursday, September 1, 2016

Debugging - PHP + Javascript - Unexpected Token <

I recently came across an unusual error in the JavaScript Console when processing my PHP code into JavaScript to display in the browser. The line number provided in the console is misleading - b/c it's not the actual line number with the error. So I don't use the 'line number' information to debug.

Error

Code

The root cause is how either PHP is calculating the 'addition assignment' operation (i.e. a += b; ) within JavaScript tags during compilation or how JavaScript is processing the PHP output before displaying to the browser. I'm not exactly sure as this would require more research and investigation on my part which I don't currently have time - yay, deadlines!



Initial Attempt

At first, I thought I needed to replace the shorthand 'addition assignment' operator into its expanded equivalent. (See the green arrow in my image below.)


The expanded equivalent would be:
a = a +b;
or according to my code:
// line 91
$num_of_users_per_org[$k][1] = $num_of_users_per_org[$k][1] + 1;

Solution

At last (within 5 minutes), I figured out that I need to initialize the 2nd array in my multi-dimensional array corresponding to 'user_count'.  (See the light green arrow in my image below.)



Proof

After adding the code and rendering again in the browser, our results pass and the code works. Bingo!

Click on image to enlarge (see clearly)

Nice Debugging Reference

Wednesday, July 13, 2016

PHP Array indexes are CaSe-SeNsiTIvE

Devs,

Quick note, PHP array indexes are CaSe-SeNsiTIvE.

I ran into an issue with sending a $_POST to the database to insert a record. I was validating the data in the post array and setting required values if a key was not set properly. However, this gave me a database insert error stating I'm using duplicate field (column) names.

Well, ain't that tricky!

Did a quick search to confirm my assumption about PHP array indexes being case-sensitive.
http://stackoverflow.com/questions/1511230/php-array-are-array-indexes-case-sensitive

Yep, added the check for all cases and database insert worked! Yay!!

I wonder what other languages treat array indexes as case-sensitive? Maybe all programming languages? This would only make sense, no? The value of an index is calculated differently based on char value, right?

To be continued...

Happy Coding,
G2

Monday, June 6, 2016

Why Facebook hasn't migrated away from PHP

Dev Fam,

Sometimes I just don't have time to play with code and simply need to up my learning by reading a *good* article. Today's article is from my question identified by a post in Quora.

https://www.quora.com/Why-hasn-t-Facebook-migrated-away-from-PHP

This is good to know considering how our young generation today of developers can be influenced like fans of an entertainer. In the field of science and technology, we can't just go with the flow of the crowd or get caught in the wave of the current. STEM folk have to evaluate into the depth of what's happening with any new technology or scientific discovery and ensure its proper usage before applying. This is especially true for any large subject matter, product, or system.


Thursday, May 5, 2016

CodeIgniter: (4096) Object of class CI_Output could not be converted to string

Devs,

Earlier this morning, I ran into this CI Error which puzzled me for a split second.


I did my quick online search and didn't really find any answer on StackOverflow. I read a quick response on the CodeIgniter forum and then figured out my root cause.

Problem:  I was echo'ing the set_output($arg) method.

echo($orgs ? $orgs : $this->output->set_output("Status: FAIL"));

Attempt: The set_output($arg) method already echo's the input arguments to the view. So remove the echo'ing on the method.

$orgs ? echo $orgs : $this->output->set_output("Status: FAIL");

Solution: Since the ternary operator is not identical to an if-then execution, remove the set_output($arg) method. Parenthesis added for easier readability, but unnecessary.

echo ($orgs ? $orgs : "Status: FAIL");

A nice, quick fix and my expected results worked perfectly!

Good day,
G2

Tuesday, March 8, 2016

PHPUnit - Solving my Installation Issues

Hey Devs,

Problem

I ran into a few things while trying to install PHPUnit.

I read this article and then I also did some research on YouTube and SO, but didn't find anyone who resolved my issue.
https://phpunit.de/manual/current/en/installation.html

I am running PHP via my Bitnami application.

Solution

Here's how I resolved my issue.

First, I needed to add the PHP executable into my Windows PATH.
To be clear, I added a new PATH variable pointing to my PHP.exe file and then added this variable into my PATH:

PHP_HOME: C:\Bitnami\wampstack-5.5.29-1\php
Path: ...;%PHP_HOME%



Next, I added the PHPUnit executable into my Windows PATH. However, I did create the command script as instructed in the PHPUnit webpage (mentioned above). Notice, I created the "bin" folder as "phpunit" and placed within my Bitnami application folder.

PHPUNIT_HOME: C:\Bitnami\wampstack-5.5.29-1\phpunit\bin
Path: ...;%PHP_HOME%;%PHPUNIT_HOME%


Then, I was able to continue with the instructions per the PHPUnit Installation webpage. I ran into this error, but that's ok since I only need to update the latest PHAR file from PHPUnit.
*Note: Be sure to open a new CLI each time you update the PATH.


I'm not going to update my PHP version because I'm not sure what the effects will be on my application which I'm going to use unit tests for. I'd rather get a working PHAR first and then I'll update my PHP version and begin fixing any upgrade issues. So, I'm using PHPUnit 4.8.23 instead of current stable release version of 5.2.10.


Excellent, this message reflects the one on the PHPUnit webpage! Now I can move forward with the tutorials and creating unit tests - hip hop hoorayyyy!

Quick Online Reference

https://phpunit.de/manual/current/en/phpunit-book.pdf

Friday, November 6, 2015

Learning PHP - How to Begin and Win!

Dev Fam,

This is a good way to get started with learning PHP.

http://code.tutsplus.com/tutorials/the-best-way-to-learn-php--net-22287

The author, I think, has done a pretty good job with summing and articulating the steps to hit the track running as a PHP Developer.

Here are some book references as well.



Cheers,
G2