Category Archives: PHP

PHPStorm Live templates for PDO & PureCSS framework

Wanted to post Live Templates for phpStorm for PDO examples and for the quick PureCSS Framework ( http://purecss.io/ ) that I’ve created. I plan on updating the files as I add more to them.

PDO:

http://www.jmrowe.com/resources/livetemplates/PDO.xml

PureCSS:

http://www.jmrowe.com/resources/livetemplates/Pure.xml

Right-click the link to “Save link as..” and save in your .WebIde -> config -> templates folder to inspect/use the templates.

Creating an array from a list of items in a $_POST variable

Routinely, I’ll find my self creating forms that will take a list of items in a textarea that was pasted from another program like Excel ( column of data ). Furthermore, I will usually take this list of data and explode it into an array to be processed individually.

Using the explode function in php is simple, however, when separating items in the $_POST array by a newline – the newline may be different based on the type of php server.

\n is used for Unix systems (including Linux, and OSX).

\r\n is mainly used on Windows.

\r is used on really old Macs.

To avoid these differences, you can use the php constant PHP_EOL .

If we had a textarea named “list” – then it would look like:

 

Proper way to check if POST variable is actually set and not a empty string or NULL

Most people use isset to check if a POST variable  ( i.e. isset(_POST(‘textfield’))  ) has been entered. However, as the table displays below, if the user enters an empty string ( “”) isset will give an unexpected result.

Instead we should use the following:

 

The combination of array_key_exists and empty will check if the value is not only set but also not an empty string ( “”) or NULL.

Value of variable ($var) isset($var) empty($var) is_null($var)
“” (an empty string) bool(true) bool(true) bool(false)
” ” (space) bool(true) bool(false) bool(false)
FALSE bool(true) bool(true) bool(false)
TRUE bool(true) bool(false) bool(false)
array() (an empty array) bool(true) bool(true) bool(false)
NULL bool(false) bool(true) bool(true)
“0” (0 as a string) bool(true) bool(true) bool(false)
0 (0 as an integer) bool(true) bool(true) bool(false)
0.0 (0 as a float) bool(true) bool(true) bool(false)
var $var; (a variable declared, but without a value) bool(false) bool(true) bool(true)
NULL byte (“\ 0”) bool(true) bool(false) bool(false)

Table Credit to: @virendrachandak on Twitter