fputcsv make empty column

rgb192
rgb192 used Ask the Experts™
on


fputcsv($fp,array(NULL,NULL,NULL,$variable));

NULL makes empty column

could I say
$variable=NULL to have empty column
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
To have an empty column just send an empty string, or NULL if you wish.
If you take a look at the manual http://php.net/manual/en/function.fputcsv.php
you'll see that you can have a series of arrays, as array(array())
Basically what fputcsv does is take each array and print it on a single line, with comma (,) between the elements.
So if you had
 array (
    array('aaa', 'bbb', 'ccc', 'dddd'),
    array('123', '456', '789'),
    array('"aaa"', '"bbb"')
);
yououtput would be
aaa,bbb,ccc,dddd
123,456,789
"""aaa""","""bbb"""

If instead you would have all sencond values as '' (empty string) that column would be empty.
NerdsOfTechTechnology Scientist
Commented:
Yes, however you are UNSETing the variable $variable.

Use:

$variable = '';

(two single quotes, no spaces) instead.
$variable = '';

Open in new window

NerdsOfTechTechnology Scientist

Commented:
I meant to say if you use $variable = NULL; you actually UNSET the variable. Thus, to use $variable = '' should be better in this case
$variable = '';

Open in new window

PMI ACP® Project Management

Prepare for the PMI Agile Certified Practitioner (PMI-ACP)® exam, which formally recognizes your knowledge of agile principles and your skill with agile techniques.

Most Valuable Expert 2011
Top Expert 2016

Commented:
Code snippet outputs:

HERE IS VAR BETWEEN PIPES||NULL EMPTY

PHP is a loosely typed language, meaning that it converts data types to what it "thinks" you need.  So if you add integer 3 to float 0.5. PHP will give you float 3.5 for the answer.  With strings, the NULL value is equal to an empty string of zero length.

Note that I said "equal" and not "identical" -- strict comparisons, made with the triple equal sign find that NULL is not the same as an empty string of zero length.  But if you want an empty column in your output, you can use NULL or "" and either of those will do nicely.
<?php // RAY_temp_rgb192.php
error_reporting(E_ALL);

$var=NULL;
echo 'HERE IS VAR BETWEEN PIPES' . "|$var|";
var_dump($var);
if ($var == '') echo 'EMPTY';
if ($var === '') echo '===EMPTY';

Open in new window

Author

Commented:
but is there any way to
define $variable
before this statement
fputcsv($fp,array(NULL,NULL,NULL,$variable));

to make it a blank (empty) column
$variable = '';
fputcsv($fp,array(NULL,NULL,NULL,$variable));

should do the trick
Most Valuable Expert 2011
Top Expert 2016

Commented:
Regarding this:

but is there any way to
define $variable
before this statement
fputcsv($fp,array(NULL,NULL,NULL,$variable));

Try this:
fputcsv($fp,array(NULL,NULL,NULL,NULL));

There is no need for any variable -- if what you really want is NULL, use NULL.  It will make the code easier to read since the intent will be clear, and not obscured behind a variable name.

HTH, ~Ray

Author

Commented:
I wanted to set to NULL
but
'' works
NerdsOfTechTechnology Scientist

Commented:
?
fputcsv($fp,array('NULL','NULL','NULL','NULL'));

Open in new window

Most Valuable Expert 2011
Top Expert 2016

Commented:
@NerdsOfTech... Come closer to the warm coffee... NULL (the predefined PHP constant) is not the same as 'NULL' (the quoted character string).
http://php.net/manual/en/language.types.null.php

;-)

Best to all, ~Ray
NerdsOfTechTechnology Scientist

Commented:
LOL I know that's why I was wondering why rgb192 wants null if '' is the same, yes (that is in regards to php)? Please enlighten us Ray -- as you have more experience -- is NULL and '' the same in PHP?
NerdsOfTechTechnology Scientist

Commented:
Nevermind NULL is NULL and '' is EMPTY :)
Most Valuable Expert 2011
Top Expert 2016

Commented:
NULL and '' are constructively the same when writing data strings.  The advantage of using NULL versus '' is that you can easily see and understand that you deliberately wanted a NULL (empty) string.  I've used both and have come to prefer the more easily readable word NULL.  In a class of several hundred lines, you will see that it stands out better when you are trying to read the code.
Most Valuable Expert 2011
Top Expert 2016

Commented:
Sidebar note... zero is also empty() which is sometimes useful if you cast integer values and use them as DB keys.  MySQL has no zero in its sequence of auto_increment keys.

Bets to all, ~Ray
NerdsOfTechTechnology Scientist

Commented:
That's awesome info Ray! I understand and agree. Thanks again!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial