Link to home
Start Free TrialLog in
Avatar of rgb192
rgb192Flag for United States of America

asked on

fputcsv make empty column



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

NULL makes empty column

could I say
$variable=NULL to have empty column
ASKER CERTIFIED SOLUTION
Avatar of hexer4u
hexer4u
Flag of Romania image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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

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

Avatar of rgb192

ASKER

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
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
Avatar of rgb192

ASKER

I wanted to set to NULL
but
'' works
?
fputcsv($fp,array('NULL','NULL','NULL','NULL'));

Open in new window

@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
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?
Nevermind NULL is NULL and '' is EMPTY :)
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.
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
That's awesome info Ray! I understand and agree. Thanks again!